You need to sign in to do that
Don't have an account?
Syed 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.
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:
Please advise what is the difference between these 2 approaches.
Are they equally same as results are being achieved by both code.
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.
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. If you find your Solution than mark as this as a best answer.
Thanks and Regards
Suraj Tripathi.
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
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.
Thanks for your response. Please advise why the first code will not be a good choice for bulk records.
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.