2

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.

  • 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 Apr 18 at 13:54
6

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.