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
brenda.buckner@rsmi.combrenda.buckner@rsmi.com 

Code for trigger updating opportunity field with value on owner field

I'm just getting my feet with with learning triggers, so I'm looking for help on code!

 

We have a custom opportunity field (Opportunity MC--basically like a region and/or area of the company the opportunity is being attributed to) that needs to be updated with the value of a custom field on the user page of the opportunity owner (EU). I've been manually doing this on new opportunities, but it would be nice if this is done automatically, as well as whenever the opportunity owner is changed (which is something not being captured currently). I came up with a formula I could use for a formula field [ TEXT(OwnerLookup__r.EU__c ) ], but I don't want to change the values on closed opportunities, when we used different names for the regions.

Essentially the Opportunity MC field needs to equal the EU field of the opportunity owner whenever a new opportunity is created or when the opportunity owner changes. Thanks for any help!

ritika@developerforceritika@developerforce

Hi,

 

The trigger can be the solution - a trigger on opportunity, where you can compare the owner's old and new value and make the required changes -

 

trigger Opportunity_MC_Update on Opportunity (before insert, before update) {

 

        for(Id key : Trigger.newMap.keySet()){

             if(Trigger.isInsert || (Trigger.isUpdate && Trigger.newMap.get(key).OwnerLookup__c != Trigger.oldMap.get(key).OwnerLookup__c) ) {

                      Trigger.newMap.get(key).MC = OwnerLookup__r.EU__c;

             }

        }
}

 

 

Make sure all fields are correctly referred. In case OldMap does not return the value of Owner

Hope this helps.

 

 

 

brenda.buckner@rsmi.combrenda.buckner@rsmi.com

Thank you! I'll give this a try.

brenda.buckner@rsmi.combrenda.buckner@rsmi.com

Just got this error:

Compile Error: Variable does not exist: OwnerLookup__r.EU__c

 

Any suggestions?