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
Rani S 1Rani S 1 

Insert failed. First exception on row 0 with id 0039000001rwarYAAQ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

public class AddmultipleContactsController {

Contact con = new Contact();
public list<contact> listContacts{ get; set; }

public AddmultipleContactsController()
{
listContacts=new list<Contact>();
listContacts.add(con);
}

Public void addContacts()
{
Contact cc = new Contact();
listContacts.add(cc);
}
public PageReference saveContacts() {
for(Integer i=1; i<listContacts.size(); i++)
{
insert listContacts;
}
return Page.AllContactssaved;
}
}
Best Answer chosen by Rani S 1
Onesh ReddyOnesh Reddy
Hi Rani,

Remove the FOR loop in SaveContacts() method you can directly insert listaccounts in the method, also using DML statements in the loops hits the Governer limits of Salesforce.
If you still find problem, post ur VF page so that ut will be easy to help.
public PageReference saveContacts() {
insert listContacts;
return Page.AllContactssaved;
}
Please let me know if it helps you.

Regards,
Onesh.K
 

All Answers

Onesh ReddyOnesh Reddy
Hi Rani,

Remove the FOR loop in SaveContacts() method you can directly insert listaccounts in the method, also using DML statements in the loops hits the Governer limits of Salesforce.
If you still find problem, post ur VF page so that ut will be easy to help.
public PageReference saveContacts() {
insert listContacts;
return Page.AllContactssaved;
}
Please let me know if it helps you.

Regards,
Onesh.K
 
This was selected as the best answer
Rani S 1Rani S 1
Hi Onesh Reddy Sir, 
         It is working fine by your correction.
        But want i need to ask is what do u mean by Governor Limit of Salesforce.

Regards,
Rani
Onesh ReddyOnesh Reddy
Hi Rani,

Salesforce Apex runs in a multitenant environment, the Apex runtime engine strictly enforces limits to ensure that runaway Apex code or processes don’t monopolize shared resources. If some Apex code ever exceeds a limit, the associated governor issues a runtime exception that cannot be handled.

Please gothrough the below links.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
http://resources.docs.salesforce.com/200/16/en-us/sfdc/pdf/salesforce_app_limits_cheatsheet.pdf


Regards,
Onesh.K