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
BlairBBlairB 

Need help with passing parent ID to child record and assigning record type

Hi,

I am attempting to create a visualforce page that will be accessed from a button on a related list. This button will allow users to add multiple child records related to the parent record. 

The parent object is Sessions__c and the child object is Class_Enrollments__c. I want users to be able to click the button and be routed to a VF page where they can add child records on one page.

I am running into three issues:

1) Whenever I try to save the record, I get the following 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, the parent record ID is not being passed to the child records.

2) I want to assign the record type ID to new child records. Child records will have the same record type for this particular page/button.

3) The "Delete" row function is not working

Below is my controller extension:

User-added image
VF page:

User-added image
Finally, all of this is being accessed via a list button that has the following URL:

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

Help appreciated!

 
BlairBBlairB
I thought pictures would be more helpful, but apparently not. Here are the codes:

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){
       SessionId=ApexPages.currentPage().getParameters().get('class_enrollments.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);   
    }
}

VF Page:

<apex:page standardController="Sessions__c" recordSetVar="session" 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>