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
Rutvij Pathak 5Rutvij Pathak 5 

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call for multiple row entries

Getting error for multiple inputs in a row. Could not get an exact issue, Can someone help?

Insert failed. First exception on row 0 with id 0036F000023Ra8GQAS; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!save}' in component <apex:commandButton> in page createmultiplecontacts: Class.AddMultipleContacts.save: line 27, column 1



****************************************************************************************************

public class AddMultipleContacts {
  
    public List<Contact> ContactList {get;set;}     
    public Integer rowNum{get;set;}
    public string AcctID{get;set;}
           
    sObject con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
    
    public AddMultipleContacts(ApexPages.StandardController controller ){
        AcctID = ApexPages.currentPage().getParameters().get('Id');
        system.debug('-AcctID--'+AcctID);
        ContactList = new List<Contact>(); 
        ContactList.add(new contact(Accountid = Acctid));        
    }   
    
    public PageReference save(){
            for(Contact con :ContactList) 
            {
            if(con.FirstName == '' || con.FirstName == null)
                
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
                return null;
            } else 
                
            {
                insert ContactList ;
            }
            }    
        PageReference page1 = new PageReference ('/'+ AcctId ); 
        Page1.setRedirect(true) ;  
        return page1;        
            } 
          
    public void insertRow(){
        ContactList.add(new contact(Accountid = Acctid ));
        
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
    public PageReference Cancel(){
     PageReference page2 = new PageReference ('/'+ AcctId )   ;
     Page2.setRedirect(true) ;  
    return page2;
}
    
    
}
FearNoneFearNone
hi Rutjiv, 

based on this:
for(Contact con :ContactList) {
    if(con.FirstName == '' || con.FirstName == null) {
        ApexPages.addmessage(...));
        return null;
    } else {
        insert ContactList ;
    }
}
you loop the list, then insert the list, then insert the list again, then insert again... and so on...
therefore, the inserted data was re-inserted.

you should avoid DML inside the loop:
for(Contact con :ContactList) {
    if(con.FirstName == '' || con.FirstName == null) {
        ApexPages.addmessage(...));
        return null;
    } else {
        ....
    }
    insert ContactList ;
}


but to fix the syntax only: (but this is not suggested because there is a governor limit)
for(Contact con :ContactList) {
    if(con.FirstName == '' || con.FirstName == null) {
        ApexPages.addmessage(...));
        return null;
    } else {
        insert con;
    }
}

 
RD@SFRD@SF
Hi Rutviz,

The contact's LastName and Firstname is not getting set

try this code
 
public class AddMultipleContacts {
  
    public List<Contact> ContactList {get;set;}     
    public Integer rowNum{get;set;}
    public string AcctID{get;set;}
           
    Contact con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
    
    public AddMultipleContacts(ApexPages.StandardController controller ){
        AcctID = ApexPages.currentPage().getParameters().get('Id');
        system.debug('-AcctID--'+AcctID);
        ContactList = new List<Contact>(); 
        ContactList.add(new contact(Account =:AcctID,FirstName=:con.FirstName,LastName=:con.LastName));        
    }   
    
    public PageReference save(){
           // for(Contact con :ContactList) 
           // {
            if(con.FirstName == '' || con.FirstName == null)
                
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
                return null;
            } else 
                
            {
                insert ContactList ;
            }
          //  }    
        PageReference page1 = new PageReference ('/'+ AcctId ); 
        Page1.setRedirect(true) ;  
        return page1;        
            } 
          
    public void insertRow(){
        ContactList.add(new contact(Accountid = Acctid,FirstName=:con.FirstName,LastName=:con.LastName ));
        
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
    public PageReference Cancel(){
     PageReference page2 = new PageReference ('/'+ AcctId )   ;
     Page2.setRedirect(true) ;  
    return page2;
}
    
    
}


Hope it helps
RD
Rutvij Pathak 5Rutvij Pathak 5
Hi @rd .GIves error 'unexpected token: ':'' on line 13
RD@SFRD@SF
Had a small debug

Try this, tested and working for me
 
public class AddMultipleContacts {
  
    public List<Contact> ContactList {get;set;}     
    public Integer rowNum{get;set;}
    public string AcctID{get;set;}
           
    Contact con = [Select ID ,FirstName , LastName from Contact LIMIT 1];
    
    public AddMultipleContacts(ApexPages.StandardController controller ){
        AcctID = ApexPages.currentPage().getParameters().get('Id');
        system.debug('-AcctID--'+AcctID);
        ContactList = new List<Contact>(); 
        ContactList.add(new contact(Accountid =AcctID,FirstName=con.FirstName,LastName=con.LastName));        
    }   
    
    public PageReference save(){
           // for(Contact con :ContactList) 
           // {
            if(con.FirstName == '' || con.FirstName == null)
                
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Contact First name'));
                return null;
            } else 
                
            {
                insert ContactList ;
            }
          //  }    
        PageReference page1 = new PageReference ('/'+ AcctId ); 
        Page1.setRedirect(true) ;  
        return page1;        
            } 
          
    public void insertRow(){
        ContactList.add(new contact(Accountid =Acctid,FirstName=con.FirstName,LastName=con.LastName ));
        
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
    public PageReference Cancel(){
     PageReference page2 = new PageReference ('/'+ AcctId )   ;
     Page2.setRedirect(true) ;  
    return page2;
}
    
    
}



Regards
RD