+ Start a Discussion
Leo ChalvinLeo Chalvin 

Issues gathering information in trigger for Event object.

I have an Apex Trigger that has for main goal to set the 'Assigned To' (OwnerId) field depending on on the 'Related To' (WhatId) record's type.

I feel like I'm going a little bit crazy at this very instant because I could have sworn the trigger was working last week.

This is give or take what my code looks like currently:
trigger EventAssignToTrigger on Event (before insert) {
    for (Event e :Trigger.New) {
        String relatedObject = e.What.Type;
        
        if(e.What.Type == 'Account' || e.What.Type == 'Contract__c') {
            //related to Account, NDA Contract, or Vendor Contract
            if(e.What.Type == 'Account' || e.what.recordTypeId == '0121t000000EC3cAAG') {
                e.OwnerId = '0231w0000010AWx';  //Calendar 1
            } else if (e.what.recordTypeId == '0121t000000EC3mAAG') {
                e.OwnerId = '0231t000001EAwZ';  //Calendar 2
            }  else {
                e.OwnerId = '0231t000001EAwj'; //Calendar 3
            }
            
        }   
    }
}

The code above is slightly simplified but that is basically the just of it. For some reason I can't seem to access the 'What.Type' or 'What.RecordTypeId' from the trigger. Is that normal?
I know WhatId returns what it should but for some reason What.* returns null.
If this is normal, what are my options in terms of getting the WhatId object's type and record type?
Best Answer chosen by Leo Chalvin
Andrew GAndrew G
I am thinking that you don't get the cross-object fields in the trigger but would need to query them:
Testing trigger code example - run that through a debug on a before update:
trigger EventAssignToTrigger on Event (before insert, before update) {

	for (Event e :Trigger.New) {
		System.debug('****DEBUG****: ' + e.WhatId);
		System.debug('****DEBUG****: ' + e.What.Type);
	}

	List<String> eIds = new list<String>();
	for (Event e : Trigger.New) {
		eIds.add(e.Id);
	}

	List<Event> events = new List<Event>([SELECT Id, WhatId, What.Type FROM Event WHERE id IN :eIDs]);

	for (Event e : events ) {
		System.debug('####DEBUG####: ' + e.WhatId);
		System.debug('####DEBUG####: ' + e.What.Type);
	}
}

To save doing the query - which won't work on before insert anyways as the record won't exist - try using getSObjectType() - something like:
trigger EventAssignToTrigger on Event (before insert, before update) {
	for (Event e :Trigger.New) {
		System.debug('$$$$DEBUG$$$$: ' + e.WhatId.getSObjectType());
        if(e.WhatId.getSObjectType() == 'Account' || e.WhatId.getSObjectType() == 'Contract__c') {
            // do stuff
        }
	}
}


Regards
Andrew

 

All Answers

Andrew GAndrew G
I am thinking that you don't get the cross-object fields in the trigger but would need to query them:
Testing trigger code example - run that through a debug on a before update:
trigger EventAssignToTrigger on Event (before insert, before update) {

	for (Event e :Trigger.New) {
		System.debug('****DEBUG****: ' + e.WhatId);
		System.debug('****DEBUG****: ' + e.What.Type);
	}

	List<String> eIds = new list<String>();
	for (Event e : Trigger.New) {
		eIds.add(e.Id);
	}

	List<Event> events = new List<Event>([SELECT Id, WhatId, What.Type FROM Event WHERE id IN :eIDs]);

	for (Event e : events ) {
		System.debug('####DEBUG####: ' + e.WhatId);
		System.debug('####DEBUG####: ' + e.What.Type);
	}
}

To save doing the query - which won't work on before insert anyways as the record won't exist - try using getSObjectType() - something like:
trigger EventAssignToTrigger on Event (before insert, before update) {
	for (Event e :Trigger.New) {
		System.debug('$$$$DEBUG$$$$: ' + e.WhatId.getSObjectType());
        if(e.WhatId.getSObjectType() == 'Account' || e.WhatId.getSObjectType() == 'Contract__c') {
            // do stuff
        }
	}
}


Regards
Andrew

 
This was selected as the best answer
Leo ChalvinLeo Chalvin
@Andrew G
Thank you for your response. That's extremely helpful! I figured there was probably an accessibility issue with cross-object fields.
Do you have any ideas as to how I could get the RecordTypeId given e.WhatId? I am not aware of any methods similar to e.WhatId.getSObjectType() that I could use to get the record type ID.
Andrew GAndrew G
Hi Leo

I had a quick play with .getSObject but it didn't want to play - perhaps a vagary with the Event Object and whatid

The only way I can think would be to do a Select Query for Accounts related to the trigger for the object.

Psuedo code:
for (event e:trigger.new) 
    if .getSbobjectType == Account
        add id to account id list
    end if
end loop

Map<id,account>=SELECT Id,recordtypeid FROM account where ID in :accountid list

for (event e:trigger.new)
    if .getSbobjectType == Account
        if map.containsKey(whatid)
           account = map.get(whatid)
           if account is recordtype 
             //do stuff
           end if
       end if
    end if
end loop
note, in the first loop when creating the Account Id List, you could also be populating a list of Events.  That way your second loop can skip the test for if .getsSobjecttype == account.


Regards
Andrew