You need to sign in to do that
Don't have an account?
Leo C
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:
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?
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?
Testing trigger code example - run that through a debug on a before update:
To save doing the query - which won't work on before insert anyways as the record won't exist - try using getSObjectType() - something like:
Regards
Andrew
All Answers
Testing trigger code example - run that through a debug on a before update:
To save doing the query - which won't work on before insert anyways as the record won't exist - try using getSObjectType() - something like:
Regards
Andrew
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.
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: 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