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
shweta kumari 25shweta kumari 25 

custom object relationship

I have custom object relationship as follows:

Course__c is a Master-Detail to Course_Delivery__c ; so Course__c is a Master and Course_Delivery__c  is child object

Course_Attendee__c is a Master-detail to Course_Delivery__c ; so Course_Attendee__c is a Master and Course_Delivery__c  is child object.

Course__c has fields like : location , startdate , instructor

records exists on all custom objects.

I have written a upward traversal soql as follows:

List<Course_Delivery__c> lsCourse_delivery=[select ID,
                                            Instructor__c,
                                            Start_Date__c,
                                            Course_Delivery__c.Course_Attendee__r.InstructorNotes__c,
                                            Course_Delivery__c.Course__r.Name
                                            from 
                                            Course_Delivery__c
                                            Order By Location__c];
system.debug(lsCourse_delivery);

I am getting an error as :  Course_Delivery__c.Course_Attendee__r.InstructorNotes__c ^ ERROR at Row:4:Column:45 Didn't understand relationship 'Course_Attendee__r' in field path.

Please help me

shweta
Sampath SuranjiSampath Suranji
Hi,
Please make sure that API name of the 'Course Attendee' master detils field in the 'Course Delivery' object is 'Course_Attendee__c'.
If not, please use the exact API with replacing __c with __r.

regards
Sampath
shweta kumari 25shweta kumari 25
Hi sampath
Let me  give more clear picture.

In course_delivery__c  : Under Custom Fields & Relationships  ; I have as follows

 Attendee Count      Attendee_Count__c    Roll-Up Summary (COUNT Course Attendee)

In Course_Attendee__c : Under Custom Fields & Relationships  ; I have as follows

Course Delivery     Course_Delivery__c      Master-Detail(Course Delivery)

But the query I posted throws error, I missing something?

shweta
Sampath SuranjiSampath Suranji
Hi,

I'm little bit confused. Initially you have mentioned that,
Course__c is a Master-Detail to Course_Delivery__c and Course_Attendee__c is a Master-detail to Course_Delivery__c
then Course__c  and Course_Attendee__c  should be parents and Course_Delivery__c should be the child.
Then Roll-Up Summary fields should be inside the parents.
It is better if you can share the screens of Custom Fields & Relationships section of each objects.

 
shweta kumari 25shweta kumari 25

Hello 

I have attached screen shots for each custom object in question.

Course__c

Course__c
Course_Attendee__c

Course_Attendee__c

Course_Delivery__c

Course_Delivery__c
Sampath SuranjiSampath Suranji
Hi,
You cannot select 'InstructorNotes__c' like Course_Delivery__c.Course_Attendee__r.InstructorNotes__c
because you don't have a lookup or master detail relationship to Course_Attendee__C objetc in Course_Delivery__c.
If you want to get the InstructorNotes__c field also with the fields in Course_Delivery__c I suggest you to write a wrapper class.

regards
 
shweta kumari 25shweta kumari 25
Ok I understood, I missed  that.

Cud u pls show me sample how the wrapper class would be if I were to access the field InstructorNotes__c also from the course_delivery__c object?

shweta
Sampath SuranjiSampath Suranji
Hi,
Since one Course_Delivery__c can have linked with many Course_Attendee__c,  when you select fields from  Course_Delivery__c, you may get many Course_Attendee__c(InstructorNotes__c) records for a given  Course_Delivery__c  record. So please let me know how you are going to use this records for VF page or some where else?
shweta kumari 25shweta kumari 25
Hi

yes,  vf page to display records of course_delivery__c and also instructornotes__c

 
Sampath SuranjiSampath Suranji
Hi,
You can try like below,
public class devForum7 {
    
    public List<courseWrapper>courseWrapperList{get;set;}
    
    public devForum7(){
        getData();
    }
    
    public void getData(){
        List<Id>cDeliveryIds= new List<id>();
        courseWrapperList = new List<courseWrapper>();
        Map<id,LIST<Course_Attendee__c>> caMap= new Map<id,LIST<Course_Attendee__c>>();
        List<Course_Delivery__c> cDeliveryList=[select ID,name, Start_Date__c,Instructor__c,  Course_Delivery__c.Course__r.Name
                                                from Course_Delivery__c];
        for(Course_Delivery__c cd:cDeliveryList){
            cDeliveryIds.add(cd.Id);
        }
        List<Course_Attendee__c>cAttendeeList = [select Course_Delivery__c,InstructorNotes__c  from Course_Attendee__c where Course_Delivery__c in :cDeliveryIds];
        for(Course_Attendee__c ca:cAttendeeList){
            if(caMap.containsKey(ca.Course_Delivery__c)){
                List<Course_Attendee__c> cdList= caMap.get(ca.Course_Delivery__c);
                cdList.add(ca);
                caMap.put(ca.Course_Delivery__c,cdList);  
            }
            else{
                caMap.put(ca.Course_Delivery__c,new List<Course_Attendee__c>{ca});
            }
            
        }
        
        for(Course_Delivery__c cd:cDeliveryList){
            List<Course_Attendee__c> caList= new List<Course_Attendee__c>();
            if(caMap.containsKey(cd.Id)){
                caList =caMap.get(cd.Id);
            }
            courseWrapper objWrapper = new courseWrapper(cd.Id,cd.Name,cd.Instructor__c,string.valueOf(cd.Start_Date__c), caList,cd.Course__r.Name);
            courseWrapperList.add(objWrapper);
        }
        System.debug('courseWrapperList '+courseWrapperList);
        //you can access the courseWrapperList inside the VF page
    }
    
    public class courseWrapper{
        public Id courseDelId;
        public string courseDelName;
        public string instructor;
        public string startDate;
        public List<Course_Attendee__c> caList;
        public string courseName;
        
        public courseWrapper(Id courseDelId,string courseDelName,string instructor,string startDate,List<Course_Attendee__c> caList,string courseName ){
            this.courseDelId = courseDelId;
            this.courseDelName = courseDelName;
            this.instructor = instructor;
            this.startDate = startDate;
            this.caList = caList;
            this.courseName = courseName;
        }
        
    }
}

You can access the 'courseWrapperList' inside the VF page and need it to itterate.
<apex:page controller="devForum7"           >
    <apex:pageBlock>
    
        <apex:dataTable value="{!courseWrapperList}" var="c">
            <apex:column value="{!c.courseDelName}"  headerValue="course Name" />
            <apex:column value="{!c.instructor}"  headerValue="instructor" />
            <apex:column value="{!c.startDate}"  headerValue="start Date" />
            <apex:column  headerValue="Instructor Notes" >
                <apex:repeat value="{!c.caList}" var="note">
                {!note.InstructorNotes__c}
                <br/>
                </apex:repeat>
            </apex:column>
        </apex:dataTable>
        
    </apex:pageBlock>
    
</apex:page>

regards
Sampath