0

I have the following trigger for changing values when a record is cloned:

trigger oppAutomation on Opportunity (before insert) {
   if(trigger.isBefore){
         if(trigger.isInsert){
            for (Opportunity o : trigger.New){
                if(o.isClone() && o.StageName =='Closed Won'){
                    system.debug('Cloned Won Opp');
                    o.ClosedWonStage__c ='Pre-Approved';
                    system.debug(o.ClosedWonStage__c);
                }
            }
        }
    }

}

My issue is: In my test class after i clone a record the ClosedWonStage__c field takes the value of the record it was cloned from, As I expect.

While in the above trigger the ClosedWonStage__c value is set to 'Pre-Approved'

After the record is insert the ClosedWonStage__c value is resetting to the value of the original record.

Do I need to set the value of this field after the record is insert?

  • whats the value you are getting in : system.debug(o.ClosedWonStage__c); – SFDC FAN May 16 '18 at 10:50
  • system.debug(o.ClosedWonStage__c); - The value here is 'Pre-Approved' but then in my test class after I insert the Cloned Opp it sets back to the original record Value – denisoleary May 16 '18 at 14:01
0

It must set the value of ClosedWonStage__c in before insert, you do not need to update it explicitly.

Please check if there is any workflow/process builder which may update value of this field from Stage Name.

  • I don't have any Workflows/Processes that are updating anything on the Opportunity Object – denisoleary May 16 '18 at 14:04
  • I just copied pasted your code in my dev org...And cloned by using clone button..It's working as expected – Ayub May 16 '18 at 14:10
  • Please check your debug logs you will come. To know.. May be other related trigger also effect that – SFDC FAN May 16 '18 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.