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
vgorgolvgorgol 

determine if Event whatid is an account

I have a custom button on the Event Page Layout designed to summarize the account/contact/event information from an event.

 

However, we require that an account be associated to the event in order to use the button.

 

The What field can be a number of different object types.

 

How can I programmatically ensure that the what is in fact an account and not something else?

 

Using javascript, I attempted to use the accountid:

 

var eventAccount = '{!Event.accountId }';

 

but there is no actual accountid field on Events.

 

"Field Event.accountId does not exist. Check spelling."

 

Is there a way to access accountid via javascript?

 

Thanks for the help!

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

You could also do:

SObject WhatIdObj = event.What.Id;

if(whatIdObj.getsObjectType() == Account.sObjectType){

 

}

All Answers

guest1231231guest1231231

You'll notice in the URL for accounts that they all have an Id that starts with 001, https://ssl.salesforce.com/00100000003Kvh5.

 

 

if(whatId.startsWith('001')) {
//yes the task is related to an account so do something
}

 

 

vgorgolvgorgol

Now that's just sneaky. Thanks!

Anand@SAASAnand@SAAS

You could also do:

SObject WhatIdObj = event.What.Id;

if(whatIdObj.getsObjectType() == Account.sObjectType){

 

}

This was selected as the best answer
Dan_CDan_C

Hey ,

 

I am attempting something similar. I need to determine the object type for WhatId on some tasks. I've tried your example above and am getting the error "Illegal assignment from Id to SObject". Any help would be appreciated.

 

My Code is

for (Task t : completedTaskList) {
    SObject WhatIdObj = t.What.Id;
    if(whatIdObj.getsObjectType() == Case.sObjectType){
        //body of code
    }
}

 

RayGRayG

You might want to try

 

SObject WhatIdObj = t.What;

 

FlippsieFlippsie
Set<Id> accountIdSet = new Set<Id>();
for (Event evt : eventList){	
    if(evt.WhatId.getsObjectType() == Account.sObjectType){
        accountIdSet.add(evt.AccountId);
    }
}
You need to used WhatId, not What or What.Id  I've added this to a Set, because on its own, the account id is not going to be all that useful and you are probably going to want to write some soql to return the actual Account objects and you are not going to do this 1-by-1.