0

At the beginning of every year, we mass update any open opportunities that were created 2 years prior and prevent users from changing the sales stage back to an open stage. In the event that any of these opportunities are still active, we instruct users to clone them.

However, in cloning them, visibility of the original opportunity create date is lost. I want to create a field that will capture this date but for the life of me cannot figure out how to do so.

Can anyone assist? Thanks in advance.

2
  • Do both opportunity have any direct relationship? Commented Jan 23, 2018 at 6:43
  • One is cloned from the other using the native clone with or without product functionality.
    – toddk
    Commented Jan 23, 2018 at 7:01

1 Answer 1

1

You can populate the new field from the cloned Opportunity's CreatedDate in insert trigger.

Apex has two methods that let you detect if a record is being cloned and from what source record id. You can use this in your triggers.

  • isClone() - Returns true if an entity is cloned from something, even if the entity hasn’t been saved.
  • getCloneSourceId() - Returns the ID of the entity from which an object was cloned.

Refer this thread, for more details

Now, using the isClone() method you will get the cloned record and getCloneSourceId() will give you the original record Id, which can be used to query the CreatedDate.

4
  • Thanks! I was afraid of that...I'm sure these methods would do the trick but unfortunately, I don't know how to use them.
    – toddk
    Commented Jan 23, 2018 at 7:20
  • You can try it out and let us know if you are facing any difficulties in it Commented Jan 23, 2018 at 7:22
  • Sorry, Hemant. I don't know where to begin. I don't know where to use this function in a trigger and how I would then populate a field with it. I have this basic trigger that I use to populate an IsCloned field that I created but that's as far as I was able to get
    – toddk
    Commented Jan 23, 2018 at 7:55
  • trigger OppClonedTest on Opportunity (before insert) { for (Opportunity o : Trigger.new) { System.debug('Original Opportunity Id=' + o.Original_Opportunity_Id__c); if(o.Original_Opportunity_Id__c == null) { o.IsCloned__c = FALSE; } else if (o.Original_Opportunity_Id__c !=' ') { o.IsCloned__c = TRUE; } } }
    – toddk
    Commented Jan 23, 2018 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .