Are objects from For Loops passed by reference or value?
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
add a comment |
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
@Himanshu just a friendly reminder, comments are for clarification of the question, not answers.
– sfdcfox
Jan 29 at 20:47
add a comment |
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
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
update loop reference
asked Jan 29 at 20:32
BlondeSwanBlondeSwan
504
504
@Himanshu just a friendly reminder, comments are for clarification of the question, not answers.
– sfdcfox
Jan 29 at 20:47
add a comment |
@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
add a comment |
2 Answers
2
active
oldest
votes
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).
Okay, that's the answer I was looking for! Thanks
– BlondeSwan
Jan 29 at 21:01
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
Okay, that's the answer I was looking for! Thanks
– BlondeSwan
Jan 29 at 21:01
add a comment |
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).
Okay, that's the answer I was looking for! Thanks
– BlondeSwan
Jan 29 at 21:01
add a comment |
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).
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).
answered Jan 29 at 20:40
David ReedDavid Reed
34.1k72052
34.1k72052
Okay, that's the answer I was looking for! Thanks
– BlondeSwan
Jan 29 at 21:01
add a comment |
Okay, that's the answer I was looking for! Thanks
– BlondeSwan
Jan 29 at 21:01
Okay, that's the answer I was looking for! Thanks
– BlondeSwan
Jan 29 at 21:01
Okay, that's the answer I was looking for! Thanks
– BlondeSwan
Jan 29 at 21:01
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 29 at 20:40
Pranay JaiswalPranay Jaiswal
15.8k32653
15.8k32653
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@Himanshu just a friendly reminder, comments are for clarification of the question, not answers.
– sfdcfox
Jan 29 at 20:47