You need to sign in to do that
Don't have an account?
jack-cepheid
Trigger to update Related user
I'm new to Apex in general and cannot seem to get this trigger to work...
I have a custom object LOC that has a related custom object called GPO.
I need to have a trigger that will update a related user field on LOC with the owner of the related GPO.
I need the related user so I can use it in an approval process.
Here is what I have so far and it does nothing. From my research I know maps are needed but I don't understand the concept at this point.
trigger LOCapprover on LOC__c (before insert,before update) { for(LOC__c loc: Trigger.new){ if(loc.LOC_GPO__c != null) loc.Receiving_User__c = loc.LOC_GPO__r.Owner.Id; } }
The problem is this line:
if (loc.Account__r.GPO_Affiliation__c != null)....
You are trying to access related records again there, which will always result in null, which means that nothing gets added into the ID set.
Instead you'll need to move the related object request out to the query. Try something like this:
All Answers
Triggers won't give you immediate access to related records like you're trying to do there. Instead, you'll need to do a query yourself to get that information.
You could do it like this (though I would typically prefer to perform the query and logic in a class than directly in the trigger)...
Thanks that works great. My other question is this... Is it possible to reference a related objects related object? (example an LOC that is related to an Account and that Account is related to a GPO)
If that is possible will the existing map that the trigger has created allow me to pull the owner ID from the Accounts related GPO or is that asking too much of a trigger?
You can do that kind of relational lookup in the SOQL query, like:
select Id, .... Account.GOP__c from LOC__c where .....
Okay
I tried to write (well modified the one you posted) a separate trigger that would find the owner id of the related Accounts related GPO and there are no errors yet it does nothing.
Do i need to point the query to the account. record on this trigger?
The problem is this line:
if (loc.Account__r.GPO_Affiliation__c != null)....
You are trying to access related records again there, which will always result in null, which means that nothing gets added into the ID set.
Instead you'll need to move the related object request out to the query. Try something like this:
Absolutely amazing. I think I'm starting to understand the syntax of the Map method.
Thank you so much. My companies normal Apex guy is on an extended vacation so I was left to hack my way through the weeds alone.
I have a few small tweaks to make but this is fantastic.