I want to implement a functionality to copy specific custom object in Force.com.

I was reviewing some ideas from Success Salesforce.com forum but I need more digestible thing than that:

This is my controller method to cp this object:

private Support_Order__c CopySO(){
    Support_Order__c SupportOrderCopied = new Support_Order__c();
    SupportOrderCopied.Support_Order_Owner__c = oldSO.Support_Order_Owner__c;
    SupportOrderCopied.RecordType = oldSO.RecordType;
    //SupportOrderCopied.Account_Name__c = oldSO.Account_Name__c;
    SupportOrderCopied.Contracted_Account_Name__c = oldSO.Contracted_Account_Name__c;
    SupportOrderCopied.Opportunity__c = oldSO.Opportunity__c;
    SupportOrderCopied.Support_Order_Type__c =  oldSO.Support_Order_Type__c;
    SupportOrderCopied.Current_Term_Start_Date__c = oldSO.Current_Term_Start_Date__c;
    SupportOrderCopied.Current_Term_End_Date__c = oldSO.Current_Term_End_Date__c;
    SupportOrderCopied.Extended_Support_Date__c = oldSO.Extended_Support_Date__c;
    SupportOrderCopied.Agreement_End_Date__c = oldSO.Agreement_End_Date__c;
    SupportOrderCopied.Client_Notice_Date__c = oldSO.Client_Notice_Date__c;
    SupportOrderCopied.Reason_for_Risk__c = oldSO.Reason_for_Risk__c;
    SupportOrderCopied.Cloned_From__c = oldSO.Cloned_From__c;
    //SupportOrderCopied.Copied_To__c = oldSO.Copied_To__c;
    SupportOrderCopied.Consolidated_Support_Order__c = oldSO.Consolidated_Support_Order__c;
    //SupportOrderCopied.Original_Support_End_Date__c = oldSO.Original_Support_End_Date__c;
    SupportOrderCopied.Do_Not_Consolidate__c = oldSO.Do_Not_Consolidate__c;
    //SupportOrderCopied.Opportunity_Owner__c = oldSO.Opportunity_Owner__c;
    SupportOrderCopied.Status__c = oldSO.Status__c;
    SupportOrderCopied.Sub_Status__c = oldSO.Sub_Status__c;
    SupportOrderCopied.Contract_Date__c = oldSO.Contract_Date__c;
    SupportOrderCopied.Auto_Renewal__c = oldSO.Auto_Renewal__c;
    SupportOrderCopied.Owner_Expiration_Notice__c = oldSO.Owner_Expiration_Notice__c; // Renewal Trigger days
    //SupportOrderCopied.Renewal_Dated__c = oldSO.Renewal_Dated__c;
    //SupportOrderCopied.Renewal_Month_Num__c = oldSO.Renewal_Month_Num__c;
    SupportOrderCopied.Cancellation_Date__c = oldSO.Cancellation_Date__c;
    SupportOrderCopied.Cancellation_Effective_Date__c = oldSO.Cancellation_Effective_Date__c;
    SupportOrderCopied.Customer_Region__c = oldSO.Customer_Region__c;
    SupportOrderCopied.Default_Escalation_Index__c = oldSO.Default_Escalation_Index__c;
    SupportOrderCopied.Default_Escalation_CAP__c = oldSO.Default_Escalation_CAP__c;
    SupportOrderCopied.Escalation_Floor__c = oldSO.Escalation_Floor__c;
    SupportOrderCopied.Default_Escalation_Effective_Date__c = oldSO.Default_Escalation_Effective_Date__c;
    SupportOrderCopied.Default_Escalation_P__c = oldSO.Default_Escalation_P__c;
    SupportOrderCopied.I_Agree__c = oldSO.I_Agree__c; // Calculate Escalation
    //SupportOrderCopied.Annual_Support_Fees__c = oldSO.Annual_Support_Fees__c;//data child object comes from the Support Order
    SupportOrderCopied.Cancellation_Policy__c = oldSO.Cancellation_Policy__c;
    SupportOrderCopied.Payment_Terms__c = oldSO.Payment_Terms__c;
    SupportOrderCopied.Billing_Status__c = oldSO.Billing_Status__c;
    SupportOrderCopied.PO_Notice__c = oldSO.PO_Notice__c;
    SupportOrderCopied.Quote_Notice__c = oldSO.Quote_Notice__c;
    //SupportOrderCopied.Clone_Date__c = oldSO.Clone_Date__c;
    insert SupportOrderCopied;
    return SupportOrderCopied;

My question is how to display by the edit mode this object in salesforce.com using Visualforce page after cp functionality is calling through the custom object. I have a piece of visual code following:

<apex:page standardController="Support_Order__c">
  <apex:detail subject="{!getSupportOrder.Id}" mode="edit"/>

Please any idea will be very useful.


  • Just use the standard clone button. – Adrian Larson May 23 '16 at 14:24
  • Thanks @AdrianLarson quick answer. My copy is custom func. It is able to copy specific custom fields. wsdl and web service can be an option. – kitsunebrave May 23 '16 at 15:52
  • So did you create a custom button and add it to the layout? – Adrian Larson May 23 '16 at 15:54
  • Yes @AdrianLarson I did. But now I need to associate my sObject with my apex method through ajax. I am getting undefined object from execute("myClass","myMtd",{object}). – kitsunebrave May 23 '16 at 18:30
  • why in the world would you use ajax? Make it a Visualforce button. It's a much more straightforward design. – Adrian Larson May 23 '16 at 18:35

Your custom object should have a standard Clone button available. I recommend using that instead of rolling a custom Visualforce implementation.

However, if you want to use a Visualforce Page as a Custom Button, you need to first create the button, then modify your Page Layout to include it.

  1. To create the button, go to Customize > Cases > Buttons, Links, and Actions.
  2. Click New Button or Link.
  3. Populate Label > Clone.
  4. Select Display Type > Detail Page Button
  5. Select Content Source > Visualforce Page.
  6. Select your page.
  7. Click Save

Remember to add the button to your Page Layout once you are done.

Another note, even if you insist on sticking with Apex, you can still use the SObject.clone method. (See sObject class):

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

Support_Order__c clone = oldSO.clone();
insert clone;
return clone;

To copy a Custom Object first thing to do is create the custom Detail Page Button in Javascript code:


var customSO = new sforce.SObject("Support_Order__c");
var soId = '{!Support_Order__c.Id}';

var result = sforce.apex.execute("SO_AutoCopy_WSController","CopySO",  {oldSO:soId});

if('{!Support_Order__c.IsAlreadyCloned__c}' == true) {
  alert('Already Copied');
  sforce.debug.trace = true;
  alert('Support Order has been copied successfully');
  window.location.href = '/'+result;

then create a internal wsdl script in apex code to call this functionality externally from your s-control.

global with sharing class SO_AutoCopy_WSController {//using WSDL Class to store the Copied Support Order 

public static Support_Order__c oldSOToCopy{get; set;}
public SO_AutoCopy_WSController(ApexPages.StandardController controller){
    oldSOToCopy = new Support_Order__c();

//Support Order Copy Functionality    
webservice static String CopySO(String oldSO){
    oldSOToCopy = [SELECT 
            Id, Agreement_End_Date__c,Auto_Renewal__c,
            FROM Support_Order__c WHERE Id =: oldSO LIMIT 1];   
    Support_Order__c SupportOrderCopied = new Support_Order__c();
    SupportOrderCopied.IsAlreadyCloned__c = true;
    SupportOrderCopied.Support_Order_Owner__c = oldSOToCopy.Support_Order_Owner__c;
    SupportOrderCopied.RecordTypeId = oldSOToCopy.RecordTypeId;
    SupportOrderCopied.Contract__c = oldSOToCopy.Contract__c;
    //SupportOrderCopied.Account_Name__c = oldSO.Account_Name__c;
    SupportOrderCopied.Contracted_Account_Name__c = oldSOToCopy.Contracted_Account_Name__c;
        insert SupportOrderCopied;
        return SupportOrderCopied.Id;
    }catch(DmlException e){
        return e.getMessage();

Just to highlight every time calling from s-control or OnClick Javascript you have to recreate the properly structure for your goals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.