So currently, I was curious on what is the best way to find out how many records have been modified after being queried.
So far, the only way I have found to do it is the following:
List<sObject> exampleObjectList = [SELECT Id, FieldToUpdate FROM Example__c];
Set<sObject> clonedSet = new Set<sObject>();
for(sObject example : exampleObjectList)
clonedSet.Add(example.clone(true, true, true, true));
//Modify records in exampleObjectList
clonedSet .RemoveAll(exampleObjectList);
//Gives you the amount of records that have been modified in that list
Integer sObjectsModifiedCount = clonedSet.size();
I am just curious if there is a more efficient way to accomplish this. What are the performance drawbacks to this if I were to add this in a trigger or something (mainly to have a count of records modified by a particular trigger class). Any thoughts?
Code Update
I was looking over some documentation for Lists and Sets to find out if I can do this in less steps. This led me to the following correction:
List<sObject> exampleObjectList = [SELECT Id, FieldToUpdate FROM Example__c];
Set<sObject> clonedSet = new Set<sObject>(exampleObjectList.deepClone(true, true, true));
//Perform operations on exampleObjectList
clonedSet.RemoveAll(exampleObjectList);
Integer sObjectsModifiedCount = clonedSet.size();