+ Start a Discussion
karthik Jonnalagaddakarthik Jonnalagadda 

I need to copy a record from One object to another object on button click

Hi,

Anybody suggest me a simple process to copy a record from one custom object to another custom object on button click. 


 
Best Answer chosen by karthik Jonnalagadda
JayantJayant
Three ways - 

1. A custom button with JavaScript.
2. A custom button with a call to an Apex class using apex.sforce.execute(). The method in the class should be marked as a webservice.
3. A custom button and a visualforce page. Visual force page would just have an Action attribute on <apex: page> and a method returning PageReference to the page you want to redirect to and logic to copy/dml on new object. This method will be called from Action on <apex: page>.

3 is the best option from simplicity and peformance perspective.
1 and 2 are also simple but each call would be an API request since JavaScript will connect via API and 2 is a webservice callout :).

All Answers

NagaNaga (Salesforce Developers) 
Hi Karthik,


There is no other way that Apex Code to copy records from one object to other object.
However to simplify or limit the code you can choose different approach i.e. instead of writing all 100+ fields in apex trigger, use Custom setting to store these fields and then use sObject get() and set() methods do the data copy stuff.


As custom setting is like an object you can create 2 custom fields on that and then create records 100+ in the custom setting to populate the field API Names.
For Example:
List type custom setting: Copy Values (Copy_Values__c)
Custom Fields:
Source Field Name (Source_Field_Name)
Dest Field Name (Dest_Field_Name)

Then you have to click on manage button and insert record to this custom setting. lets say you have fields as Source_A__c, Source_B__c etc and have to copy to Dest_A__c, Dest_B__c etc.
then you have to create records in custom setting.. one for each pair.

Once you done with this configuration then you have to write code in your trigger to create map of source field name to destination field name.

Please let me know if this helps

Best Regards
Naga Kiran
Ashok0572Ashok0572
Hi Karthik,
Cal me @7829250877.
JayantJayant
Three ways - 

1. A custom button with JavaScript.
2. A custom button with a call to an Apex class using apex.sforce.execute(). The method in the class should be marked as a webservice.
3. A custom button and a visualforce page. Visual force page would just have an Action attribute on <apex: page> and a method returning PageReference to the page you want to redirect to and logic to copy/dml on new object. This method will be called from Action on <apex: page>.

3 is the best option from simplicity and peformance perspective.
1 and 2 are also simple but each call would be an API request since JavaScript will connect via API and 2 is a webservice callout :).
This was selected as the best answer
JayantJayant
If this is resolved, please do mark the question as Resolved and the most appropriate/helpful answer as the best answer :).
If none of the answers helped you significantly, please post the solution. You may also mark your solution as the best answer.