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
sfdc Beginnersfdc Beginner 

How to Query the child records of parent object on which trigger is running.

How to Query the child records of parent object on which trigger is running and fetch the respective child records of the enitity.
Best Answer chosen by sfdc Beginner
Shyama B SShyama B S

Hi sfdc Beginner,

You can use the below format for standard objects:
select id, (select id from childobjects) from parentobject
Which gives you a list of parent objects with their corresponding child objects. You should use the plural of the child object (childobject becomes childobjects).

For custom objects, there is a small change in the query :
select id, (select id from childobjects__r) from parentobject__c
where childobjects__r is the relationship name of childobject__c custom object and parentobject__c is the name of the custom parent object.

Please let me know if this helped you.

Thanks,
Shyama

All Answers

Shyama B SShyama B S

Hi sfdc Beginner,

You can use the below format for standard objects:
select id, (select id from childobjects) from parentobject
Which gives you a list of parent objects with their corresponding child objects. You should use the plural of the child object (childobject becomes childobjects).

For custom objects, there is a small change in the query :
select id, (select id from childobjects__r) from parentobject__c
where childobjects__r is the relationship name of childobject__c custom object and parentobject__c is the name of the custom parent object.

Please let me know if this helped you.

Thanks,
Shyama
This was selected as the best answer
sfdc Beginnersfdc Beginner
Thanks Shyama...