+ Start a Discussion
sailersailer 

To Add the rows in VF Page and update the records in the object

Hello Everyone,

 

I Have created the VF page that shows account and the VF page contains another section where there will be contact list ,and whenevr the user click the Add Button its creates a new row ,when i save from the VF page the records are not created .

Secondly the relatedlist of records are not getting displayed under that account.

 VF PAGE

 

<apex:page StandardController="Account" extensions="MultiAdd" id="thePage">
<apex:form >
<apex:pageblock id="pb" >
    <apex:pageBlockButtons >
        <apex:commandbutton value="Add" action="{!Add}" rerender="pb1"/>
        <apex:commandbutton value="Save" action="{!Save}"/>
    </apex:pageBlockButtons>
    
      <apex:pageBlockSection title="Account Details" collapsible="true" id="mainRecord" columns="2" >           
                    <apex:inputField value="{!Account.Name}"/>
                    <apex:inputField value="{!Account.Type}"/>
                    <apex:inputField value="{!Account.BillingStreet}"/>
                    <apex:inputField value="{!Account.Industry}"/>
                    <apex:inputField value="{!Account.Phone}"/>
            </apex:pageBlockSection>
    
        
        <apex:pageblock id="pb1">
            
        <apex:repeat value="{!lstInner}" var="e1" id="therepeat">
                <apex:panelGrid columns="6">
                
                <apex:panelGrid headerClass="Name">
                    <apex:facet name="header">Del</apex:facet>
                    <apex:commandButton value="X" action="{!Del}" rerender="pb1">
                        <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
                    </apex:commandButton>
                </apex:panelGrid>   
                
                <apex:panelGrid title="SPD" >
                    <apex:facet name="header">LastName</apex:facet>
                    <apex:inputfield value="{!e1.acct.LastName}"/>
                </apex:panelGrid>
                
                <apex:panelGrid >
                    <apex:facet name="header">FirstName</apex:facet>
                    <apex:inputfield value="{!e1.acct.FirstName}"/>
                </apex:panelGrid>
                
                <apex:panelGrid >
                    <apex:facet name="header">TotalNumber</apex:facet>
                    <apex:inputfield value="{!e1.acct.TotalNumber__c}"/>
                </apex:panelGrid>
                
                <apex:panelGrid >
                    <apex:facet name="header">DoctorDescription</apex:facet>
                    <apex:inputfield value="{!e1.acct.DoctorDescription__c}"/>
                </apex:panelGrid>
                
                
                
                
                
            </apex:panelgrid>
            
                         
            
        </apex:repeat>
    </apex:pageBlock>
        
</apex:pageblock>
</apex:form>
</apex:page>

 

Apex Class:

 

public class MultiAdd
{
    
    public List<contact> lstcontact  = new List<contact>();
    public List<Contact> contacts;
    private ApexPages.StandardController std;
    
    //list of the inner class
    public List<innerClass> lstInner
    {   get;set;    }
    
    //will indicate the row to be deleted
    public String selectedRowIndex
    {get;set;}  
    
