+ Start a Discussion
Ethan WestEthan West 

Adding multiple rows; not working.

Hi - 
 

I have a Mass Create Button that I'm trying to get functionality to work on but it doesn't seem to be going to plan. Can someone assist?
 

I'm trying to get multiple rows to be created when I click on the "Add Assignment Row" button, but it just refreshes the page and shows a single row again (see button and row below). 
User-added image
This is my current VF Page:

<apex:page standardController="pse__Assignment__c" recordSetVar="unused" extensions="AddanotherAssignmentController" >
<apex:enhancedList type="pse__Assignment__c" height="300" rowsPerPage="10" id="ListViewID"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!listAssignment}" var="TC">
                <apex:column headerValue="Assignment Name">
                    <apex:inputField value="{!TC.Name}"/>
                </apex:column>
                <apex:column headerValue="Resource">
                    <apex:inputField value="{!TC.pse__Resource__c}"/>
                </apex:column>
                <apex:column headerValue="Project">
                    <apex:inputField value="{!TC.pse__Project__c}"/>
                </apex:column>
                <apex:column headerValue="Candidate">
                    <apex:inputField value="{!TC.Candidate__c}"/>
                </apex:column>
                <apex:column headerValue="Bill Rate">
                    <apex:inputField value="{!TC.pse__Bill_Rate__c}"/>
                </apex:column>    
                <apex:column headerValue="Start Date">
                    <apex:inputField value="{!TC.Start_Date__c}"/>
                </apex:column>
                <apex:column headerValue="End Date">
                    <apex:inputField value="{!TC.End_Date__c}"/>
                </apex:column>
                <apex:column headerValue="Monday Hours">
                    <apex:inputField value="{!TC.Monday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Tuesday Hours">
                    <apex:inputField value="{!TC.Tuesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Wednesday Hours">
                    <apex:inputField value="{!TC.Wednesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Thursday Hours">
                    <apex:inputField value="{!TC.Thursday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Friday Hours">
                    <apex:inputField value="{!TC.Friday_Hours__c}"/>
                </apex:column>                       
                <apex:column headerValue="Saturday Hours">
                    <apex:inputField value="{!TC.Saturday_Hours__c}"/>
                </apex:column>                             
                <apex:column headerValue="Sunday Hours">
                    <apex:inputField value="{!TC.Sunday_Hours__c}"/>
                </apex:column>
            </apex:pageBlockTable>
                <apex:pageBlockButtons >
                    <apex:commandButton value="Add Assignment Row" action="{!addAssignment}"/>
                    <apex:commandButton value="Save Assignments" action="{!save}"/>
                </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

and this is my current Apex Class Page:

public class AddanotherAssignmentController {

    public AddanotherAssignmentController(ApexPages.StandardSetController controller) {

    }

    pse__Assignment__c assignment = new pse__Assignment__c();
    public list<pse__Assignment__c> listAssignment{ get; set; }
    
    public AddanotherAssignmentController() {
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }
    
    Public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        listAssignment = new List<pse__Assignment__c>();
        listAssignment.add(TC);
    }
    public PageReference saveAssignment() {
        for(Integer i=0; i<listAssignment.size(); i++) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}

Any interpretation as to why I'm only getting one row added at a time would be very useful.

Many thanks,
Ethan

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ethan,

Greetings to you!

I have made a few changes to your code. Please use below code:
 
