Showing posts with label Overriding clone button in salesforce. Show all posts
Showing posts with label Overriding clone button in salesforce. Show all posts

Wednesday, April 11, 2012

Deep clone functionality

There is often a need to copy all the related lists associated with an object. The following example helps to copy the related lists by using an extension. 

Deep Clone

Salesforce provides a clone button by default. This helps to clone the existing record. However, at times there is a need to clone the records along with the related lists. 

The following solution provides a programmatic way using Apex extension to implement Salesforce.com deep clone functionality.

Standard Clone Functionality
Step 1 : Create a visual force page


Create the page with following lines.  Standard controller can be any object. In this example I used Opportunity
 <apex:page standardController="Opportunity" 
    extensions="cloneExtension" 
    action="{!cloneRecord}">
    <apex:pageMessages />
</apex:page>

Note:-
When overriding buttons with a Visualforce page, only Visualforce pages that use the standard controller for the object on which the button appears can be selected. For example, if you want to use a page to override the button on accounts, the page markup must include the standardController="Account" attribute on the <apex:page> tag.
Save this page. 


Step 2: Create the extension

public with sharing class cloneExtension 
{
    //Variable to hold current record
    Opportunity currentRecord;
    
    //Standard constructor method
    public cloneExtension(ApexPages.StandardController controller) 
    {
        currentRecord = (Opportunity)controller.getRecord();
    }
    
    public PageReference cloneRecord()
    {
        //Variable to hold the new record
        Opportunity newRecord;
        Savepoint sp = Database.setSavepoint();
        try
        {
            currentRecord = [Select id, name from Opportunity where id =:currentRecord.id];
            newRecord = currentRecord.clone(false);
            insert newRecord;
            
            //Copy the related list - Test (this is a sample related list I created) 
            List<Test__c> test = new List<Test__c>();
            for( Test__c  testStep : [SELECT Expected_Result__c,Step_Description__c,Step_Number__c FROM  Test__c  WHERE  Test__c =:currentRecord.id])
            {
                 Test__c newTestStep = testStep.clone(false);
                 newTestStep.Test__c = newRecord.id;
                 Test__c.add(newTestStep);
            }
            insert test_case_step;
            
            //Copy related list - Test Case User - Another sample list
            List<Test_Case_User_Join__c> test_Case_User = new List<Test_Case_User_Join__c>();
            for(Test_Case_User_Join__c testCaseUser : [SELECT Id,Status__c,User__c FROM Test_Case_User_Join__c
  WHERE Test_Case__c =:currentRecord.id])
            {
                Test_Case_User_Join__c newTestCaseUser = testCaseUser.clone(false);
                newTestCaseUser.Test_Case__c = newRecord.id;
                test_Case_User.add(newTestCaseUser);
            }
            insert test_Case_User;
            
        }
        catch(Exception e)
        {
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
        }
        return new PageReference('/'+newRecord.id);
    }

}


Step 3 : Override the clone button

Edit Clone Button

Once you click on the override button, you will see the following screen, select the page you created 
Override the button with the visual force page
That's it!
You should be able to clone the related list with any object. Remember that you will have to write the code to copy the related list in the extension. The example above copies only 2 related list. You will have to repeat the code for all your related lists. 

Authentic Blog, featured by BlogUpp