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
Syed F RazaSyed F Raza 

Using 2 Lists Versus One for Updating Records

Hi,
Please refer to the following code where I am querying on a Custom Object STUDENT to return my results where country name is Netherlands. If the result comes back with records, I am updating the country name to Switzerland.
 
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];

for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        }
}

Update sList;
system.debug(sList.size());

This code runs as per expectations and queried records are updated as per the condition set.

However I have noticed that some of the developers would use 2 Lists for such operation where the first list would be used for the SOQL query and in the second list they will add the results of the records where Country field is being updated. The code is as follows:
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];


List<Student__c> sList2 = new List<Student__c>();

for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        sList2.add(std);
    }
}


Update sList2;
system.debug(sList2.size());

Please advise what is the difference between these 2 approaches.
Are they equally same as results are being achieved by both code.

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Syed F Raza,
Kindly Find the solution.
Both Are same for only update operation. First apporoch is for just updating the records but the second approach is to keep old record data for comparison.
//first approch for only update only.
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];
for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        }
}
Update sList;
system.debug(sList.size());

//Second approch is to also keep old data of that record.
List<Student__c> sList = [Select Name, Country__c from Student__c
                         WHERE Country__c ='Netherlands'];
List<Student__c> sList2 = new List<Student__c>();

for(Student__c std:sList){
    if(std.country__c=='Netherlands'){
        std.Country__c='Switzerland';
        sList2.add(std);
    }
}


Update sList2;
system.debug(sList2.size());
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.
PriyaPriya (Salesforce Developers) 
Hi Syed,

You should always go with the second approach. Reason behind is that the code is bulkified. It means when you have lots of record to update, this code will run perfectly but not in case of first approach. 
 

Please mark as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan


 
Syed F RazaSyed F Raza
@ Suraj

thanks for the response. Kindly pleae elaborate the following statement of yours:

Both Are same for only update operation


Also in such operations, what is a good practice. To have one List or 2 Lists.
Syed F RazaSyed F Raza
@Priya

Thanks for your response. Please advise why the first code will not be a good choice for bulk records.
Suraj Tripathi 47Suraj Tripathi 47
@Syed F Raza,
Both will work same for updation purpose ,that mean if you want to update only then both approch is good but some time you need to keep track of old records then go for second approch.
First approch is best according to me beacuse it save memory space of second list.


If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.