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
Ranu JainRanu Jain 

Master to child query



I have an master object 'Account' and its child 'Agreement ' .

This child object have a picklist field Agg category which have following values - Data,multiple offering.


I want to run a query on Account and want to retrieve tagg with its one child (limit 1 in child query).


but requirement is if there is 'Data' category available than it should be retrieved and if 'Data' is not available than we will retrieve 'multiple offering'.

 Is this possible by writing only one SOQl query?




Hi Ranu Jain


I have written a code for you please try this and modify its query limit according your requirment I have written a generic code means bulkified.

list<Account> objAccountSoql = [SELECT Id,(SELECT Id,Agg__c FROM Agreements__r WHERE Agg__c!=null) FROM ACCOUNT];
for(Account acc:objAccountSoql){
	for(Agreement__c agg:acc.Agreements__r){
		if(agg.Agg__c == 'multiple offering'){
			//Create logic for "multiple offering" value 
		}else if(agg.Agg__c == 'Data'){
			//Create logic for "Data" value 



Mohammad Usman


Ranu JainRanu Jain

Hi Mousman,



Thank you very much for your efforts.


But  that's can not be the solution because I don't have only 'Data' ,Their can be 'Data','Services'. These are not fixed.

I posted this one to  search a way of doing this by query only. Is this possible?