1

Is it possible to add or edit multiple records on a custom VF page? For the example below, if lineItemList return fewer than 10 records, I would like to render blank inputfields for insert of new records. The way I have it set now, it will only render as many inputfields as existing records. As always, any help is appreciated.

Page:

<apex:page standardcontroller="Resourcing_Plan__c" extensions="MultipleLineItemsController">
    <apex:form >
        <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:outputPanel >
                <apex:outputField value="{!rp.name}"/>
                <apex:pageblocktable>
                    <apex:repeat value="{!lineItemList}" var="a">
                        <tr>
                            <td><apex:inputField value="{!a.Resource_type__c}" /></td>
                            <td><apex:inputField value="{!a.Weekly_commitment_fte__c}"/></td>
                            <td><apex:inputField value="{!a.Area_of_Expertise__c }"/></td>
                        </tr>
                    </apex:repeat>
                </apex:pageblocktable>
            </apex:outputPanel>
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public with sharing class MultipleLineItemsController 
{
    public resourcing_plan__c rp {get;set;}
    public list<Resource_plan_line_item__c> lineItemList{get;set;}
    public MultipleLineItemsController(ApexPages.StandardController controller) 
    {
        controller.addFields(New String[]{'name'});
        rp= (Resourcing_Plan__c)controller.getRecord();
        lineItemList=[select Resource_type__c, Weekly_commitment_fte__c, 
            Area_of_Expertise__c from Resource_plan_line_item__c where 
            Resourcing_Plan__c=:rp.id limit 10];    
    }
}

1 Answer 1

2

This is an example similar to what you are trying, you can run the below standalone files and find the logic. You can implement the same logic in your code.

VF Page (addcontactpage)

<apex:page standardController="Account" extensions="AddContacts" sidebar="false">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <apex:form >
    <apex:pageBlock title="Accounts" id="pb">
      <apex:pageMessages />      
      <apex:variable var="rowNumber" value="{!0}"/>
        <apex:pageBlockTable id="thetable" title="Contacts" var="acc" value="{!contactList}">
          <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext">
            <apex:outputText value="{0}" style="text-align:center;">
              <apex:param value="{!rowNumber+1}" />
            </apex:outputText>
          </apex:column>
          <apex:column headerValue="First Name" >
            <apex:inputField value="{!acc.FirstName}"/>
          </apex:column>
          <apex:column headerValue="Last Name" >
            <apex:inputField value="{!acc.LastName}"/>
          </apex:column>
          <apex:column headerValue="Action" >
            <apex:commandButton value="Delete" action="{!deleteRow}" reRender="pb">
              <apex:param name="rowIndex" value="{!rowNumber}"/>
            </apex:commandButton>
            <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
          </apex:column>
        </apex:pageBlockTable>
      <apex:commandButton action="{!addRow}" value="Add Contact" reRender="pb"/>      
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}" />
        <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>           
    </apex:pageBlock>
  </apex:form>
</apex:page>

Controller

public class AddContacts {
        public Account accounts;
        public Contact del;
        public List < Contact > addContactList {get;set;}
        public List < Contact > delContactList {get;set;}
        public List < Contact > contactList {get;set;}
        public Integer totalCount {get;set;}
        public Integer rowIndex {get;set;}
        public List < Contact > delContacts {get;set;}

        public AddContacts(ApexPages.StandardController controller) {
                accounts = (Account) controller.getRecord();
                contactList = [Select id, firstName, LastName from Contact where AccountId = : accounts.Id];
                totalCount = contactList.size();
                delContactList = new List < Contact > ();
                delContacts = new List < Contact > ();
        }

        public void addRow() {
                addContactList = new List < Contact > ();
                contactList.add(new Contact(AccountId = accounts.Id));
        }

        public PageReference save() {
                upsert contactList;
                delete delContactList;
                //return (new ApexPages.StandardController(accounts)).view();
                PageReference pageRef = new PageReference('/apex/addcontactpage');
                pageRef.getParameters().put('id',accounts.Id);
                return pageRef;
        }
        public void deleteRow() {
                rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
                del = contactList.remove(rowIndex);
                delContactList.add(del);
        }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .