Detect when a record is being cloned in trigger
Asked Answered
T

3

16

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 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.

Thyroid answered 29/7, 2012 at 6:57 Comment(1)
Ideas: Clone - Ability to Control Which Fields are ClonedThyroid
C
15

In the Winter '16 release, Apex has two new 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.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_getCloneSourceId

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_getCloneSourceId

Carper answered 13/9, 2016 at 15:22 Comment(1)
This needs more loveHewe
E
5

One approach, albeit kind of kludgy, would be to create a new field, say original_id__c, which gets populated by a workflow (or trigger, depending on your preference for the order of execution) when blank with the salesforce id of the record. For new records this field will match the standard salesforce id, for cloned records they won't. There are a number of variations on when and how and what to populate the field with, but the key is to give yourself your own hook to differentiate new and cloned records.

If you're only looking to control the experience for the end user (as opposed to a developer extending your managed package) you can override the standard clone button with a custom page that clears the values for a subset of fields using url hacking. There are some caveats, namely that the field is editable and visible on the page layout for the user who clicked the clone button. As of this writing I don't believe you can package standard button overrides, but the list of what's possible changes with ever release.

Eugenaeugene answered 29/7, 2012 at 22:10 Comment(0)
M
1

You cannot detect clone operation inside the trigger. It is treated as "Insert" operation.

You can still use dedicated code to invoke sObject.Clone() and exclude the fields that aren't required. You can ensure that you include all fields by using the sObject describe information to get hold of all fields for that object, and then exclude the fields that are not required.

Hope this makes sense!

Anup

Mitten answered 29/7, 2012 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.