A lot of processing can occur when cloning large hierarchies of objects that contain custom Apex, workflows, process builders, and flows. One workaround is to split the cloning process into multiple steps where the primary object is cloned first, and then this is followed by copying child relationships that are processing intense. The user will need to click Save on each page to complete the full process.
There are a few drawbacks of this method. First, a user would end up with only a partially cloned hierarchy if they click cancel or exit the browser after the first step. We will hide the cancel button on some pages to reduce this possibility. Second, fields that use the action “Reference/Clone” or “Reference/Clear” reconnect lookup/master detail fields with new records that were cloned. If the referenced record is separated into a different step, the logic will not have the record information to set the field value.
This will show an example where the process is split into three steps. First, the primary object (Job) will be cloned using the Clone page. Then, a child relationship (Job Tasks) will be copied from the original hierarchy to the new primary object using the Copy page. Finally, the other child relationship (Job Orders) will be copied from the original hierarchy to the new primary object using the Copy page.
Each configuration will have the primary object (Job), but the child relationships will be different.
JobClone – Configuration to clone the primary object and some child objects that don’t have much logic to run.
We will break this into multiple steps to show how the URLs change as they get encoded and combined together. We will start with what the URLs would look like unencoded. The “buttonCancel=0” parameter will hide the cancel button. The “saveURL2” parameter will be the encoded URL of what page to load after the save button is pressed. The JobOrderCopy URL doesn’t have a “saveURL2” because it will redirect to the record referenced in the “rid” parameter.
/apex/lcrm_scp__scpClone?rid={!Job__c.Id}&cfg=JobClone&lss=1&saveURL2=
/apex/lcrm_scp__scpCopy?fromid={!Job__c.Id}&rid=[newid]&cfg=JobTaskCopy&lss=1&buttonCancel=0&saveURL2=
/apex/lcrm_scp__scpCopy?fromid={!Job__c.Id}&rid=[newid]&cfg=JobOrderCopy&lss=1&buttonCancel=0
First we will encode the JobOrderCopy URL. To make things easier we will replace characters !,{,},[,] with other characters. We don’t want them encoded, so the record Ids can be merged into the string as expected. For the example I’ll use three Zs(ZZZ) to mark them in the string, so I can spot them and revert them back to the bracket types required. To URL encode, I usually google an online utility. Here is one I’ve used in the past.
1) Below is the encoded string with replaced special characters.
%2Fapex%2Flcrm_scp__scpCopy%3Ffromid%3DZZZJob__c.IdZZZ%26rid%3DZZZnewidZZZ%26cfg%3DJobOrderCopy%26lss%3D1%26buttonCancel%3D0
2) We now append this as the saveURL2 parameter to the JobTaskCopy URL
/apex/lcrm_scp__scpCopy?fromid={!Job__c.Id}&rid=[newid]&cfg=JobTaskCopy&lss=1&buttonCancel=0&saveURL2=%2Fapex%2Flcrm_scp__scpCopy%3Ffromid%3DZZZJob__c.IdZZZ%26rid%3DZZZnewidZZZ%26cfg%3DJobOrderCopy%26lss%3D1%26buttonCancel%3D0
3) Then replace the special characters for merge variables in the new URL with ZZZ and encode again.
%2Fapex%2Flcrm_scp__scpCopy%3Ffromid%3DZZZJob__c.IdZZZ%26rid%3DZZZnewidZZZ%26cfg%3DJobTaskCopy%26lss%3D1%26buttonCancel%3D0%26saveURL2%3D%252Fapex%252Flcrm_scp__scpCopy%253Ffromid%253DZZZJob__c.IdZZZ%2526rid%253DZZZnewidZZZ%2526cfg%253DJobOrderCopy%2526lss%253D1buttonCancel%253D0
4) Take this string and then set it as the saveURL2 parameter on the JobClone URL
/apex/lcrm_scp__scpClone?rid={!Job__c.Id}&cfg=JobClone&lss=1&saveURL2=%2Fapex%2Flcrm_scp__scpCopy%3Ffromid%3DZZZJob__c.IdZZZ%26rid%3DZZZnewidZZZ%26cfg%3DJobTaskCopy%26lss%3D1%26buttonCancel%3D0%26saveURL2%3D%252Fapex%252Flcrm_scp__scpCopy%253Ffromid%253DZZZJob__c.IdZZZ%2526rid%253DZZZnewidZZZ%2526cfg%253DJobOrderCopy%2526lss%253D1%2526buttonCancel%253D0
5) Finally, fix the spots with ZZZ so the merge variables will work again.
/apex/lcrm_scp__scpClone?rid={!Job__c.Id}&cfg=JobClone&lss=1&saveURL2=%2Fapex%2Flcrm_scp__scpCopy%3Ffromid%3D{!Job__c.Id}%26rid%3D[newid]%26cfg%3DJobTaskCopy%26lss%3D1%26buttonCancel%3D0%26saveURL2%3D%252Fapex%252Flcrm_scp__scpCopy%253Ffromid%253D{!Job__c.Id}%2526rid%253D[newid]%2526cfg%253DJobOrderCopy%2526lss%253D1%2526buttonCancel%253D0
Add this URL to the custom button. Add the button to a page layout, and test it out to confirm it works as expected.
Below are screen shots using the very simple records and hierarchy, but they will give you an impression of what the users will walk through.
JobClone: click the clone button from the record page, and the first Clone page should display. Click Save.
JobTaskCopy: The copy page shows the original Job record, but fields are not editable because the copy page only creates the child record. Notice the Cancel button is hidden because of the URL parameter setting.