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
Bill WoodsonBill Woodson 

query related list of parent object of custom object

Hello,

I need to get a list of custom objects from a parent object of a custom object using soql.  The parent object is the standard Contact record

Ex.

SpecialInfo__c.Contact_Record__r.CustomNotes__r

psuedo code:

for each CustomNote
Get [some CustomNotes fields]  From CustomeNotes
where Contact_Record.parentid = SpecialInfo.id

Thanks!

 
Best Answer chosen by Bill Woodson
SonamSonam (Salesforce Developers) 
Hi Bill, In that case you can first try to run an SOQL to get the Contact.id frrom the SpecialInfo__c using the ID being passed, like :

SpecialInfo__c Sinfo = new​ SpecialInfo__c([Select Contact__c From SpecialInfo__c
where ID =: SpecialInfo__c.id]);

and then use the following:

Select [some CustomNotes fields]  From CustomeNotes
where Contact__c =: Sinfo.Contact__c

Hope I've understood the schema correctly and this helps!

 

All Answers

SonamSonam (Salesforce Developers) 
As per your psuedo code : CustomNotes has Contact field and SpecialInfo__c also has the same..right?

If that is true, you can write:
Get [some CustomNotes fields]  From CustomeNotes
where Contact__c =: SpecialInfo__c.Contact__c
provided you already have the value of SpecialInfo__c.Contact__c
Bill WoodsonBill Woodson
yes both have a same, but what is passed into the class is the SpecialInfo__c.id so some how I need to get the proper Contact.id based on the SpecialInfo__c.id.
SonamSonam (Salesforce Developers) 
Hi Bill, In that case you can first try to run an SOQL to get the Contact.id frrom the SpecialInfo__c using the ID being passed, like :

SpecialInfo__c Sinfo = new​ SpecialInfo__c([Select Contact__c From SpecialInfo__c
where ID =: SpecialInfo__c.id]);

and then use the following:

Select [some CustomNotes fields]  From CustomeNotes
where Contact__c =: Sinfo.Contact__c

Hope I've understood the schema correctly and this helps!

 
This was selected as the best answer
Bill WoodsonBill Woodson
So here is the code I am attempting...

for(CustomNote__c note_record:[select id,
                                   CreatedBy.Name,Body__c,LastModifiedDate
                                   from CustomeNote__c
                                   where Contact__r =: SpecialInfo__c.Contact_Record__c
                                   and SpecialInfo__c.id =: Apexpages.currentPage().getParameters().get('id') ])
Bill WoodsonBill Woodson
So I have modified my code to this and this line is giving me an expect semi-colon error, but I cannot figure out what type-o exists...

        SpecialInfo__c spInfo = new​ SpecialInfo__c([select Contact_Record__c 
                                                from SpecialInfo__c 
                                                where id =: Apexpages.currentPage().getParameters().get('id')]);
Bill WoodsonBill Woodson
I think I figured it out, I was thinking I could set the contact record property as part of the construction of a new object, but instead I just needed to set the object equal to the query result.

Thanks for all your help!