Visualforce:
<apex:page standardController="pse__Assignment__c" recordSetVar="unused" extensions="AddanotherAssignmentController" >
<apex:enhancedList type="pse__Assignment__c" height="300" rowsPerPage="10" id="ListViewID"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!listAssignment}" var="asgn">
                <apex:column headerValue="Assignment Name">
                    <apex:inputField value="{!asgn.Name}"/>
                </apex:column>
                <apex:column headerValue="Resource">
                    <apex:inputField value="{!asgn.pse__Resource__c}"/>
                </apex:column>
                <apex:column headerValue="Project">
                    <apex:inputField value="{!asgn.pse__Project__c}"/>
                </apex:column>
                <apex:column headerValue="Candidate">
                    <apex:inputField value="{!asgn.Candidate__c}"/>
                </apex:column>
                <apex:column headerValue="Bill Rate">
                    <apex:inputField value="{!asgn.pse__Bill_Rate__c}"/>
                </apex:column>    
                <apex:column headerValue="Start Date">
                    <apex:inputField value="{!asgn.Start_Date__c}"/>
                </apex:column>
                <apex:column headerValue="End Date">
                    <apex:inputField value="{!asgn.End_Date__c}"/>
                </apex:column>
                <apex:column headerValue="Monday Hours">
                    <apex:inputField value="{!asgn.Monday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Tuesday Hours">
                    <apex:inputField value="{!asgn.Tuesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Wednesday Hours">
                    <apex:inputField value="{!asgn.Wednesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Thursday Hours">
                    <apex:inputField value="{!asgn.Thursday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Friday Hours">
                    <apex:inputField value="{!asgn.Friday_Hours__c}"/>
                </apex:column>                       
                <apex:column headerValue="Saturday Hours">
                    <apex:inputField value="{!asgn.Saturday_Hours__c}"/>
                </apex:column>                             
                <apex:column headerValue="Sunday Hours">
                    <apex:inputField value="{!asgn.Sunday_Hours__c}"/>
                </apex:column>
            </apex:pageBlockTable>
                <apex:pageBlockButtons >
                    <apex:commandButton value="Add Assignment Row" action="{!addAssignment}"/>
                    <apex:commandButton value="Save Assignments" action="{!save}"/>
                </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class AddanotherAssignmentController {

    pse__Assignment__c assignment = new pse__Assignment__c();
    public list<pse__Assignment__c> listAssignment{ get; set; }

    public AddanotherAssignmentController(ApexPages.StandardSetController controller) {
          listAssignment=new list<pse__Assignment__c>();
          listAssignment.add(assignment);
    }
   
    Public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        listAssignment.add(TC);
    }

    public PageReference saveAssignment() {
        if(listAssignment.size()>0){ // You don't need a for loop
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ethan WestEthan West

Hi Khan,

I tried your code and it doesn't seem to be saving records, and for some reason this one isn't taking me to my Allassignmentsaved reference either. 

Any assistance as to why this isn't saving the records? [Same issue happening with my other open case] 
 

Kind regards,

Ethan

Khan AnasKhan Anas (Salesforce Developers) 
I have tried the same piece of code and it is working fine for me. Please go to pse__Assignment__c object and check the records (select All from ListView).
Sami ShakithSami Shakith
Hello Ethan

Try the below code
Apex Class
public class AddanotherAssignmentController {

    public AddanotherAssignmentController(ApexPages.StandardSetController controller) {
		listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }

    pse__Assignment__c assignment = new pse__Assignment__c();
    public list<pse__Assignment__c> listAssignment{ get; set; }
	
	//As you defined standard controller attribute on Vf page you dont need of the custom controller
    /*********************************
    public AddanotherAssignmentController() {
        listAssignment=new list<pse__Assignment__c>();
        listAssignment.add(assignment);
    }
    **************************************/
	
    Public void addAssignment() {
        pse__Assignment__c TC = new pse__Assignment__c();
        // listAssignment = new List<pse__Assignment__c>(); //You should not to initialize the list everytime you call add assignment method
        listAssignment.add(TC);
    }
    public PageReference saveAssignment() {
        if(listAssignment != null && listAssignment.size()>0) {
            insert listAssignment;
        }
        return Page.Allassignmentsaved;
    }
}


VF page
<apex:page standardController="pse__Assignment__c" recordSetVar="unused" extensions="AddanotherAssignmentController" >
<apex:enhancedList type="pse__Assignment__c" height="300" rowsPerPage="10" id="ListViewID"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!listAssignment}" var="TC">
                <apex:column headerValue="Assignment Name">
                    <apex:inputField value="{!TC.Name}"/>
                </apex:column>
                <apex:column headerValue="Resource">
                    <apex:inputField value="{!TC.pse__Resource__c}"/>
                </apex:column>
                <apex:column headerValue="Project">
                    <apex:inputField value="{!TC.pse__Project__c}"/>
                </apex:column>
                <apex:column headerValue="Candidate">
                    <apex:inputField value="{!TC.Candidate__c}"/>
                </apex:column>
                <apex:column headerValue="Bill Rate">
                    <apex:inputField value="{!TC.pse__Bill_Rate__c}"/>
                </apex:column>    
                <apex:column headerValue="Start Date">
                    <apex:inputField value="{!TC.Start_Date__c}"/>
                </apex:column>
                <apex:column headerValue="End Date">
                    <apex:inputField value="{!TC.End_Date__c}"/>
                </apex:column>
                <apex:column headerValue="Monday Hours">
                    <apex:inputField value="{!TC.Monday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Tuesday Hours">
                    <apex:inputField value="{!TC.Tuesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Wednesday Hours">
                    <apex:inputField value="{!TC.Wednesday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Thursday Hours">
                    <apex:inputField value="{!TC.Thursday_Hours__c}"/>
                </apex:column>
                <apex:column headerValue="Friday Hours">
                    <apex:inputField value="{!TC.Friday_Hours__c}"/>
                </apex:column>                       
                <apex:column headerValue="Saturday Hours">
                    <apex:inputField value="{!TC.Saturday_Hours__c}"/>
                </apex:column>                             
                <apex:column headerValue="Sunday Hours">
                    <apex:inputField value="{!TC.Sunday_Hours__c}"/>
                </apex:column>
            </apex:pageBlockTable>
                <apex:pageBlockButtons >
                    <apex:commandButton value="Add Assignment Row" action="{!addAssignment}"/>
                    <apex:commandButton value="Save Assignments" action="{!saveAssignment}"/><!-- should call the saveAssignment method------>
                </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Thanks