    //no. of rows added/records in the inner class list
    public Integer count = 1;
    //{get;set;}
    
    
    ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
    public PageReference Save()
    {
        PageReference pr = new PageReference('/apex/jqueryDoctorProfile');
        
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstcontact .add(lstInner[j].acct);
        }
        insert lstcontact;
        pr.setRedirect(True);
        return pr;
    }
        
    //add one more row
    public void Add()
    {   
        count = count+1;
        addMore();      
    }
    
    /*Begin addMore*/
    public void addMore()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count);
        
        //add the record to the inner class list
        lstInner.add(objInnerClass);    
        system.debug('lstInner---->'+lstInner);            
    }/* end addMore*/
    
    /* begin delete */
    public void Del()
    {
        system.debug('selected row index---->'+selectedRowIndex);
        lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
        count = count - 1;
        
    }/*End del*/
    
    
    
    /*Constructor*/
    public MultiAdd(ApexPages.StandardController ctlr)
    {
    
        std=ctlr;
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';
        
        
    }/*End Constructor*/
     public Account getAccount()  
      {      
        return (Account) std.getRecord();   
      }
        


    /*Inner Class*/
    public class innerClass
    {       
        /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
        public String recCount
        {get;set;}
        
        
        public contact acct
        {get;set;}
        
        /*Inner Class Constructor*/
        public innerClass(Integer intCount)
        {
            recCount = String.valueOf(intCount);        
            
            /*create a new account*/
            acct = new contact();
            
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
     public List<Contact> getContacts()    
  {        if ( (null!=getAccount().id) && (contacts == null) )  
          {            
            contacts=[SELECT Id, Name, Email, Phone, AccountId, Title,  Salutation, OtherStreet, OtherState, OtherPostalCode,   OtherPhone, OtherCountry, OtherCity, MobilePhone, MailingStreet, MailingState,     MailingPostalCode, MailingCountry, MailingCity, LeadSource, LastName, HomePhone, FirstName, Fax, Description, Department   FROM Contact  WHERE AccountId = : getAccount().ID   ORDER BY CreatedDate];  
                  }                                    return contacts;  
                     }
}/*End Class*/

 

Thanks for the Help in advance.

 

Regards

Sailer

 

hitesh90hitesh90

Hi sailer,

 

you have to modify your controller.

try to use below apex class.

 

Apex class:

public class MultiAdd
{
    
    public List<contact> lstcontact  = new List<contact>();
    public List<Contact> contacts;
    private ApexPages.StandardController std;
    
    //list of the inner class
    public List<innerClass> lstInner
    {   get;set;    }
    
    //will indicate the row to be deleted
    public String selectedRowIndex
    {get;set;}  
    
    //no. of rows added/records in the inner class list
    public Integer count = 1;
    //{get;set;}
    
    
    ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
    public PageReference Save()
    {
        PageReference pr = new PageReference('/apex/jqueryDoctorProfile');
        
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstcontact .add(lstInner[j].acct);
        }
        upsert lstcontact;
        pr.setRedirect(True);
        return pr;
    }
        
    //add one more row
    public void Add()
    {   
        count = count+1;
        addMore();      
    }
     public void addMoreconst()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count);
        List<Contact> lstCon = getContacts();
        for(Contact c: lstCon){
            objInnerClass = new innerClass(count);
            objInnerClass.acct = c;
            lstInner.add(objInnerClass);
        }
        //add the record to the inner class list
            
        system.debug('lstInner---->'+lstInner);            
    }/* end addMore*/
    
    /*Begin addMore*/
    public void addMore()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count);
        
        //add the record to the inner class list
            lstInner.add(objInnerClass);
        system.debug('lstInner---->'+lstInner);            
    }/* end addMore*/
    
    /* begin delete */
    public void Del()
    {
        system.debug('selected row index---->'+selectedRowIndex);
        lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
        count = count - 1;
        
    }/*End del*/
    
    
    
    /*Constructor*/
    public MultiAdd(ApexPages.StandardController ctlr)
    {
    
        std=ctlr;
        lstInner = new List<innerClass>();
        addMoreconst();
        selectedRowIndex = '0';
        
        
    }/*End Constructor*/
     public Account getAccount()  
      {      
        return (Account) std.getRecord();   
      }
        


    /*Inner Class*/
    public class innerClass
    {       
        /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
        public String recCount
        {get;set;}
        
        
        public contact acct
        {get;set;}
        
        /*Inner Class Constructor*/
        public innerClass(Integer intCount)
        {
            recCount = String.valueOf(intCount);        
            
            /*create a new account*/
            acct = new contact();
            
            
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
     public List<Contact> getContacts()    
  {        if ( (null!=getAccount().id) && (contacts == null) )  
          {            
            contacts=[SELECT Id, Name, Email, Phone, AccountId, Title,  Salutation, OtherStreet, OtherState, OtherPostalCode,   OtherPhone, OtherCountry, OtherCity, MobilePhone, MailingStreet, MailingState,     MailingPostalCode, MailingCountry, MailingCity, LeadSource, LastName, HomePhone, FirstName, Fax, Description, Department   FROM Contact  WHERE AccountId = : getAccount().ID   ORDER BY CreatedDate];  
                  }                                    return contacts;  
                     }
}/*End Class*/

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

sailersailer

Hi Patel,

 

When is the abpve code i get the following error.

 


System.NullPointerException: Attempt to de-reference a null object 

 

Class.testvantage.MultiAdd.addMoreconst: line 46, column 1
Class.testvantage.MultiAdd.<init>: line 84, column 1

hitesh90hitesh90

Try to use following code..

 

Apex Class:

public class MultiAdd
{
    
    public List<contact> lstcontact  = new List<contact>();
    public List<Contact> contacts;
    private ApexPages.StandardController std;
    
    //list of the inner class
    public List<innerClass> lstInner
    {   get;set;    }
    
    //will indicate the row to be deleted
    public String selectedRowIndex
    {get;set;}  
    
    //no. of rows added/records in the inner class list
    public Integer count = 1;
    //{get;set;}
    
    
    ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
    public PageReference Save()
    {
        PageReference pr = new PageReference('/apex/jqueryDoctorProfile');
        
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstcontact .add(lstInner[j].acct);
        }
        upsert lstcontact;
        pr.setRedirect(True);
        return pr;
    }
        
    //add one more row
    public void Add()
    {   
        count = count+1;
        addMore();      
    }
     public void addMoreconst()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count);
        List<Contact> lstCon = new List<Contact>();
	lstCon = getContacts();
        for(Contact c: lstCon){
            objInnerClass = new innerClass(count);
            objInnerClass.acct = c;
            lstInner.add(objInnerClass);
        }
        //add the record to the inner class list
            
        system.debug('lstInner---->'+lstInner);            
    }/* end addMore*/
    
    /*Begin addMore*/
    public void addMore()
    {
        //call to the iner class constructor
        innerClass objInnerClass = new innerClass(count);
        
        //add the record to the inner class list
            lstInner.add(objInnerClass);
        system.debug('lstInner---->'+lstInner);            
    }/* end addMore*/
    
    /* begin delete */
    public void Del()
    {
        system.debug('selected row index---->'+selectedRowIndex);
        lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
        count = count - 1;
        
    }/*End del*/
    
    
    
    /*Constructor*/
    public MultiAdd(ApexPages.StandardController ctlr)
    {
    
        std=ctlr;
        lstInner = new List<innerClass>();
        addMoreconst();
        selectedRowIndex = '0';
        
        
    }/*End Constructor*/
     public Account getAccount()  
      {      
        return (Account) std.getRecord();   
      }
        


    /*Inner Class*/
    public class innerClass
    {       
        /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
        public String recCount
        {get;set;}
        
        
        public contact acct
        {get;set;}
        
        /*Inner Class Constructor*/
        public innerClass(Integer intCount)
        {
            recCount = String.valueOf(intCount);        
            
            /*create a new account*/
            acct = new contact();
            
            
        }/*End Inner class Constructor*/    
    }/*End inner Class*/
     public List<Contact> getContacts()    
  {        if ( (null!=getAccount().id) && (contacts == null) )  
          {            
            contacts=[SELECT Id, Name, Email, Phone, AccountId, Title,  Salutation, OtherStreet, OtherState, OtherPostalCode,   OtherPhone, OtherCountry, OtherCity, MobilePhone, MailingStreet, MailingState,     MailingPostalCode, MailingCountry, MailingCity, LeadSource, LastName, HomePhone, FirstName, Fax, Description, Department   FROM Contact  WHERE AccountId = : getAccount().ID   ORDER BY CreatedDate];  
                  }                                    return contacts;  
                     }
}/*End Class*/

 

sailersailer

Hi 

Still facing the same issues.

 

kindly help me out pl 

 

 

Regards

Sailer