Tuesday, January 31, 2017

Find WhoId and WhatId object name in Apex and Visualforce


We frequently use WhatId and WhoId attributes of Event and Task object.

According to Salesforce documentation those are:

The WhatId represents nonhuman objects such as accounts, opportunities, campaigns, cases, or custom objects. WhatIds are polymorphic. Polymorphic means a WhatId is equivalent to the ID of a related object. The label is Related To ID.


The WhoId represents a human such as a lead or a contact. WhoIds are polymorphic. Polymorphic means a WhoId is equivalent to a contact’s ID or a lead’s ID. The label is Name ID.
If Shared Activities is enabled, the value of this field is the ID of the related lead or primary contact. If you add, update, or remove the WhoId field, you might encounter problems with triggers, workflows, and data validation rules that are associated with the record. The label is Name ID.
Sometimes, we need to find out actual object reference instead of generic WhatId or WhoId


To display the Actual Object Name instead of WhatId use this

WhatId Object is: <apex:outputLabel>{!Event.What.Type}</apex:outputLabel>

It will display like this:
What Id Object

Similarly, to find the Object name of WhoId use this:

WhoId Object is: <apex:outputLabel>{!Event.Who.Type}</apex:outputLabel>

Who Id



Similarly, to find the name of Object from Id in Apex use this code:
Pass WhatId or WhoId which you are retrieving from this SOQL query into that method.

Event eventRec = [SELECT WhatId, WhoId FROM Event Where Id = '00U9000001NgqWF'];

public static String getSobjectNameById(Id inputId)
    {
        Schema.SObjectType sobjectType = inputId.getSObjectType();
        return sobjectType.getDescribe().getName();
    }

7 comments:

  1. Excellent post!!! The future of cloud computing is on positive side. With most of the companies integrate Salesforce CRM to power their business; there is massive demand for Salesforce developers and administrators across the world.Salesforce Training in Chennai | Salesforce Training Institutes in Chennai

    ReplyDelete
  2. Nice article Thanks for sharing this article Wonderful information from this website Thanks !!!

    Embedded training in chennai | Embedded training centre in chennai | PLC Training institute in chennai

    ReplyDelete
  3. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Hadoop training in bangalore

    ReplyDelete
  4. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    angularjs training in chennai
    Click here:
    angularjs2 training in chennai
    Click here:
    angularjs4 Training in Chennai
    Click here:
    angularjs5 Training in Chennai

    ReplyDelete
  5. Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
    Click here:
    Microsoft azure training in chennai
    Click here:
    Microsoft azure training in online
    Click here:
    Microsoft azure training in tambaram
    Click here:
    Microsoft azure training in chennai

    ReplyDelete
  6. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism online training

    Blueprism training in tambaram

    ReplyDelete
  7. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Devops training in tambaram|Devops training in velachery|Devops training in annanagar|Devops training in sholinganallur

    ReplyDelete