function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nihar SharmaNihar Sharma 

Clone record with its related list by clicking on custom button on that record

Hi,
i have created one button on record top beside the clone button,
when clicked, execute code that would make a new version record with the same name and append today's date on the end of the name to distinguish it from the original.

How should i do this ?

Thank you very much in advance
Best Answer chosen by Nihar Sharma
KaranrajKaranraj
Using javascript button and sebservice class you can able to do that functioanlity, following are steps involved
  • Create global webservice apex class
  • Call your apex class from the javascript button
Step1:
Below is the sample apex class, which will clone the Account record along with its related contact record.
If you want other related object similar to Contact modify the apex code, you have the sample for Contact, easily you can add other different object
 
global class customClone
{
    webservice static void cloneAccount(Id acctId) // you can pass parameters
    { 
       List<Contact> cont = new List<Contact>();
       Account acc = [SELECT ID, Name FROM Account WHERE Id = : acctId];
       Account accCopy = acc.clone(false,true);
       accCopy.Name = acc.Name +'-'+system.today();
       insert accCopy;
       //cloning Related contact Records
       for(Contact c : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId = : acc.Id]){
         Contact conCopy = c.clone(false,true);
         conCopy.AccountId = accCopy.Id;
         cont.add(conCopy);
        }
       insert cont; 
    }
}

Step 2:
Now create a java script button to call the apex class
  • Goto --> Setup --> Object --> Buttons, links and Actions section
  • Click New Button or Link
  • Enter the Name of the button
  • Behaviour : Execute Javascript
  • Content source : On-Click Javascript
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("customClone","cloneAccount",{acctId:{!Account.Id}}"});

 

All Answers

KaranrajKaranraj
Using javascript button and sebservice class you can able to do that functioanlity, following are steps involved
  • Create global webservice apex class
  • Call your apex class from the javascript button
Step1:
Below is the sample apex class, which will clone the Account record along with its related contact record.
If you want other related object similar to Contact modify the apex code, you have the sample for Contact, easily you can add other different object
 
global class customClone
{
    webservice static void cloneAccount(Id acctId) // you can pass parameters
    { 
       List<Contact> cont = new List<Contact>();
       Account acc = [SELECT ID, Name FROM Account WHERE Id = : acctId];
       Account accCopy = acc.clone(false,true);
       accCopy.Name = acc.Name +'-'+system.today();
       insert accCopy;
       //cloning Related contact Records
       for(Contact c : [SELECT Id, LastName, AccountId FROM Contact WHERE AccountId = : acc.Id]){
         Contact conCopy = c.clone(false,true);
         conCopy.AccountId = accCopy.Id;
         cont.add(conCopy);
        }
       insert cont; 
    }
}

Step 2:
Now create a java script button to call the apex class
  • Goto --> Setup --> Object --> Buttons, links and Actions section
  • Click New Button or Link
  • Enter the Name of the button
  • Behaviour : Execute Javascript
  • Content source : On-Click Javascript
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("customClone","cloneAccount",{acctId:{!Account.Id}}"});

 
This was selected as the best answer
Nirmal9114Nirmal9114
the error message is displayed using this code

"A problem with the OnClick JavaScript for this button or link was encountered
expected }" 


i tried all the thing but it still comes.please help