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
JaredPHJaredPH 

Help? Trigger to insert user id when oppty changed from particular stage

We change the owner of our opportunities when the owner changes from one certain stage to another, but need to have the previous owner for a report. I want to put the name of the previous owner in a custom lookup field that I created when the stage changes from "App In" to something else. I'm really a beginner with triggers, can anyone help? I've tried doing it with workflows and it doesn't allow me to do it. I'm thinking I need an apex trigger.

 

Any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
craigmhcraigmh

Yeah, it should be before update.

 

trigger OppUpdatePriorOwner on Opportunity(before update){
   for(Opportunity opp : trigger.new){
      if(Trigger.oldMap.get(opp.ID).StageName != 'App In' && opp.StageName == 'App In'){
         opp.CUSTOM_PREVIOUS_OWNER_FIELD = Trigger.oldMap.get(opp.ID).OwnerID;
      }
   }
}

 

All Answers

stollmeyerastollmeyera

Shoudl be something like this:

 

trigger OppUpdatePriorOwner on Opportunity(after update){

   List<Opportunity> oppsToUpdate = new List<Opportunity>();
   for(Opportunity opp : trigger.new){
      if(Trigger.oldMap.get(opp.ID).StageName != 'App In' && opp.StageName == 'App In'){
         opp.CUSTOM_PREVIOUS_OWNER_FIELD == Trigger.oldMap.get(opp.ID).OwnerID;
         oppsToUpdate.add(opp);
      }
   }
   if(oppsToUpdate.size() > 0){
      update oppsToUpdate;
   }
}

 Sort of in a hurry, so didn't have a chance to test or check for types. Let me know if it throws any errors.  

JaredPHJaredPH

It doesn't seem to like the line:

 

opp.App_Rep__c == Trigger.oldMap.get(opp.ID).OwnerID;

 

Compile error: Expression cannot be a statement at line 6 column 10

craigmhcraigmh

Yeah, that line should be an assignment, not a comparison. Use only one equal sign.

JaredPHJaredPH

Thanks again.

 

Compiled ok but then when I ran a test record it gave me this error:

 

Error:Apex trigger OppUpdatePriorOwner caused an unexpected exception, contact your administrator: OppUpdatePriorOwner: execution of AfterUpdate
caused by: System.FinalException: Record is read-only: Trigger.OppUpdatePriorOwner: line 6, column 1

 

Not sure why it says the record is read-only. It's a normal custom lookup field and I have all the permissions to edit it.

craigmhcraigmh

Yeah, it should be before update.

 

trigger OppUpdatePriorOwner on Opportunity(before update){
   for(Opportunity opp : trigger.new){
      if(Trigger.oldMap.get(opp.ID).StageName != 'App In' && opp.StageName == 'App In'){
         opp.CUSTOM_PREVIOUS_OWNER_FIELD = Trigger.oldMap.get(opp.ID).OwnerID;
      }
   }
}

 

This was selected as the best answer
stollmeyerastollmeyera

@craigmh, you beat me to it :)

craigmhcraigmh

I had the easy part, you did all the grunt work.

JaredPHJaredPH

Thanks again. Still trouble. Wish I wasn't so clueless and could try something myself, but I'm over my head here.

 

Error:Apex trigger OppUpdatePriorOwner caused an unexpected exception, contact your administrator: OppUpdatePriorOwner: execution of BeforeUpdate
caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.OppUpdatePriorOwner: line 11, column 1

craigmhcraigmh

You shouldn't need anything more than what I posted. Make sure that you remove the update statement.

 

In a before insert/update trigger, any changes made to the sObject collection are automatically committed when the object(s) are inserted/updated.

JaredPHJaredPH

Right. Working great now. Not sure what I had wrong.

 

Thanks so much for your help!