function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
phantom1982phantom1982 

Determine the Object Type

How would you determine what object is associated with an event. The Related To field on Events object is a drop down of Objects so how can one determine the selected object at runtime?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
hchhch

Retrieve the first three characters of the RelatedTo id. And use the comparison statement:

string parentPrefix = parentId.substring(0,3);

if(parentPrefix == '001')

    //Object = Account

else if(parentPrefix == '003')

    //Object = Contact

RelatedTo 

All Answers

hchhch

Retrieve the first three characters of the RelatedTo id. And use the comparison statement:

string parentPrefix = parentId.substring(0,3);

if(parentPrefix == '001')

    //Object = Account

else if(parentPrefix == '003')

    //Object = Contact

RelatedTo 
This was selected as the best answer
Jeremy_nJeremy_n

Another even simpler (possibly) way is to use the What.Type field. This will return 'Opportunity' or 'Account' or whatever, and can be used as a WHERE in queries as well.

list<Event> evs = [select Id, WhatId, What.Type from Event where What.Type = 'Account'];
for (Event ev : evs)
{
   system.assertEquals('Account', ev.What.Type);
}

 

Jeremy