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
Nirmal9114Nirmal9114 

Clone record with its related list by clicking on custom button

I am facing the error saying "A problem with the OnClick JavaScript for this button or link was encountered''

CLASS:
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; 
    }
}

MY JAVASCRIPT CODE for the custom button

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("customClone","cloneAccount",{acctId:{!Account.Id}}"});




 
Deepak GulianDeepak Gulian

If you do not have Namespace in your Org try this
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("customClone","cloneAccount",{acctId:"{!Account.Id}"});
 

If you have Namespace in your Org try this
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("YourOrgNameSpace.customClone","cloneAccount",{acctId:"{!Account.Id}"});

sachin kadian 5sachin kadian 5
I think you are missing " in last line. try like this 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("customClone","cloneAccount",{acctId:"{!Account.Id}"});
Nirmal9114Nirmal9114
Hi Deepak,
I dont have namespace in my org. so i tried the above code.
Now the clone button is not working itself.
Nirmal9114Nirmal9114
Sachin i tried that also...nothing is working :(
Deepak GulianDeepak Gulian
Are you getting any error?
Nirmal9114Nirmal9114
DEEPAK - No error but the clone button is not working. if a press nothing happens
Deepak GulianDeepak Gulian
Can you please share a screenshot of detail page button with code?
Deepak GulianDeepak Gulian
Change in below query
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 = : acctId]){
         Contact conCopy = c.clone(false,true);
         conCopy.AccountId = accCopy.Id;
         cont.add(conCopy);
        }
       System.debug(cont.size());
       insert cont;
    }
}
Nirmal9114Nirmal9114
i have written the class with i shared it above.
this is what u asked.
User-added image
Nirmal9114Nirmal9114
DEEPAK- Still the button doesnot work..even i am not getting what the error is
Deepak GulianDeepak Gulian
Can you please add an alert message after the sforce.apex.execute()
Nirmal9114Nirmal9114
DEEPAK- I added an error msg. after displaying that error message correctly.
Iit displays this error.
User-added image
Deepak GulianDeepak Gulian
In which browser you are testing it?
Deepak GulianDeepak Gulian
Use 15.0 instead of 30.0
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
Nirmal9114Nirmal9114
DEEPAK- i have checked on chrome and explorer same issue. :(
sachin kadian 5sachin kadian 5
HI Tanya,

I tried the same i posted and its working totally fine for me. 

Did you try to paste the code i gave you?

Thanks,
sachin kadian 5sachin kadian 5
User-added image See i have 3 records in starting, User-added image after pressing that button, it becomes 4
Nirmal9114Nirmal9114
SACHIN - yes i pasted that. i noticed that its happening that the backend.
but if the user want to change anything then they cannot. we have to go back and edit it
sachin kadian 5sachin kadian 5
I am sorry i didnt get what you want to say. is it cloning the account or not?? what you are trying to edit?
Nirmal9114Nirmal9114
SACHIN - yes its about cloning account but when i clone i should be redirecting it to the page where i can add or change data then save it.
u got my point?
sachin kadian 5sachin kadian 5
Tanya if you want to redirect it to new record, then plz try following code -
 
Class - 

global class customClone
{
    webservice static Id 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; 
       return accCopy.Id;
    }
}




button - 

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
var newAccid = sforce.apex.execute("customClone","cloneAccount",{acctId:"{!Account.Id}"});
window.location.href="/"+newAccid;



 
sachin kadian 5sachin kadian 5
and if you want to go to edit page of new account, then replace the last line with this - 

window.location.href="/"+newAccid+'/e';
Nirmal9114Nirmal9114
SACHIN - Thanks for working with me. BUT i am finding out a way in which it will open the cloned account in EDIT mode so that user can edit it their itself and then save it or cancel it on their own.
 
sachin kadian 5sachin kadian 5
Yup open the item in edit mode linke by following line- 


window.location.href="/"+newAccid+'/e';
 
Nirmal9114Nirmal9114
SACHIN - its opening in the edit mode but not populating fields of the previous account
i meant the data its not fetching. why so? can you please tell
sachin kadian 5sachin kadian 5
Put all the fields in query that you want in new record. 
Nirmal9114Nirmal9114
SACHIN - their is one issue with this.
If i cancel then also the record is made and saved.
and another one that its redirecting to HOME page even on cancelling and saving
shruthi javaregowda 9shruthi javaregowda 9
Hi Tanya,

Were u able to fix the record creation upon cancel button too. And how about redirecting issue?

I have similar requirement to be implemented. Can you please let me know the fix.