How To: Clone a record and delete the original record in Salesforce
April 25, 2018
How To: Clone Person Accounts in Salesforce
September 23, 2018

How To: Increment a sequence in a text field when cloning a record in Salesforce

Super Clone Pro is a flexible tool that lets you set field values for your cloned records. However, one feature that it doesn’t contain is the ability to set an incremented sequence number in a name or text field when a record is cloned. Super Clone Pro doesn’t contain the feature because it requires querying other existing records to find the next highest sequence number. Depending on the environment, processing to do this could lead to unexpected results or hitting governor limits.

The following code will allow you to set a sequence number. It can be run from either a process builder or trigger, and you can customize the logic to work within your environment’s constraints. Running it from a trigger is more efficient because it can set the field value in the before insert context, and it will not need to run an update on the record. Running the code from a process builder will run a DML update, but it will be easier for administrators to implement.

To use this code with Super Clone Pro, deploy the classes to your production environment. Then add a a call to the setVersionNumber method in the object’s before insert trigger or as a process builder Apex method when the object is created. Be careful not to execute the method on update because it could run recursively depending on how it is implemented. If you need this logic to only run when cloning a record with Super Clone Pro, you will need to set another field on the object as an indicator for the the logic to execute. For example, you could set a hidden checkbox field to checked that indicates the inserted record is coming from Super Clone Pro. You would then need to uncheck to checkbox after the clone logic ran in the trigger or process builder.

We provide no warranty for this code because everyone’s environment is a little different. Use at your own risk.

Class: IncrementSequence

Test Class: IncrementSequenceTest

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.