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
Krishna_225Krishna_225 

dynamically call child records when parent is searched via lookup

I have a requirement where the child object i.e student__c should be displayed when parent object i.e class__c is searched via lookup. Below is my code.
public class AttendenceController {
    public Class__C selectedUser { get; set; }
    public student__c stu {get; set;}

    public class StudentWrapper {   
        public Student__c student {get; set;} 
        public Boolean absent {get; set;}

        public StudentWrapper() {
            this.absent = false;
        }

        public StudentWrapper(Student__c student) {
            this();
            this.student = student;
        }
    }

    public List<StudentWrapper> students {get; set;}

    Date day = Date.today();

    public AttendenceController() {
        students = new List<StudentWrapper>();

        for (Student__c student : [select Name, Class__c from Student__c WHERE Class__C!= Null]) {
            students.add(new StudentWrapper(student));
        }
    } 

    public PageReference submit() {
        List<Attendance_Tracker__c> attendance = new List<Attendance_Tracker__c>();

        for (StudentWrapper wrapper : this.students) {
            attendance.add(new Attendance_Tracker__c(
                Date__c = this.day,
                Student__c = wrapper.student.Id,
                Present_Today__c = wrapper.absent
            ));
        }

        if (!attendance.isEmpty()) {
            insert attendance;
        }

        return null;
    }

}

and visualforce page
<apex:page controller="AttendenceController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!students}" var="wrapper">
            <apex:column value="{!wrapper.student.Name}" />
            <apex:column >
                <apex:facet name="header">Present Today</apex:facet>
                <apex:inputCheckbox value="{!wrapper.absent}" />
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:commandButton action="{!submit}" value="Submit" onComplete="window.location.reload();"/>
</apex:form>

when a class is searched from lookup, the related students under the class should show in the page and should be saved after the submission. But as of now, I can only able to display all the students irrespective of class. Code is working fine but I need an extra option of adding the lookup


Any help would be highly helpful.
Thankyou in advance
Krish