Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
4 views
in Salesforce by (11.9k points)
edited by
Is there a way to detect that a record being inserted is the result of a clone operation in a trigger?

As part of a managed package, I'd like to clear out some of the custom fields when Opportunity and OpportunityLineItem records are cloned.

Or is a trigger not the correct place to prevent certain fields being cloned?

I had considered creating a dedicated code to invoke sObject.Clone() and excluding the fields that aren't required. This doesn't seem like an ideal solution for a managed package as it would also exclude any other custom fields on Opportunity.

1 Answer

+2 votes
by (32.1k points)
edited by

Apex has two methods which help you to detect if a record is cloned or not. The following are the sObject methods used to avoid this:

isClone(): It returns true if there is an entity being cloned from something. 

Are you interested in learning Salesforce from scratch! Have a look at this interesting video on Salesforce provided by Intellipaat:

getCloneSourceId(): This method is used to return the entity ID from which an object has been cloned. For example: If P is cloned to Q, Q is cloned to R, and R is cloned to S, then Q, R, and S all point to A as their clone source.

Browse Categories

...