0

I have a page with three sections. Each section holds a table representing a custom object (one table per object). The requirement is that for each section, there must have a checkbox for selecting/unselecting all item checkboxes. All items selected on that page must be saved in just one object (This is another object to gather/summarize all the items selected).

Select All Checkbox function for each section is working, my only problem is that only one of the selected records is inserted successfully.

Here are the controller and the vf page:

Controller: EnrollmentCardCX4

public without sharing class EnrollmentCardCX4 {

    private ApexPages.StandardController enrollmentController;
    public Enrollment_Form__c enrollmentRecord {get;set;}
    public Parent__c parentqry {get;set;}
    //public Parent__c parentclone {get;set;}
    public Boolean Page1 {get;set;}
    public Boolean Page2 {get;set;}
    public Boolean ShowTable {get;set;} 
    public Boolean ShowParent {get;set;}
    public List<resultWrapper> wrapperList {get;set;} 
    public List<otherWrapper> wrapperoList {get;set;} 
    public List<newWrapper> wrapper2List {get;set;} 
    public List<Enrollment_Form__c> enrollment {get;set;}
    public Enrollment_Form__c NewEnrollment {get;set;}
    public Parent__c parentclone = new Parent__c();

    public EnrollmentCardCX4(ApexPages.StandardController controller) {
        enrollmentController = controller;
        Page1 = true;
        Page2 = false;
        ShowTable = false;
        ShowParent = false;
        wrapperList = new List<resultWrapper>();
        wrapperoList = new List<otherWrapper>();
        wrapper2List = new List<newWrapper>();
        enrollmentRecord = new Enrollment_Form__c();
        parentqry = new Parent__c();
        parentclone = new Parent__c();
        enrollment = new List<Enrollment_Form__c>();
        //NewEnrollment = new Enrollment_Form__c();

    }

    public void initializevalues() {

        ShowTable = true;

        List<Student__c> students = new List<Student__c>();
        List<Other_Children__c> others = new List<Other_Children__c>();
        List<Fetcher__c> fetchers = new List<Fetcher__c>();
        List<resultWrapper> tempwrapper = new List<resultWrapper>();
        List<otherWrapper> tempowrapper = new List<otherWrapper>();
        List<newWrapper> tempo2wrapper = new List<newWrapper>();

        if(enrollmentRecord.Parent_Guardian1__c != null) {
            parentqry = [SELECT ID, Name, First_Name__c, Middle_Name__c, Last_Name__c, Address__c, Cell_Phone__c, Email__c, Employer_Organization__c, Home_Phone__c, Position__c, Salutation__c, Other_Salutation__c, Work_Phone__c FROM Parent__c WHERE ID =: enrollmentRecord.Parent_Guardian1__c];
            ShowParent = true;
            parentclone = parentqry.Clone(true,true,true,true);
        }

        if(enrollmentRecord.Parent_Guardian1__c == null) {
            wrapperList.clear();
            wrapperoList.clear();
            wrapper2List.clear();
        }

        if(enrollmentRecord.Parent_Guardian1__c != null) {

            students = [SELECT ID, Student_Name__c, First_Name__c, Middle_Name__c, Last_Name__c, Name, Age__c, Date_of_Birth__c, Grade_Level__c, Father_s_Name__c, Mother_s_Name__c, Parent_Father__c, Parent_Mother__c FROM Student__c WHERE Parent_Father__c =: enrollmentRecord.Parent_Guardian1__c];

            if(students.size() <= 0) {
                students = [SELECT ID, Student_Name__c, First_Name__c, Middle_Name__c, Last_Name__c, Name, Age__c, Date_of_Birth__c, Grade_Level__c, Father_s_Name__c, Mother_s_Name__c, Parent_Father__c, Parent_Mother__c FROM Student__c WHERE Parent_Mother__c =: enrollmentRecord.Parent_Guardian1__c];
            }

            others = [SELECT ID, Name, Father__c, Mother__c, Date_Of_Birth__c FROM Other_Children__c WHERE Father__c =: enrollmentRecord.Parent_Guardian1__c];

            if(others.size() <= 0) {
                others = [SELECT ID, Name, Father__c, Mother__c, Date_Of_Birth__c FROM Other_Children__c WHERE Mother__c =: enrollmentRecord.Parent_Guardian1__c];
            }

            fetchers = [SELECT ID, Name, Fetcher_Name__c, First_Name__c, Middle_Name__c, Last_Name__c, Employer_Student_s_Father__c, Employer_Student_s_Mother__c FROM Fetcher__c WHERE Employer_Student_s_Father__c =: enrollmentRecord.Parent_Guardian1__c];

            if(fetchers.size() <= 0) {
                fetchers = [SELECT ID, Name, Fetcher_Name__c, First_Name__c, Middle_Name__c, Last_Name__c, Employer_Student_s_Father__c, Employer_Student_s_Mother__c FROM Fetcher__c WHERE Employer_Student_s_Mother__c =: enrollmentRecord.Parent_Guardian1__c];
            }

            for(Student__c stloop : students) {
                tempwrapper.add(new resultWrapper(false,stloop));
            }

            for(Other_Children__c ocloop : others) {
                tempowrapper.add(new otherWrapper(false,ocloop));
            }

            for(Fetcher__c ftloop : fetchers) {
                tempo2wrapper.add(new newWrapper(false,ftloop));
            }

            if(tempwrapper != null) {
                wrapperList = tempwrapper;
            }
            if(tempowrapper != null) {
                wrapperoList = tempowrapper;
            }
            if(tempo2wrapper != null) {
                wrapper2List = tempo2wrapper;
            }
        }
    }

    public void proceed() {

    List<Enrollment_Form__c> enrollmentList = new List<Enrollment_Form__c>();
    Enrollment_Form__c NewEnrollment = new Enrollment_Form__c();

    Set<id> newIds = new Set<id>();
        system.debug('parentqry v parentclone >> ' + parentqry + ' >> ' + parentclone);

        if(parentclone.Salutation__c != parentqry.Salutation__c || parentclone.Employer_Organization__c != parentqry.Employer_Organization__c ||
           parentclone.First_Name__c != parentqry.First_Name__c || parentclone.Cell_Phone__c != parentqry.Cell_Phone__c || 
           parentclone.Middle_Name__c != parentqry.Middle_Name__c || parentclone.Home_Phone__c != parentqry.Home_Phone__c || 
           parentclone.Last_Name__c != parentqry.Last_Name__c || parentclone.Email__c != parentqry.Email__c || 
           parentclone.Address__c != parentqry.Address__c || parentclone.Work_Phone__c != parentqry.Work_Phone__c) {
           system.debug('before update parentqry >> ' + parentqry);
           update parentqry;
           system.debug('after update parentqry >> ' + parentqry);

        }
        for(resultWrapper rwloop : wrapperList) {
            if(rwloop.IsSelected) {
                //Enrollment_Form__c NewEnrollment = new Enrollment_Form__c();
                NewEnrollment.Student__c = rwloop.studentlist.Id;
                if(rwloop.studentlist.Parent_Father__c != null) {
                    NewEnrollment.Parent_Guardian1__c = rwloop.studentlist.Parent_Father__c;
                }
                if(rwloop.studentlist.Parent_Mother__c != null) {

                    NewEnrollment.Parent_Guardian2__c = rwloop.studentlist.Parent_Mother__c;
                }
                enrollmentList.add(NewEnrollment);

            }
        }
        for(otherWrapper owloop : wrapperoList) {
            if(owloop.IsSelected) {
                //Enrollment_Form__c NewEnrollment = new Enrollment_Form__c();
                NewEnrollment.New_Student_Enrollee__c = owloop.otherlist.Id;
                if(owloop.otherlist.Father__c != null) {
                    NewEnrollment.Parent_Guardian1__c = owloop.otherlist.Father__c;
                }
                if(owloop.otherlist.Mother__c != null) {
                    NewEnrollment.Parent_Guardian2__c = owloop.otherlist.Mother__c;
                }
                enrollmentList.add(NewEnrollment);
            }
        }

        for(newWrapper ftloop : wrapper2List) {
            if(ftloop.IsSelected) {
                //Enrollment_Form__c NewEnrollment = new Enrollment_Form__c();
                NewEnrollment.Fetcher__c = ftloop.fetcherList.Id;
                if(ftloop.fetcherList.Employer_Student_s_Father__c != null) {
                    NewEnrollment.Parent_Guardian1__c = ftloop.fetcherList.Employer_Student_s_Father__c;
                }
                if(ftloop.fetcherList.Employer_Student_s_Mother__c != null) {
                    NewEnrollment.Parent_Guardian2__c = ftloop.fetcherList.Employer_Student_s_Mother__c;
                }
                enrollmentList.add(NewEnrollment);

            }
        }




        if(enrollmentList.size() > 0) {

            insert NewEnrollment;


            Set<id> ids = new Set<id>();

            for(Enrollment_Form__c idloop : enrollmentList) {
                ids.add(idloop.id);
            }

            List<Enrollment_Form__c> EFNqry = [SELECT ID, Name, Student_Name__c, Sibling_Name__c, Fetcher_Name__c FROM Enrollment_Form__c WHERE ID IN : ids];
            system.debug('EFNqry >> ' + EFNqry);

            enrollment = EFNqry;

        }

        Page1 = False;
        Page2 = True;

        sendPdf(enrollment);

    }

    public PageReference sendPdf(List<Enrollment_Form__c> elist) {

        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        for(Enrollment_Form__c ord : elist) {   
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            attach.setContentType('application/pdf');
            attach.setFileName('Enrollment Information.pdf');

            String body;

            body = '<html><body><center><table align="center"><tr><td><img src="https://sampledomain2-dev-ed--c.ap2.content.force.com/servlet/servlet.ImageServer?id=01528000000ppJo&oid=00D280000011MVx&lastMod=1458195004000" width="50" height="50"/></tr></td></table></center><br/><br/><h1 style=\"text-align:center;\">Enrollment Information</h1><br/><br/><table align=\"center\"><tr><td>Enrollment Number</td><td>' + ord.Name + '</td></tr><tr><td>Student Name</td><td>' + ord.Student_Name__c + '</td></tr><tr><td>Other Child(ren)</td><td>' + ord.Sibling_Name__c + '</td></tr><tr><td>Fetcher(s)</td><td>' + ord.Fetcher_Name__c + '</td></tr></table></body></html>';
            System.debug('HTML is ' + body);

            attach.Body = Blob.toPDF(body);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId('00528000001EPYy');
            mail.setSubject('PDF Generation');
            mail.saveAsActivity = false;
            mail.setHtmlBody('PFA');
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });     

            mails.add(mail);  
        }
        if(!mails.isEmpty()) {
            Messaging.SendEmail(mails);
        }

        return null;
    }



    public class resultWrapper {

        public Student__c studentlist {get;set;}
        public Boolean IsSelected {get;set;}

        public resultWrapper(Boolean is, Student__c st) {
            IsSelected = is;
            studentlist = st;
        }
    }
    public class otherWrapper {

        public Other_Children__c otherlist {get;set;} 
        public Boolean IsSelected {get;set;}

        public otherWrapper(Boolean is, Other_Children__c ol) {
            IsSelected = is;
            otherlist = ol;
        }
    }
    public class newWrapper {
        public Fetcher__c fetcherList {get;set;}
        public Boolean IsSelected {get;set;}

        public newWrapper(Boolean is, Fetcher__c ft) {
            IsSelected = is;
            fetcherList = ft;
        }
    }
}

VF Page: EnrollmentCardPage4

<apex:page standardController="Enrollment_Form__c" extensions="EnrollmentCardCX4" sidebar="false" showHeader="false">

    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID) {
            var inputCheckbox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckbox.length; i++) {
                if(inputCheckbox[i].id.indexOf(receivedInputID)!=-1) {
                    inputCheckbox[i].checked = obj.checked;
                }
            }
        }

        function selectAllCheckboxes2(obj,receivedInputID) {
            var inputCheckbox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckbox.length; i++) {
                if(inputCheckbox[i].id.indexOf(receivedInputID)!=-1) {
                    inputCheckbox[i].checked = obj.checked;
                }
            }
        }

        function selectAllCheckboxes3(obj,receivedInputID) {
            var inputCheckbox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckbox.length; i++) {
                if(inputCheckbox[i].id.indexOf(receivedInputID)!=-1) {
                    inputCheckbox[i].checked = obj.checked;
                }
            }
        }
    </script>

     <apex:form id="theForm" rendered="{!Page1}">

        <div id="divblock">
            <apex:pageBlock id="block1">
                <apex:Messages style="color: red;"/>
                <apex:pageBlockSection id="showMsg">
                    <apex:inputField value="{!enrollmentRecord.Parent_Guardian1__c}">
                        <apex:actionSupport action="{!initializevalues}" event="onchange" />
                    </apex:inputField>
                </apex:pageBlockSection>
                <apex:pageblockSection id="parentinfo" rendered="{!ShowParent}">
                    <apex:inputField value="{!parentqry.Salutation__c}"/>
                    <apex:inputField value="{!parentqry.Employer_Organization__c}"/>
                    <apex:inputField value="{!parentqry.First_Name__c}"/>
                    <apex:inputField value="{!parentqry.Cell_Phone__c}"/>
                    <apex:inputField value="{!parentqry.Middle_Name__c}"/>
                    <apex:inputField value="{!parentqry.Home_Phone__c}"/>
                    <apex:inputField value="{!parentqry.Last_Name__c}"/>
                    <apex:inputField value="{!parentqry.Email__c}"/>
                    <apex:inputField value="{!parentqry.Address__c}"/>
                    <apex:inputField value="{!parentqry.Work_Phone__c}"/>
                </apex:pageblockSection>
                <apex:pageblockSection columns="1" id="pagenotification" rendered="{!ShowParent}">
                    <div id="divblock" align="center">    
                        <h5>Please verify the above information and make changes if necessary. &nbsp;</h5><br/>
                        <h5>All changes will be saved after clicking PROCEED</h5>
                    </div>
                </apex:pageblockSection>
            </apex:pageBlock>
        </div>
        <div id="divblock" align="center">
            <apex:pageBlock id="block2" rendered="{!ShowTable}" >

                <apex:pageBlockSection id="student" collapsible="false" title="STUDENT(S) ENROLLING" columns="1">
                    <apex:pageBlockTable value="{!wrapperList}" var="s">
                        <apex:column width="2" >
                            <apex:facet name="header">
                                <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId2')"/>
                            </apex:facet>
                            <apex:inputCheckbox value="{!s.IsSelected}" id="inputId2"/>
                        </apex:column>
                        <apex:column headerValue="Full Name" value="{!s.studentlist.Student_Name__c}" />
                        <apex:column headerValue="Age" value="{!s.studentlist.Age__c}" />
                        <apex:column headerValue="Grade Level" value="{!s.studentlist.Grade_Level__c}" />
                        <apex:column headerValue="Parent(Father)" value="{!s.studentlist.Father_s_Name__c}" />
                        <apex:column headerValue="Parent(Mother)" value="{!s.studentlist.Mother_s_Name__c}" />

                    </apex:pageBlockTable>
                </apex:pageBlockSection>
                <apex:pageBlockSection id="other" collapsible="false" title="OTHER CHILD(REN)" columns="1">
                    <apex:pageBlockTable value="{!wrapperoList}" var="s">
                        <apex:column width="2" >
                            <apex:facet name="header">
                                <apex:inputCheckbox onclick="selectAllCheckboxes2(this,'inputId3')"/>
                            </apex:facet>
                            <apex:inputCheckbox value="{!s.IsSelected}" id="inputId3"/>
                        </apex:column>
                        <apex:column headerValue="Full Name" value="{!s.otherlist.Name}" />
                        <apex:column headerValue="Date of Birth" value="{!s.otherlist.Date_of_Birth__c}" />

                    </apex:pageBlockTable>
                </apex:pageBlockSection>
                <apex:pageBlockSection id="fetcher" collapsible="false" title="FETCHER(S)" columns="1">
                    <apex:pageBlockTable value="{!wrapper2List}" var="s">
                        <apex:column width="2">
                            <apex:facet name="header">
                                <apex:inputCheckbox onclick="selectAllCheckboxes3(this,'inputId4')"/>
                            </apex:facet>
                            <apex:inputCheckbox value="{!s.IsSelected}" id="inputId4"/>
                        </apex:column>
                        <apex:column headerValue="First Name" value="{!s.fetcherList.First_Name__c}"/>
                        <apex:column headerValue="Last Name" value="{!s.fetcherList.Last_Name__c}"/>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>

            <apex:commandButton action="{!proceed}" title="Proceed" value="Proceed" />
            </apex:pageBlock>
        </div>


    </apex:form>
    <apex:form id="resultForm" rendered="{!Page2}">
        <div id="divresult">
            <apex:pageBlock >
                <p>You have now successfully accomplished the pre-enrollment form</p>
                <p>This is your reference number for each student being enrolled: </p>

                <apex:pageBlockTable id="results" value="{!enrollment}" var="e">
                    <apex:column styleClass="rescolumn" headerValue="Name" value="{!e.Student_Name__c}"/>
                    <apex:column styleClass="rescolumn" headerValue="Reference Number" value="{!e.Name}"/>
                </apex:pageBlockTable>


            </apex:pageBlock>
        </div>

    </apex:form>
</apex:page> 

Any help would be appreciated. Thanks in advance.

  • Your code could use some optimization, as well, consider reviewing your code carefully. – sfdcfox Apr 6 '16 at 3:03
1
insert NewEnrollment;

should be

insert enrollmentList;

You could have avoided making this mistake by putting the temporary variable, NewEnrollment, inside the loop to avoid scope creep. In fact, you did at one point do so, but then commented it out.

  • Every time I try inserting enrollmentList, there's this error: "Before Insert or Upsert list must not have two identically equal elements". Also, the reason why I've commented out temporary variable NewEnrollment for every loop is that this causes the creation of new records, getting only the parts included on that loop, instead of saving all the selected items in just one record – lol Apr 6 '16 at 3:13
  • @lol That error makes sense, though, because you're actually just adding the same record to the list over and over again. Only add the record to the list once, if that's your intent, but since you're apparently allowing multi-select, values are still going to be lost. You may need to rethink your entire UI, database, and code design. I'm not sure if anyone can get it all done in one answer, but take your time and I'm sure you'll get there. – sfdcfox Apr 6 '16 at 3:22
  • I'll try of thinking other approach for this. Thanks for your help. – lol Apr 6 '16 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.