How To: Clone a Hierarchy of Records Asynchronously with Super Clone Pro
February 19, 2018
How To: Increment a sequence in a text field when cloning a record in Salesforce
June 24, 2018

How To: Clone a record and delete the original record in Salesforce

Deleting the original record after successfully cloning it can be accomplished with a combination of Apex, a custom field, a Process Builder, and Super Clone Pro configurations. This sounds like a lot, but I promise that it will be easy. These directions will give you the Apex code that lets a Process Builder delete records, and they will illustrate all of the other settings you’ll need.

Fist we will get the hard part out of the way first. We will need to deploy some Apex code. Process Builder does not have a feature that lets you delete records. It will let you call an Apex class that can delete records, and that is what we are doing here. The only thing that may need adjustment is the test class. This test class creates accounts in the test. It may need to be changed if your environment has additional requirements to create an account. If not, you should be good to go.

  • Create a class named “PbDeleteRecords”, and paste in the code below.

  • Create a class named “PbDeleteRecordsTest”, and paste in the code below.

  • Work with your Salesforce administrator to deploy the two classes.

Next, lets create a custom field on the object that we will be deleting. Users should have Edit permission to the field, so Super Clone Pro can assign a value to the field. However, this field should not be displayed on any page layouts.

  • Create a “Text” field 18 characters long that is labeled “Delete Source Id”.

Now we will create a Process Builder flow to run the Apex class we deployed.  The Process Builder logic will be set to only run on Insert to prevent the delete logic from running if someone accidentally enters a value into the field at a later date.

  • Create a Process Builder flow for your object.  The example below uses the Account object to demonstrate the steps.

  • Create a condition named “Delete source record”.  Enter the logic shown below to only run the steps if the record is new and field value isn’t blank.

  • Create a Call Apex action named “Delete Source Record”. This step calls the Apex method, and it passes in the value from the “Delete Source Id” custom field.

  • Create a Field Update action named “Clear Delete Source Id”. This step will blank out the value of the “Delete Source Id” custom field.

  • Activate the Process Builder.

We are almost there. Now we need to set the Clone configuration to put the original record’s Id into the “Delete Source Id” custom field, so it is there for the Process Builder.

  • Open the Super Clone Pro configuration for your object.
  • Find the “Delete Source Id” field in the list, and set the field Action to “Action Formula List”. Then enter “field(Id)” in the value column. This field should not be displayed on the page.
  • Save the configuration.

That’s it! That wasn’t too hard after we got the Apex class out of the way. Now the next time you clone, the original record will be deleted when the new record is inserted.


* Here is the disclaimer. The code comes with no warranty. I think it’s pretty good, but I’m bias because I wrote it. However, every Salesforce environment has a unique setup with different objects and custom logic, so there are bound to be instances where things don’t run as expected. This logic will delete records. Please test, test, and test some more before deploying any new logic.

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.