Are objects from For Loops passed by reference or value?












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 asking is if Option 2 and Option 3 do the same thing? Or is the better practice option 1?










share|improve this question























  • @Himanshu just a friendly reminder, comments are for clarification of the question, not answers.

    – sfdcfox
    Jan 29 at 20:47
















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 asking is if Option 2 and Option 3 do the same thing? Or is the better practice option 1?










share|improve this question























  • @Himanshu just a friendly reminder, comments are for clarification of the question, not answers.

    – sfdcfox
    Jan 29 at 20:47














1












1








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 asking is if Option 2 and Option 3 do the same thing? Or is the better practice option 1?










share|improve this question














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 asking is if Option 2 and Option 3 do the same thing? Or is the better practice option 1?







update loop reference






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 29 at 20:32









BlondeSwanBlondeSwan

30910




30910













  • @Himanshu just a friendly reminder, comments are for clarification of the question, not answers.

    – sfdcfox
    Jan 29 at 20:47



















  • @Himanshu just a friendly reminder, comments are for clarification of the question, not answers.

    – sfdcfox
    Jan 29 at 20:47

















@Himanshu just a friendly reminder, comments are for clarification of the question, not answers.

– sfdcfox
Jan 29 at 20:47





@Himanshu just a friendly reminder, comments are for clarification of the question, not answers.

– sfdcfox
Jan 29 at 20:47










2 Answers
2






active

oldest

votes


















3














Objects are passed by reference.



Option 1 is Bad because it is un-bulkified. That pattern will rapidly chew through your slim allotment of 150 DML statements per transaction, yielding a LimitException.



Options 2 and 3 are largely equivalent, with 2 being more idiomatic. The integer loop variable is extraneous unless you have some other need to have access to the loop index.



Both 2 and 3 are effectively bulkified, meaning your code will be more performant, all else being equal, and will have much lower limits risk. It's important to note that limits still apply to bulkified code, but you'll be consuming them at a much more sustainable rate. You'll use only one DML statement in these options, although you'll still use N DML rows for N records - but the row limit is 10,000, rather than 150!



(Option 3 is missing the .Name, but I don't think that's material to your example).






share|improve this answer































    2














    We can only do 150 DML statements in 1 transaction. If you are trying to insert 1000 records using option 1, The code will break with LIMITS Exception. Check DML limits . Doing DML and SOQL in for loops is RED FLAG. Very bad in SF environment.



    Option 2 and 3 are better than option 1.



    Performance wise option 2 will be better as you are not manually accessing array via Index and letting salesforce do that job for you.






    share|improve this answer
























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "459"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f248396%2fare-objects-from-for-loops-passed-by-reference-or-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Objects are passed by reference.



      Option 1 is Bad because it is un-bulkified. That pattern will rapidly chew through your slim allotment of 150 DML statements per transaction, yielding a LimitException.



      Options 2 and 3 are largely equivalent, with 2 being more idiomatic. The integer loop variable is extraneous unless you have some other need to have access to the loop index.



      Both 2 and 3 are effectively bulkified, meaning your code will be more performant, all else being equal, and will have much lower limits risk. It's important to note that limits still apply to bulkified code, but you'll be consuming them at a much more sustainable rate. You'll use only one DML statement in these options, although you'll still use N DML rows for N records - but the row limit is 10,000, rather than 150!



      (Option 3 is missing the .Name, but I don't think that's material to your example).






      share|improve this answer




























        3














        Objects are passed by reference.



        Option 1 is Bad because it is un-bulkified. That pattern will rapidly chew through your slim allotment of 150 DML statements per transaction, yielding a LimitException.



        Options 2 and 3 are largely equivalent, with 2 being more idiomatic. The integer loop variable is extraneous unless you have some other need to have access to the loop index.



        Both 2 and 3 are effectively bulkified, meaning your code will be more performant, all else being equal, and will have much lower limits risk. It's important to note that limits still apply to bulkified code, but you'll be consuming them at a much more sustainable rate. You'll use only one DML statement in these options, although you'll still use N DML rows for N records - but the row limit is 10,000, rather than 150!



        (Option 3 is missing the .Name, but I don't think that's material to your example).






        share|improve this answer


























          3












          3








          3







          Objects are passed by reference.



          Option 1 is Bad because it is un-bulkified. That pattern will rapidly chew through your slim allotment of 150 DML statements per transaction, yielding a LimitException.



          Options 2 and 3 are largely equivalent, with 2 being more idiomatic. The integer loop variable is extraneous unless you have some other need to have access to the loop index.



          Both 2 and 3 are effectively bulkified, meaning your code will be more performant, all else being equal, and will have much lower limits risk. It's important to note that limits still apply to bulkified code, but you'll be consuming them at a much more sustainable rate. You'll use only one DML statement in these options, although you'll still use N DML rows for N records - but the row limit is 10,000, rather than 150!



          (Option 3 is missing the .Name, but I don't think that's material to your example).






          share|improve this answer













          Objects are passed by reference.



          Option 1 is Bad because it is un-bulkified. That pattern will rapidly chew through your slim allotment of 150 DML statements per transaction, yielding a LimitException.



          Options 2 and 3 are largely equivalent, with 2 being more idiomatic. The integer loop variable is extraneous unless you have some other need to have access to the loop index.



          Both 2 and 3 are effectively bulkified, meaning your code will be more performant, all else being equal, and will have much lower limits risk. It's important to note that limits still apply to bulkified code, but you'll be consuming them at a much more sustainable rate. You'll use only one DML statement in these options, although you'll still use N DML rows for N records - but the row limit is 10,000, rather than 150!



          (Option 3 is missing the .Name, but I don't think that's material to your example).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 29 at 20:40









          David ReedDavid Reed

          38.7k82356




          38.7k82356

























              2














              We can only do 150 DML statements in 1 transaction. If you are trying to insert 1000 records using option 1, The code will break with LIMITS Exception. Check DML limits . Doing DML and SOQL in for loops is RED FLAG. Very bad in SF environment.



              Option 2 and 3 are better than option 1.



              Performance wise option 2 will be better as you are not manually accessing array via Index and letting salesforce do that job for you.






              share|improve this answer




























                2














                We can only do 150 DML statements in 1 transaction. If you are trying to insert 1000 records using option 1, The code will break with LIMITS Exception. Check DML limits . Doing DML and SOQL in for loops is RED FLAG. Very bad in SF environment.



                Option 2 and 3 are better than option 1.



                Performance wise option 2 will be better as you are not manually accessing array via Index and letting salesforce do that job for you.






                share|improve this answer


























                  2












                  2








                  2







                  We can only do 150 DML statements in 1 transaction. If you are trying to insert 1000 records using option 1, The code will break with LIMITS Exception. Check DML limits . Doing DML and SOQL in for loops is RED FLAG. Very bad in SF environment.



                  Option 2 and 3 are better than option 1.



                  Performance wise option 2 will be better as you are not manually accessing array via Index and letting salesforce do that job for you.






                  share|improve this answer













                  We can only do 150 DML statements in 1 transaction. If you are trying to insert 1000 records using option 1, The code will break with LIMITS Exception. Check DML limits . Doing DML and SOQL in for loops is RED FLAG. Very bad in SF environment.



                  Option 2 and 3 are better than option 1.



                  Performance wise option 2 will be better as you are not manually accessing array via Index and letting salesforce do that job for you.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 29 at 20:40









                  Pranay JaiswalPranay Jaiswal

                  18.4k43058




                  18.4k43058






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Salesforce Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f248396%2fare-objects-from-for-loops-passed-by-reference-or-value%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory