Skip to main content
Hello,

I have created a Visualforce page where users can add multiple child records at once. I want to add a list button to the related list on the parent object. Both are custom objects.The parent object in this scenario Sessions__c and the child object is Class_Enrollments__c. 

Despite trying to use a Standard Controller + Extension, the VF page is not appearing in the Content when trying to add a custom list button. So, instead, I am passing it through the URL (/apex/Add_Class_Enrollments?class_enrollmentsId={!Class_Enrollments__c.Id}).

The below code is not working to pass the Session ID to the Class Enrollment records. The button opens the VF page and the VF page looks correct. However, when I try to save, it gives this error: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Session]: [Session]

Error is in expression '{!insertClass_Enrollments}' in component <apex:commandButton> in page add_class_enrollments: Class.AddingEnrollmentsController.insertClass_Enrollments: line 14, column 1

Here is my VF code:

<apex:page standardController="Class_Enrollments__c" recordSetVar="enrollments" extensions="AddingEnrollmentsController">

    <apex:form >

        <apex:variable var="rowNum" value="{!0}"  />

        <apex:pageBlock >

            <apex:variable var="rowNum" value="{!0}"  />  

            <apex:pageBlockTable value="{!Class_EnrollmentsList}" var="CE">

                <apex:facet name="footer">

                    <apex:commandLink value="Add" action="{!insertRow}"/>

                </apex:facet>

                <apex:column headerValue="Youth Name">

                    <apex:inputField value="{!CE.Youth_Name__c}"/>

                </apex:column>

                <apex:column headerValue="Works Wonders Record">

                    <apex:inputField value="{!CE.Works_Wonders_Youth_Name__c}"/>

                </apex:column>

                <apex:column headerValue="Attendance">

                    <apex:inputField value="{!CE.Attendance__c}"/>

                </apex:column>

                 <apex:column headerValue="Participation Note">

                    <apex:inputField value="{!CE.Participation_Note__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="{!insertClass_Enrollments}"/>

            </apex:pageBlockButtons>

        </apex:pageBlock>

    </apex:form>

</apex:page>

Controller extension:

public class AddingEnrollmentsController {

    Id SessionId;

    public List<Class_Enrollments__c> class_enrollmentsList {get;set;}

    public Integer rowNum{get;set;}

    

    public AddingEnrollmentsController(ApexPages.StandardSetController controller){

       Id enrollmentsId = ApexPages.currentPage().getParameters().get('enrollmentsId');

       SessionId=ApexPages.currentPage().getParameters().get('SessionId');

       class_enrollmentsList = new List<Class_Enrollments__c>();  

       class_enrollmentsList.add(new Class_Enrollments__c());      

    }

    

    public pagereference insertClass_Enrollments(){

    insert class_enrollmentsList;

    Pagereference page=new pagereference('/'+SessionId);

    Return page;

}

    

    public void insertRow(){

        class_enrollmentsList.add(new Class_Enrollments__c()); 

    }

    public void delRow(){

        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));

        class_enrollmentsList.remove(rowNum);   

    }

}

1) Can anyone offer me tips on how to modify that code to successfully pass the parent record ID to the child records? 

2) If you add a second row and decide to remove it, the "X" to delete the row is not working.

Thank you!

Blair
2 respuestas
  1. 16 nov 2019, 15:09
    Hi,

    Thank you, but I cannot get this to work for me, in my code. My understanding was these two pieces of code should pass the parent ID to the child record:

           SessionId=ApexPages.currentPage().getParameters().get('class_enrollments.SessionId');

    And then:

        Pagereference page=new pagereference('/'+SessionId);

    However, I continue to get this error:

    System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Session]: [Session]

    Error is in expression '{!insertClass_Enrollments}' in component <apex:commandButton> in page add_class_enrollments: Class.AddingEnrollmentsController.insertClass_Enrollments: line 14, column 1

    Class.AddingEnrollmentsController.insertClass_Enrollments: line 14, column 1

    So, something is wrong with this section of code:

        public AddingEnrollmentsController(ApexPages.StandardSetController controller){

           SessionId=ApexPages.currentPage().getParameters().get('class_enrollmentsList.SessionId');

           class_enrollmentsList = new List<Class_Enrollments__c>();  

           class_enrollmentsList.add(new Class_Enrollments__c());

        }

        

        public pagereference insertClass_Enrollments(){

        insert  class_enrollmentsList ;

        Pagereference page=new pagereference('/'+SessionId);

        Return page;

    }

        

    Any thoughts? Additionally, I have been trying all morning to assign all newly created child records the appropriate record type ID. I want them all to have the same record type, so I just need to assign that on record creation. I have tried to add various types of code, including "Id recordTypeId = Schema.SObjectType.OBJECT_NAME.getRecordTypeInfosByDeveloperName() .get('RECORD_TYPE_DEVELOPER_NAME').getRecordTypeId();" and simply, "

    class_enrollmentsList.RecordTypeId = recordTypeId (with the actual record type ID);

    but that is throwing me an error too.

    I updated my VF page so that i am using the standard controller for the parent object, instead. I think one error was using the Child object. My list button directs to this URL: 

    /apex/Add_Class_Enrollments?SessionId={!Class_Enrollments__c.SessionId__c}

    I feel like I'm soooo close. Any help?
0/9000