May 14, 2014

Adding multiple child records using apex in Salesforce

List button:


Visualforce page:

<apex:page controller="AddingInterestsController">
    <apex:form >
        <apex:variable var="rowNum" value="{!0}"  />
        <apex:pageBlock >
            <apex:variable var="rowNum" value="{!0}"  />  
            <apex:pageBlockTable value="{!interestList}" var="int">
                <apex:facet name="footer">
                    <apex:commandLink value="Add" action="{!insertRow}"/>
                </apex:facet>
                <apex:column headerValue="Name">
                    <apex:inputField value="{!int.Name}"/>
                </apex:column>
                <apex:column headerValue="Description">
                    <apex:inputField value="{!int.Description__c}"/>
                </apex:column>         
                <apex:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}">
                        <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink>
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
                </apex:column>          
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!insertIntests}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:

public class AddingInterestsController {
    Id empId;
    public List<Interest__c> interestList {get;set;}
    public Integer rowNum{get;set;}
    
    public AddingInterestsController(){
        empId = ApexPages.currentPage().getParameters().get('empId');
        interestList = new List<Interest__c>();  
        interestList.add(new Interest__c());      
    }
    
    public void insertIntests(){
        insert interestList;
    }
    
    public void insertRow(){
        interestList.add(new Interest__c()); 
    }
    
    public void delRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        interestList.remove(rowNum);   
    }
}

Output:



7 comments:

  1. Hi,

    I'm facing challenge in getting record saved, it showing error for master record id.

    Regards
    Krishan Gopal

    ReplyDelete
    Replies
    1. Do use try and catch blocks to find the exception.

      Delete
  2. Hi,
    I'm not add master record lookup at visualforce page and trying to save my record, it showing error for master record id. I want child record to be save under master record without lookup input from user. Please suggest the required changes in program for the same.

    ReplyDelete
    Replies
    1. Before inserting the child records, update the Master Detail field.

      In my example before insert interestList, we should iterate and pass Employee__c = empId for each interest record.

      Delete
  3. Hi
    I trying to iterate through the list but still i'm getting error Variable does not exist: Employee__c at line X column Y
    Please help me to fix this error

    public void insertIntests(){
    for(Integer i=1; i<interestList.size(); i++){
    interestList.Employee__c = empId;
    }
    insert interestList;
    }

    ReplyDelete
  4. It is incorrect.
    Try the below way

    public void insertIntests(){
    for( Interest__c objInt : interestList ){
    objInt.Employee__c = empId;
    }
    insert interestList;
    }

    ReplyDelete
  5. Many Thanks Sir!!
    Now its working

    ReplyDelete