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
gshuflingshuflin 

How do I write a trigger that modifies the original object when I clone it?

I want to set it up such that when a user clones an Opportunity, salesforce automatically sets a custom field of the original Opportunity object that the new one was cloned from to a particular value. My thinking is that I could write a trigger that runs when an Opportunity is created, but I'm not sure how to get a reference to the original object using the standard Trigger functionality. Maybe there's another way to go about doing this.

Shashikant SharmaShashikant Sharma

there is no trigger on clone of any object. Clone will call insert trigger. So in trigger you can not directly determine it is a clone. One way might be a method that search a records having all fields similar to the current record then if any record is resulted in search then we can decide it is a clone. But in this case even if any one will insert a record using apex code with same value will be considered as clone.

WizradWizrad

In this case you don't write a trigger.  Override the Clone functionality.  Just create a VF page that is backed by a controller that performs a clone and redirects the user to detail page of the clone when the operation completes.  When you implement the clone yourself, you will have access to the new and old version.