You need to sign in to do that
Don't have an account?
Prema -
URGENT Hi All, please help me with copying one object field values to another at one go !
I want to do something like this. How can it be possible , Please let me know :
I don't want to do it like this :
for(Account acc2:[select id from account limit 1]) { //sObject s = new Account(); Vehicle__c vt=new Vehicle__c(); vt=(Sobject)acc2; insert vt; System.debug('sobject '+vt); }
I don't want to do it like this :
for(Account acc2:[select id from account limit 1]) { Vehicle__c vt=new Vehicle__c(); vt=acc2.name; insert vt; System.debug('sobject '+vt); } Here we are assigning the variables one by one. I have 157 fields how can I assign all of them in one go. Please help me this is bit urgent!!.
I have an out of box solution for your problem but for this make sure that field type of all the map fields are same otherwise an error may occur.
The solution is to use the sObject.get() and sObject.put() methods.
Try to use this code. If you encounter any error let me know. If it solves your issue please mark this as best solution.
Thanks and Regards
Ayush Sharma :)
All Answers
If you want to copy the records from Accounts to your custom object(Vehicle__c).
Then query all the fields of Account which fields you want to copy in the custom object but one thing must be remembered for copying is this the type of fields must be the same in both objects(Account and Vehical__c).
Then start the loop for Account and copy one by one field in custom object fields.
Then insert one by one or insert the list by adding records in the list.
For example-
for(Account acc2:[select id,Name,Industry from account limit 1])
{
Vehicle__c vt = new Vehicle__c();
vt.name = acc2.name;
vt.Industry__c = acc2.Industry;
insert vt;
System.debug('sobject '+vt);
}
or
List<Vehicle__c> vt = new List<Vehicle__c>();
for(Account acc2:[select id,Name,Industry from account limit 1])
{
Vehicle__c vtInst = new Vehicle__c();
vtInst.name = acc2.name;
vtInst.Industry__c = acc2.Industry;
vt.add(vtInst);
System.debug('sobject '+vtInst);
}
insert vt;
I recommend the second example of using.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
I have an out of box solution for your problem but for this make sure that field type of all the map fields are same otherwise an error may occur.
The solution is to use the sObject.get() and sObject.put() methods.
Try to use this code. If you encounter any error let me know. If it solves your issue please mark this as best solution.
Thanks and Regards
Ayush Sharma :)