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
StaciStaci 

save multirows apex page not creating new records

I have some code I found on here to create "Add Row" functionality.  Everything seems to work except the Save button.  Its not creating the new records.  

What should happen is on a Subscription record, I have an embedded apex page to add Subscription Assets (which is a related list on the Subscription record).  I want to be able to add multiple rows of the Subscription Assets and save them to the related list on the Subscription record.  What happens is when I click save, it refreshes, blanks out the fields in the embedded apex page and doesn't save any of the Subscription Asset records anywhere.  It's been awhile since I've done any coding and I'm a bit rusty. 

Apex Page
<apex:page standardController="Support_Subscription__c" 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:pageblock id="pb1">

   
        <apex:repeat value="{!lstInner}" var="e1" id="therepeat">
        

            <apex:panelGrid columns="10">

                <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 >

                 <apex:facet name="header">Equipment/Asset Licensed</apex:facet>

                    <apex:inputfield value="{!e1.asset.Equipment_Asset_Licensed__c}"/>

                </apex:panelGrid>

                <apex:panelGrid >

                    <apex:facet name="header">Serial Number</apex:facet>

                    <apex:inputfield value="{!e1.asset.Serial_Number__c}"/>

                </apex:panelGrid>
         
                <apex:panelGrid >

                    <apex:facet name="header">Site Location</apex:facet>

                    <apex:inputfield value="{!e1.asset.Site_Location__c}"/>

               </apex:panelGrid>
                
                <apex:panelGrid >

                    <apex:facet name="header">MineStar Product</apex:facet>

                    <apex:inputfield value="{!e1.asset.MineStar_Product__c}" style="width:200px"/>

                </apex:panelGrid>
                
                <apex:panelGrid >

                    <apex:facet name="header">Version</apex:facet>

                    <apex:inputfield value="{!e1.asset.Version__c}"/>

                </apex:panelGrid>

            </apex:panelGrid>

        </apex:repeat>
</apex:pageBlock>

</apex:pageblock>

</apex:form>

</apex:page>

Class
public class MultiAdd {

    

    //will hold the Subscription Asset records to be saved

    public List<Subscription_Assets__c>lstAsset = new List<Subscription_Assets__c>();

    //list of the inner class

    public List<innerClass> lstInner

    {   get;set;    }

    //will indicate the row to be deleted

    public String selectedRowIndex

    {get;set;} 
    
    Public Support_Subscription__c ss
        {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 lstAsset,return to the same page

    public PageReference Save()

    {
         
        PageReference pr = new PageReference('/apex/CW_Support_Subscription');
 for(Subscription_Assets__c sa : lstAsset)
 {
        for(Integer j = 0;j<lstInner.size();j++)

        {
           

            sa.Support_Subscription__c  = ss.Name;
            lstAsset.add(lstInner[j].asset);
        } 
        }
        insert lstAsset;
        
        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)

    {

        lstInner = new List<innerClass>();

        addMore();

        selectedRowIndex = '0';

    }/*End Constructor*/

    /*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 Subscription_Assets__c asset

        {get;set;}
        
        

        /*Inner Class Constructor*/

        public innerClass(Integer intCount)

        {

            recCount = String.valueOf(intCount);       
           
            /*create a new account*/

            asset = new Subscription_Assets__c();
            
            
            

        }/*End Inner class Constructor*/   

    }/*End inner Class*/

}/*End Class*/
User-added image
​​​​​​​
David Zhu 🔥David Zhu 🔥
Your repeat is on lstInner Class instead of on lstAsset. You may modify you code by this:
 
public PageReference Save()

    {
         
        PageReference pr = new PageReference('/apex/CW_Support_Subscription');

        lstAsset = new List<Subscription_Assets__c>();

        for(Integer j = 0;j<lstInner.size();j++)
        {
           Subscription_Assets__c sa = lstInner.asset;

            sa.Support_Subscription__c  = ss.Name;
            lstAsset.add(sa);
        } 

        insert lstAsset;
        
        pr.setRedirect(True);

        return pr;

    }

 
SSP AdminSSP Admin
Hi Staci,

It looks like our team of experts can help you resolve this ticket.
We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com

Thanks,
Jarvis SFDC team
StaciStaci
@David Zhu, when I replace my Save class with your suggestion it tells me Variable does not exist: asset on this line lstAsset.add(lstInner.asset);
Where do I need to declare it now? Its currently in the innerClass, but when I try to move it to various places I still get the same error. 
David Zhu 🔥David Zhu 🔥
Add this line 
List<Subscription_Assets__c>lstAsset = new List<Subscription_Assets__c>();

before lstAsset = new List<Subscription_Assets__c>();
StaciStaci
@David Zhu
now its giving the same error for this line Subscription_Assets__c sa = lstInner.asset;
 
public class MultiAdd {

    

    //will hold the Subscription Asset records to be saved

    public List<Subscription_Assets__c>lstAsset = new List<Subscription_Assets__c>();

    //list of the inner class

    public List<innerClass> lstInner

    {   get;set;    }

    //will indicate the row to be deleted

    public String selectedRowIndex

    {get;set;} 
    
    Public Support_Subscription__c ss
        {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 lstAsset,return to the same page

public PageReference Save()

    {

        PageReference pr = new PageReference('/apex/CW_Support_Subscription');
        List<Subscription_Assets__c>lstAsset = new List<Subscription_Assets__c>();
        lstAsset = new List<Subscription_Assets__c>();

        for(Integer j = 0;j<lstInner.size();j++)

        {

           Subscription_Assets__c sa = lstInner.asset;

            sa.Support_Subscription__c = ss.Name;
            lstAsset.add(sa);
        }

        insert lstAsset;

        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)

    {

        lstInner = new List<innerClass>();

        addMore();

        selectedRowIndex = '0';

    }/*End Constructor*/

    /*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 Subscription_Assets__c asset

        {get;set;}
        
        

        /*Inner Class Constructor*/

        public innerClass(Integer intCount)

        {

            recCount = String.valueOf(intCount);       
           
            /*create a new account*/

            asset = new Subscription_Assets__c();
            
            
            

        }/*End Inner class Constructor*/   

    }/*End inner Class*/

}/*End Class*/