I have a custom object called 'myObject__c'. This object has a lookup with Account. Now I want to get the values of an existing record (e.g. recordA) and update another record (e.g. recordB).

this is my code:

List<myObject__c> abc = new List<myObject__c>();
List<myObject__c> record1 = [SELECT field1, field2 FROM myObject__c WHERE AccountID = 1];
List<myObject__c> record2 = [SELECT field1, field2 FROM myObject__c WHERE AccountID = 2];
for (myObject__c oldrecord : record1) {
     for(myObject__c newrecord : record2) {
          newrecord.field1 = oldrecord.field1;
          newrecord.field2 = oldrecord.field2;
update newrecord;

when I try to save the file, it gives me an error:

Variable does not exist: newrecord // last line

  • record1 is a List, not a single SObject. Are you expecting exactly one result in each query? It's not clear what behavior is desired here.
    – Adrian Larson
    Aug 18, 2021 at 15:02
  • yes one record but i want to update many fields
    – MShal
    Aug 18, 2021 at 15:03

1 Answer 1


A List<MyObject__c> does not have properties of the fields on those records within. You have to index in to get a single record. Alternatively, if you know you will get exactly one result, just assign to a single record.

MyObject__c record1 = [...];
MyObject__c record2 = [...];
record2.Field__c = record1.Field__c;
  • I updated the question. I will get more than one record that is why i am using list
    – MShal
    Aug 18, 2021 at 15:44
  • So how do the two lists correspond to each other...
    – Adrian Larson
    Aug 18, 2021 at 16:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .