function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kaushik Sabapathykaushik Sabapathy 

difference between contact c and List<contact>

Hi
Im a new to salesforce. i have some questions regarding single sobject and list of sobject.
in the below code it uses contact cont = c. clone  is it for a single contact ??
And what clone method actually does for the below code
public class AddPrimaryContact implements Queueable
{
    private Contact c;
    private String state;
    public  AddPrimaryContact(Contact c, String state)
    {
        this.c = c;
        this.state = state;
    }
    public void execute(QueueableContext context) 
    {
         List<Account> ListAccount = [SELECT ID, Name ,(Select id,FirstName,LastName from contacts ) FROM ACCOUNT WHERE BillingState = :state LIMIT 200];
         List<Contact> lstContact = new List<Contact>();
         for (Account acc:ListAccount)
         {
                 Contact cont = c.clone(false,false,false,false);
                 cont.AccountId =  acc.id;
                 lstContact.add( cont );
         }
         
         if(lstContact.size() >0 )
         {
             insert lstContact;
         }
             
    }

}
Ekta Gupta 11Ekta Gupta 11
Hi Kaushik,

Your code will work for single contact.
In this code new contact record will be created with totally different sfdc Id .
No related objects will be copied neither autonumber or timesstamps field will be copied.

For more information check: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_clone

Let me know if this helos you.