[SalesForce] Opportunity standard Clone button. How to detect cloned record in an Apex trigger

I have spent hours on it but haven't been able to figure out anything. The idea is simple:

  1. User clicks on standard Opportunity clone button.
  2. User selects either 'Clone without products' or 'Clone with Products'
  3. On the cloned record, the stage is set to Identified (first one) and certain custom fields are cleared out (doesn't matter if it's with or w/o products).
    I started off with a simple trigger like this:

    trigger OpportunityClone on Opportunity (before insert) {
    for (Opportunity o : Trigger.new) {
        if (o.isClone()) {
            o.StageName = 'Identification';
            o.IsCloned__c = true;
            o.Roll_Out_Plan__c = NULL;
            o.Rollout_Duration__c = NULL;
            o.Roll_Out_Start__c = NULL;
            o.External_Opportunity_ID__c = NULL;
       }
    }}
    

But for some weird reason, it works perfectly for the 'Clone With Products' button action but doesn't do anything for 'Clone Without Products' action.

It seems like the isClone() isn't being detected at all when doing the latter.

Is there a way to fix the above or an alternative better way? Any help is much appreciated. I have pulled my hair enough on this.

Best Answer

You can take this following approach.

  1. Create a custom field called Record Id in opportunity and update that field with Opportunity Id through the workflow field update (condition: when record is created).

  2. In the before insert trigger, verify if Record Id is null then that record is new record. and If Record Id is not null then that record is being created through cloning.

This way you can bypass the use of IsCloned() or getCloneSourceId() methods in trigger.

Please find below sample code.

trigger OpportunityClone on Opportunity (before insert) {
    for (Opportunity o : Trigger.new) 
    {
        System.debug('Record Id=' + o.Record_Id__c);

        if(o.Record_Id__c == null)
        {
            //handle records for new operation
        }
        else if (o.Record_Id__c !='') 
        {
           //handle records for clone
            o.StageName = 'Identification';                  
        }        
    }
}
Related Topic