• Rutvij Pathak 5
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
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;
}
    
    
}
Hi , I am trying to insert list of Contacts from visualforce page on a Button click. And after button click the page should automatically go back to account page.

I have Created a Custom Button on an Account page.

The list also has a Validation to check if the first name is updated or not.

Basically - 1 - Contact First name must not be blank.
2 - On Click of Save, Added Contacts should be inserted to the database.
3 - after insert Source account record should open

My below code saves the records and redirects correctly but does not fire validation on first name.

Please find controller code below :- 

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'));
            } else 
                
            {
                insert ContactList ;
            }
            }    
        PageReference page1 = new PageReference ('https://ap4.salesforce.com/'+ 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 ('https://ap4.salesforce.com/'+ AcctId )   ;
     Page2.setRedirect(true) ;  
    return page2;
}
    
    
}
I am creating a Page to add multiple contacts to an Account. The Page is Called by a Button Click on Account Detail Page.'
For now, Contacts are getting Created however, they are not getting Associated to Account.

VISUALFORCE PAGE ---------

<apex:page name="Create Multiple Contacts" controller="AddMultipleContacts" showHeader="true" sidebar="true">        
    <apex:form >    
    <apex:pageBlock title="Add Contacts">
        <apex:variable var="rowNum" value="{!0}"/>
        <apex:pageBlockTable value="{!ContactList}" var="con">
            <apex:facet name="footer">               
                <apex:commandButton value="Add" style="float: right;" action="{!insertRow}" />                    
             </apex:facet>
            <apex:column headerValue="First Name">
                <apex:inputField value="{!con.FirstName}"/>
            </apex:column>
            
            <apex:column headerValue="Last Name">
                <apex:inputField value="{!con.LastName}"/>
            </apex:column>
            <apex:column headerValue="Phone">
                <apex:inputField value="{!con.Phone}"/>
            </apex:column>
            <apex:column headerValue="Email">
                <apex:inputField value="{!con.Email}"/>                
            </apex:column>
            
             <apex:column headerValue="Action" >
                    <apex:commandLink style="font-size:12px; font-weight:bold; text-align:center;color:Black;" value="Delete" action="{!delRow}">
                    <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink> 
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
             </apex:column> 
         </apex:pageBlockTable>
            <apex:commandButton value="Cancel" onclick="window.history.previous()"/>
            
            <apex:pageBlockButtons location="bottom" >
                <apex:commandButton value="Save Contacts" action="{!insertContacts}"/>           
            </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>        
</apex:page>

CONTROLLER ---------

public class AddMultipleContacts {
    Id AcctID;
    public List<Contact> ContactList {get;set;}
    public Integer rowNum{get;set;}
    
    public AddMultipleContacts(){
        AcctID = ApexPages.currentPage().getParameters().get('AcctID');
        ContactList = new List<Contact>();  
        ContactList.add(new contact());        
    }        
    public void insertContacts(){
        insert ContactList;
    }                    
    public void insertRow(){
        ContactList.add(new contact()); 
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
}
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;
}
    
    
}
I am creating a Page to add multiple contacts to an Account. The Page is Called by a Button Click on Account Detail Page.'
For now, Contacts are getting Created however, they are not getting Associated to Account.

VISUALFORCE PAGE ---------

<apex:page name="Create Multiple Contacts" controller="AddMultipleContacts" showHeader="true" sidebar="true">        
    <apex:form >    
    <apex:pageBlock title="Add Contacts">
        <apex:variable var="rowNum" value="{!0}"/>
        <apex:pageBlockTable value="{!ContactList}" var="con">
            <apex:facet name="footer">               
                <apex:commandButton value="Add" style="float: right;" action="{!insertRow}" />                    
             </apex:facet>
            <apex:column headerValue="First Name">
                <apex:inputField value="{!con.FirstName}"/>
            </apex:column>
            
            <apex:column headerValue="Last Name">
                <apex:inputField value="{!con.LastName}"/>
            </apex:column>
            <apex:column headerValue="Phone">
                <apex:inputField value="{!con.Phone}"/>
            </apex:column>
            <apex:column headerValue="Email">
                <apex:inputField value="{!con.Email}"/>                
            </apex:column>
            
             <apex:column headerValue="Action" >
                    <apex:commandLink style="font-size:12px; font-weight:bold; text-align:center;color:Black;" value="Delete" action="{!delRow}">
                    <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink> 
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
             </apex:column> 
         </apex:pageBlockTable>
            <apex:commandButton value="Cancel" onclick="window.history.previous()"/>
            
            <apex:pageBlockButtons location="bottom" >
                <apex:commandButton value="Save Contacts" action="{!insertContacts}"/>           
            </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>        
</apex:page>

CONTROLLER ---------

public class AddMultipleContacts {
    Id AcctID;
    public List<Contact> ContactList {get;set;}
    public Integer rowNum{get;set;}
    
    public AddMultipleContacts(){
        AcctID = ApexPages.currentPage().getParameters().get('AcctID');
        ContactList = new List<Contact>();  
        ContactList.add(new contact());        
    }        
    public void insertContacts(){
        insert ContactList;
    }                    
    public void insertRow(){
        ContactList.add(new contact()); 
    }    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        ContactList.remove(rowNum);   
    }        
}