3

By using search function i need to fetch parent record and child record, the search function is happening in child record. Search function is fine But it is showing all child records in parent. this is my search code[part of]

soql1='Select Id,Name,Country__c,Logo_id__c,University_URL__c,Few_lines_about_University__c,City__c,I_20_Status_Offer_letter__c,No_of_course__c,('+soql+') from University__c where Country__c!=null';
try {
    Coursesearch= Database.query(soql1);
    WrapperList = New List<WrapperClassEx>();
    WrapperList1 = New List<WrapperClassEx1>();
    for ( University__c acc : Coursesearch ) {
        for ( Course_Detail__c Det : acc.Course_Detail__r ) {
            WrapperList1.add(New WrapperClassEx1(det));
        }
        WrapperList.add(New WrapperClassEx(acc,WrapperList1)); 
    }
} catch ( Exception e ) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}
return WrapperList;

This is My Wrapper Class

Public Class WrapperClassEx {
    Public University__c acc{get;set;}
    Public List<WrapperClassEx1> cd{get;set;}
    //Public Boolean selected{get;set;}

    Public WrapperClassEx(University__c accRec, List<WrapperClassEx1> courd) {
        this.acc = accRec;
        this.cd = courd;
        //this.selected= false;
    }
}

Public Class WrapperClassEx1 {
    Public Course_Detail__c acc1{get;set;}
    Public Boolean selected{get;set;}

    Public WrapperClassEx1(Course_Detail__c accRec1){
        this.acc1 = accRec1;
        this.selected= false;
    }
}
2
  • 3
    The normal pattern for this is to add one wrapper object per University__c (parent) to a list and add the Course_Detail__c objects (children) to a list within that wrapper.
    – Keith C
    Jun 25, 2014 at 13:56
  • Are you saying you don't want to get all the Child records?
    – Minutiae
    Jun 25, 2014 at 16:53

2 Answers 2

1

I'm not entirely clear what the problem is, but here is a generic way to offer a set of parent objects each with its own child objects:

public with sharing class SomeController {
    public University[] universities {
        get {
            // Lazy load being used here
            if (universities == null) {
                universities = new University[] {};
                String soql = ...;
                for (University__c sob : Database.query(soql)) {
                    universities.add(new University(sob));
                }
            }
            return universities;
        }
        private set;
    }
    // This inner class is the wrapper
    public class University {
        public Boolean selected {get; set;}
        public University__c university {get; private set;}
        public Course_Detail__c[] courseDetails (get; private set;}
        private University(University__c sob) {
            this.university = sob;
            this.courseDetails = sob.Course_Detail__r;
        }
    }
}

This is particularly simple as your query automatically returns a reference which is the list of the child objects so no further looping over the children is required.

1

You need to re declare WrapperList1 = New List<WrapperClassEx1>(); into first for loop. What it is exactly doing is, this is using a single list and adding all child into a same list. After changes, your code will look like

soql1='Select Id,Name,Country__c,Logo_id__c,University_URL__c,Few_lines_about_University__c,City__c,I_20_Status_Offer_letter__c,No_of_course__c,('+soql+') from University__c where Country__c!=null';
try {
    Coursesearch= Database.query(soql1);
    WrapperList = New List<WrapperClassEx>();
    for ( University__c acc : Coursesearch ) {
        WrapperList1 = New List<WrapperClassEx1>();
        for ( Course_Detail__c Det : acc.Course_Detail__r ) {
            WrapperList1.add(New WrapperClassEx1(det));
        }
        WrapperList.add(New WrapperClassEx(acc,WrapperList1)); 
    }
} catch ( Exception e ) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}
return WrapperList;

This will work fine, try it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .