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
mcareymcarey 

Trigger Help

Trying to set a custom sales region field with the owner's sales region defined in the user object.  I was hoping to use the formula field to do this but apparently I can't link the Owner from Opportunity to the User object there.  Here is what I have so far and I know I am way off.  Any help is appreciated.

 

trigger myAccountTrigger on Account (after insert, after update) {  
if (Trigger.isInsert) { 
// 
}  
if (Trigger.isUpdate) { 
        for(Opportunity o: Trigger.new){
            region = [select User.Sales_Region_c from User where User.ID = OwnerID]
            Sales_Region__c = region
      } 

 }

raseshtcsraseshtcs
I noticed that you are using trigger on Account and inside the trigger using the opportunity object. Could you clarify whether you need the field update on the account or the opportunity object
mcareymcarey

Sorry about the confusion.  It is on the Opportunity object.  I copied and pasted the first draft instead of the edited version, not much different than where I was stuck except I was going against the Opportunity and not Account.  I would like to replace the field with the User object Sales Region when a new Opportunity is created or it is edited.  I will most likely apply this to the Account object as well so maybe that could be in the same trigger.  Thanks for the help.

Starz26Starz26
trigger myOppTrigger on Opportunity (before insert, before update) {  

Set<id> uID = New Set<id>();
Map<ID,User> mUserRegion;

for(Opportunity o : trigger.new){

uID.add(o.OwnerID);

}

mUserRegion = New Map<ID,User>([Select ID, Sales_Region__c FROM User Where ID IN :uID]);

if (Trigger.isBefore && !Trigger.isDelete) { 
        for(Opportunity o: Trigger.new){
           If(mUserRegion.get(o.OwnerID) != Null) 
o.Sales_Region__c = mUserRegion.get(o.OwnerID).Sales_Region__c; } } }

 

mcareymcarey

Awesome, thanks for the help.  I'll try it out over the next couple days.