Benefits of using sObject.clone versus creating a new record Announcing the arrival of Valued...
How to convince students of the implication truth values?
What was the first language to use conditional keywords?
Can anything be seen from the center of the Boötes void? How dark would it be?
How do I make this wiring inside cabinet safer?
How could we fake a moon landing now?
Find 108 by using 3,4,6
How come Sam didn't become Lord of Horn Hill?
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
Why is it faster to reheat something than it is to cook it?
How does the math work when buying airline miles?
How to write the following sign?
Why are the trig functions versine, haversine, exsecant, etc, rarely used in modern mathematics?
Multiple OR (||) Conditions in If Statement
Do any jurisdictions seriously consider reclassifying social media websites as publishers?
Why is Nikon 1.4g better when Nikon 1.8g is sharper?
Why aren't air breathing engines used as small first stages?
Did MS DOS itself ever use blinking text?
What is the Characteristic of a local ring?
What's the meaning of "fortified infraction restraint"?
Is CEO the "profession" with the most psychopaths?
How do I find out the mythology and history of my Fortress?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
Benefits of using sObject.clone versus creating a new record
Using et al. for a last / senior author rather than for a first author
Benefits of using sObject.clone versus creating a new record
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election ResultsHow to avoid instantiating object inside a loop?Creating new record with Apex TriggerRestrict on creating new Record by Record TypeDoes deep cloning also create new parent record?Code Coverage to Test Custom Object Public ListExternal id field is not populating when cloning a sobject recordApex: Clone a record, then make the new record the Parent of the old cloned RecordWhat Does My Trigger Do?Create Customize cloning , copying old to new recordVague error message - duplicates value on record with idDetect and mask existing email addresses when creating new record
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Hello I am curious whats the use case of the clone method. Also when its beneficial. Example here I create a lead once then clone it 200 times.
list<lead> leadlist = new list<lead>();
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
for(integer i =0; i<200; i++) {
lead clone = l.clone(false,false,false,false);
leadlist.add(clone);
}
insert leadlist;
Is there anybenefit to doing the above clone instead of?
list<lead> leadlist = new list<lead>();
for(integer i =0; i<200; i++) {
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
leadlist.add(l);
}
insert leadlist;
I came across this in the Queueable Apex Trailhead In the challenge section where they ask you to add the same primary contact to a bunch of accounts.
apex sobject clone
add a comment |
Hello I am curious whats the use case of the clone method. Also when its beneficial. Example here I create a lead once then clone it 200 times.
list<lead> leadlist = new list<lead>();
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
for(integer i =0; i<200; i++) {
lead clone = l.clone(false,false,false,false);
leadlist.add(clone);
}
insert leadlist;
Is there anybenefit to doing the above clone instead of?
list<lead> leadlist = new list<lead>();
for(integer i =0; i<200; i++) {
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
leadlist.add(l);
}
insert leadlist;
I came across this in the Queueable Apex Trailhead In the challenge section where they ask you to add the same primary contact to a bunch of accounts.
apex sobject clone
1
You use clone to copy record information from existing records, in the trailhead i think they are asking you to use clone since they are asking you to insert the same contact for each account for a specific state which means the information has to be same on contact.Just the billing state changes
– RedDevil
4 hours ago
add a comment |
Hello I am curious whats the use case of the clone method. Also when its beneficial. Example here I create a lead once then clone it 200 times.
list<lead> leadlist = new list<lead>();
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
for(integer i =0; i<200; i++) {
lead clone = l.clone(false,false,false,false);
leadlist.add(clone);
}
insert leadlist;
Is there anybenefit to doing the above clone instead of?
list<lead> leadlist = new list<lead>();
for(integer i =0; i<200; i++) {
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
leadlist.add(l);
}
insert leadlist;
I came across this in the Queueable Apex Trailhead In the challenge section where they ask you to add the same primary contact to a bunch of accounts.
apex sobject clone
Hello I am curious whats the use case of the clone method. Also when its beneficial. Example here I create a lead once then clone it 200 times.
list<lead> leadlist = new list<lead>();
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
for(integer i =0; i<200; i++) {
lead clone = l.clone(false,false,false,false);
leadlist.add(clone);
}
insert leadlist;
Is there anybenefit to doing the above clone instead of?
list<lead> leadlist = new list<lead>();
for(integer i =0; i<200; i++) {
lead l = new lead(firstname = 'merp', lastname ='derp', company='CNN');
leadlist.add(l);
}
insert leadlist;
I came across this in the Queueable Apex Trailhead In the challenge section where they ask you to add the same primary contact to a bunch of accounts.
apex sobject clone
apex sobject clone
asked 4 hours ago
Ryan SherryRyan Sherry
676
676
1
You use clone to copy record information from existing records, in the trailhead i think they are asking you to use clone since they are asking you to insert the same contact for each account for a specific state which means the information has to be same on contact.Just the billing state changes
– RedDevil
4 hours ago
add a comment |
1
You use clone to copy record information from existing records, in the trailhead i think they are asking you to use clone since they are asking you to insert the same contact for each account for a specific state which means the information has to be same on contact.Just the billing state changes
– RedDevil
4 hours ago
1
1
You use clone to copy record information from existing records, in the trailhead i think they are asking you to use clone since they are asking you to insert the same contact for each account for a specific state which means the information has to be same on contact.Just the billing state changes
– RedDevil
4 hours ago
You use clone to copy record information from existing records, in the trailhead i think they are asking you to use clone since they are asking you to insert the same contact for each account for a specific state which means the information has to be same on contact.Just the billing state changes
– RedDevil
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
There was a question a while back that can provide some context here: How to avoid instantiating object inside a loop?
To summarize:
- Cloning is very fast
- Using the SObject constructor to set name-value pairs is also very fast, and faster than cloning if you need to change even a single field on a cloned record (e.g.
newRec = record.clone(); newRec.Field = value;
) - Both approaches will "break the reference" so that you have multiple in-memory instances of your target record (as opposed to a single in-memory instance that gets shared, which means changes to any of the "referenced" copies affects all of the referenced copies)
- But most importantly, the performance difference between the two is negligible. Don't waste your time on micro-optimizations like this
Depending on how many fields the object you're trying to duplicate has (and how many fields you need to change between instances), cloning can result in less typing.
Honestly, there's not much benefit to one over the other. The greatest benefit here will probably come from choosing the right tool for the job (e.g. clone() is good when you need to do some processing or make changes to a record but still need the original values to remain intact for something else) and being consistent across your code base.
add a comment |
If you create a record multiple times without cloning then all the record relationships are disjoint. So, changing an object will not change other objects.
If you clone like the above way then that is known as Deep cloning. So, All fields on the SObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned SObject, the original SObject is not affected.
Whereas for Shallow cloning, if you make changes to a relationship field on the cloned SObject, the corresponding field on the original SObject is also affected, and vice versa
So, cloning is faster than creating new records without cloning. Moreover Shallow cloning is faster than deep cloning.
Use Case of cloning could be: customer has purchased a property and business wants to create payment schedule, EMI information for a number of months.
In this scenario, most of the information will be same only month for Payment will be different. So, better to use clone functionality to create records.
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%2f258331%2fbenefits-of-using-sobject-clone-versus-creating-a-new-record%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
There was a question a while back that can provide some context here: How to avoid instantiating object inside a loop?
To summarize:
- Cloning is very fast
- Using the SObject constructor to set name-value pairs is also very fast, and faster than cloning if you need to change even a single field on a cloned record (e.g.
newRec = record.clone(); newRec.Field = value;
) - Both approaches will "break the reference" so that you have multiple in-memory instances of your target record (as opposed to a single in-memory instance that gets shared, which means changes to any of the "referenced" copies affects all of the referenced copies)
- But most importantly, the performance difference between the two is negligible. Don't waste your time on micro-optimizations like this
Depending on how many fields the object you're trying to duplicate has (and how many fields you need to change between instances), cloning can result in less typing.
Honestly, there's not much benefit to one over the other. The greatest benefit here will probably come from choosing the right tool for the job (e.g. clone() is good when you need to do some processing or make changes to a record but still need the original values to remain intact for something else) and being consistent across your code base.
add a comment |
There was a question a while back that can provide some context here: How to avoid instantiating object inside a loop?
To summarize:
- Cloning is very fast
- Using the SObject constructor to set name-value pairs is also very fast, and faster than cloning if you need to change even a single field on a cloned record (e.g.
newRec = record.clone(); newRec.Field = value;
) - Both approaches will "break the reference" so that you have multiple in-memory instances of your target record (as opposed to a single in-memory instance that gets shared, which means changes to any of the "referenced" copies affects all of the referenced copies)
- But most importantly, the performance difference between the two is negligible. Don't waste your time on micro-optimizations like this
Depending on how many fields the object you're trying to duplicate has (and how many fields you need to change between instances), cloning can result in less typing.
Honestly, there's not much benefit to one over the other. The greatest benefit here will probably come from choosing the right tool for the job (e.g. clone() is good when you need to do some processing or make changes to a record but still need the original values to remain intact for something else) and being consistent across your code base.
add a comment |
There was a question a while back that can provide some context here: How to avoid instantiating object inside a loop?
To summarize:
- Cloning is very fast
- Using the SObject constructor to set name-value pairs is also very fast, and faster than cloning if you need to change even a single field on a cloned record (e.g.
newRec = record.clone(); newRec.Field = value;
) - Both approaches will "break the reference" so that you have multiple in-memory instances of your target record (as opposed to a single in-memory instance that gets shared, which means changes to any of the "referenced" copies affects all of the referenced copies)
- But most importantly, the performance difference between the two is negligible. Don't waste your time on micro-optimizations like this
Depending on how many fields the object you're trying to duplicate has (and how many fields you need to change between instances), cloning can result in less typing.
Honestly, there's not much benefit to one over the other. The greatest benefit here will probably come from choosing the right tool for the job (e.g. clone() is good when you need to do some processing or make changes to a record but still need the original values to remain intact for something else) and being consistent across your code base.
There was a question a while back that can provide some context here: How to avoid instantiating object inside a loop?
To summarize:
- Cloning is very fast
- Using the SObject constructor to set name-value pairs is also very fast, and faster than cloning if you need to change even a single field on a cloned record (e.g.
newRec = record.clone(); newRec.Field = value;
) - Both approaches will "break the reference" so that you have multiple in-memory instances of your target record (as opposed to a single in-memory instance that gets shared, which means changes to any of the "referenced" copies affects all of the referenced copies)
- But most importantly, the performance difference between the two is negligible. Don't waste your time on micro-optimizations like this
Depending on how many fields the object you're trying to duplicate has (and how many fields you need to change between instances), cloning can result in less typing.
Honestly, there's not much benefit to one over the other. The greatest benefit here will probably come from choosing the right tool for the job (e.g. clone() is good when you need to do some processing or make changes to a record but still need the original values to remain intact for something else) and being consistent across your code base.
answered 3 hours ago
Derek FDerek F
21.1k52353
21.1k52353
add a comment |
add a comment |
If you create a record multiple times without cloning then all the record relationships are disjoint. So, changing an object will not change other objects.
If you clone like the above way then that is known as Deep cloning. So, All fields on the SObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned SObject, the original SObject is not affected.
Whereas for Shallow cloning, if you make changes to a relationship field on the cloned SObject, the corresponding field on the original SObject is also affected, and vice versa
So, cloning is faster than creating new records without cloning. Moreover Shallow cloning is faster than deep cloning.
Use Case of cloning could be: customer has purchased a property and business wants to create payment schedule, EMI information for a number of months.
In this scenario, most of the information will be same only month for Payment will be different. So, better to use clone functionality to create records.
add a comment |
If you create a record multiple times without cloning then all the record relationships are disjoint. So, changing an object will not change other objects.
If you clone like the above way then that is known as Deep cloning. So, All fields on the SObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned SObject, the original SObject is not affected.
Whereas for Shallow cloning, if you make changes to a relationship field on the cloned SObject, the corresponding field on the original SObject is also affected, and vice versa
So, cloning is faster than creating new records without cloning. Moreover Shallow cloning is faster than deep cloning.
Use Case of cloning could be: customer has purchased a property and business wants to create payment schedule, EMI information for a number of months.
In this scenario, most of the information will be same only month for Payment will be different. So, better to use clone functionality to create records.
add a comment |
If you create a record multiple times without cloning then all the record relationships are disjoint. So, changing an object will not change other objects.
If you clone like the above way then that is known as Deep cloning. So, All fields on the SObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned SObject, the original SObject is not affected.
Whereas for Shallow cloning, if you make changes to a relationship field on the cloned SObject, the corresponding field on the original SObject is also affected, and vice versa
So, cloning is faster than creating new records without cloning. Moreover Shallow cloning is faster than deep cloning.
Use Case of cloning could be: customer has purchased a property and business wants to create payment schedule, EMI information for a number of months.
In this scenario, most of the information will be same only month for Payment will be different. So, better to use clone functionality to create records.
If you create a record multiple times without cloning then all the record relationships are disjoint. So, changing an object will not change other objects.
If you clone like the above way then that is known as Deep cloning. So, All fields on the SObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned SObject, the original SObject is not affected.
Whereas for Shallow cloning, if you make changes to a relationship field on the cloned SObject, the corresponding field on the original SObject is also affected, and vice versa
So, cloning is faster than creating new records without cloning. Moreover Shallow cloning is faster than deep cloning.
Use Case of cloning could be: customer has purchased a property and business wants to create payment schedule, EMI information for a number of months.
In this scenario, most of the information will be same only month for Payment will be different. So, better to use clone functionality to create records.
answered 3 hours ago
Santanu BoralSantanu Boral
31.4k52456
31.4k52456
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%2f258331%2fbenefits-of-using-sobject-clone-versus-creating-a-new-record%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
1
You use clone to copy record information from existing records, in the trailhead i think they are asking you to use clone since they are asking you to insert the same contact for each account for a specific state which means the information has to be same on contact.Just the billing state changes
– RedDevil
4 hours ago