+ Start a Discussion
Madhu007Madhu007 

Updating one record values to another record

Hi,

I have a record with more than 100 fields. I want to copy all field values into another existing record and update. Is there any method to update?

Thanks.
 
Best Answer chosen by Madhu007
Andreas MeyerAndreas Meyer
Hi Mad,

get the first record (with fields to copy), get the second record (target) and set the Id of the first record object to second record object and update. E.g. for accounts:
 
Account souce_acc = new Account();
Account dest_acc = new Account();

source_acc = [SELECT ...];
dest_acc = [SELECT ...];

source_acc.Id = dest_acc.Id;
update source_acc;

Best,
Andreas




 

All Answers

viruSviruS
Hi Madhu,

Check here http://www.oyecode.com/2012/08/utility-class-how-to-get-all-fields-for.html
Andreas MeyerAndreas Meyer
Hi Mad,

get the first record (with fields to copy), get the second record (target) and set the Id of the first record object to second record object and update. E.g. for accounts:
 
Account souce_acc = new Account();
Account dest_acc = new Account();

source_acc = [SELECT ...];
dest_acc = [SELECT ...];

source_acc.Id = dest_acc.Id;
update source_acc;

Best,
Andreas




 
This was selected as the best answer
Madhu007Madhu007
Hi All, Thanks for the reply. I did mistake here. I have two records with same object.This object is having more than 100 fields. I need to update all field values of one record to another record.

Thanks,
Madhu
viruSviruS
If you have still issues connect me on skype virendrasinghnaruka
Harpreet On CloudHarpreet On Cloud
Use the link referred to by [viruS] viruS (http://www.oyecode.com/2012/08/utility-class-how-to-get-all-fields-for.html) you will get list of all field names as String. You can then user the following way.
Your_Custom_Object__c sourceRecord, destRecord;
Map < String, Schema.SObjectField > fields = r.fields.getMap();
for (string field: fields.keySet()) {
    destRecord.put(field, sourceRecord.get(field1);
}
update destRecord;