Tuesday, 4 August 2015

Cloning records in Salesforce

7 ways (at least) to Clone records in Salesforce 

I had a call from a customer last week asking me to look into an issue. The reported problem was that the "Clone" button in Salesforce was no longer working correctly for them.

This surprised me as the Clone function in Salesforce is completely standard, and to my knowledge I had not made any customisations to it.

After digging into this further I learnt a lot more about the Salesforce.com Clone function.


The issue

After talking to the customer I had a clearer idea of the issue. The client was navigating to a Case record in their Salesforce ORG, clicking the Clone button and then going to the newly created case.
The standard Clone button displaying on the Case object

The client was reporting that several additional information fields from the original case were not present in the copied version.

What exactly is Cloned?

It soon became apparent that all of the fields missing from the cloned copy were also missing off the page layout of the case originally being cloned.

So the issue in summary was that only the fields being displayed to the user at the time of clicking on Clone are actually copied - the rest are discarded.

A bug or standard behaviour?

After looking into this a bit more I was surprised to find out that this is standard behaviour in Salesforce. Only visible fields on a page layout on a record are included in a clone function.

I came upon the following articles discussing this limitation:


You can vote for a change to the current clone button via the success community. I personally think it would be excellent if the clone functionality could be customised to either copy the entire record or just the visible section.

Next steps

I asked the customer why they needed the fields to be copied but did not have them on the page layout. Their reply was that the fields contained useful data for reporting but rarely had to be edited by the user so had been removed to simplify the page.

To help the customer with this issue we discussed a number of approaches that could help. I have listed them below in case you ever come across this issue.

Please do let me know in the comments if there are any additional ones that you would have suggested in the same situation.

Alternative approaches


1. Add all required fields to the Page Layout and keep standard Clone function

The simplest fix seemed to be to ensure that each field needed on the cloned record was present on the page layout of the record. After exploring the other options below, this is the one that the customer eventually selected in this case.

2. Amend the clone button and use URL hacking to populate new Case

There is a great post from Greg Hadric on amending the Clone button and populating the URL manually to have more control over the values of the cloned record. However, this would have to be done in conjunction with ensuring that all required fields are on the page layout, otherwise the value would again be blanked, even if specified in the URL.

https://www.interactiveties.com/blog/2011/clone-records.php#.VcEVIvkyPIU

3. Set up a Workflow to change record type for Cases to be copied

It would be possible to initiate a copy of a record using Workflow. Instead of clicking on the Clone button your users could just check a box marked "Copy" or select a certain Picklist value on the page and then click save. The Workflow would be set up with a criteria waiting to see which records had been selected. As soon as the user saved the record with the matching criteria, the Workflow would execute.

There is a good example of doing this from Bartley on the Stack Exchange. http://salesforce.stackexchange.com/questions/51092/how-to-change-record-type-by-workflow

4. Use Process Builder to automate record creation
Rakesh Gupta gives an excellent overview of how the Salesforce.com Process Builder could automate the cloning of required Case records for the customer.

5. Ask users to change record type before Cloning a record

Page layouts are linked to Record Types. One option nearly selected by my customer was to create an additional record type and page layout labelled "For Copying". The "For Copying" Layout & Record Type would have all of the fields on display so when the standard Clone function was used all fields would be copied.

This was a very straightforward solution but would involve the user remembering to change the record type before each Case clone.

The Case Record Type change screen
6. Remove standard Clone button and Case Record with Visualforce Page and custom Clone button

You could develop the standard Case object page and replace it with a Visualforce page. Recreating the layout in Visualforce would allow you a lot more flexibility in how the clone was performed. You could create a custom controller and have it initiated when a user clicks a customised clone button.

I found a really detailed example of doing this on Jeff Douglas' blog: http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/

7. Use Apex method to clone record

You could use Apex to clone the selected record. You would need to decide whether the user should initiate the copy or whether you would control it programmaticly - i.e copy all cases where subject = "X, Y, Z". 

This example from Scott Hemmeter gives an example of how to accomplish this.


Summary

The standard Clone function is very useful in Salesforce, but if you want to ensure that hidden fields are copied as well as the currently visible ones you will need to look at customising it. 

The least technical solutions involve editing page layouts or using record types, leading through to workflows and process builders, before ending up at custom Visualforce pages and Apex programming.

The golden rule is usually to utilise "clicks not code" wherever possible but your eventual selection will be driven by your customer's requirements and the system users. 

Hopefully we'll see an enhancement to cloning in a future release of Salesforce.com but in the meantime, I hope some of these approaches are useful for you all.

A standard Case layout in Salesforce.com