Posts

Showing posts from February 6, 2019

Are objects from For Loops passed by reference or value?

Image
1 If I have a for loop that loops through results from a database query, is the single record a reference or copied value? The reason I ask is because I am attempting to update these records, but would it be better to update the whole array, or each one individually? For example, which option is the better way to update multiple objects: // Option 1 for (Object__c obj: objects) { obj.name = "new_name"; update obj; } // Option 2 (possibility depending if "obj" is referenced or not) for (Object__c obj: objects) { obj.name = "new_name"; } update objects; // Option 3 (same as Option 2, but different access method) for (Integer i = 0; i < objects.size(); i++) { objects[I] = "new_name"; } update objects; I guess, basically what I'm as