4

This is my SOQL

SELECT ID,NAME,
 (SELECT ID,JUNCTION_TEXT__C FROM SPENDACCOUNT__C.CASE_SPEND_ACCOUNTS__R) 
FROM SPENDACCOUNT__C

SpendAccount__c is the Master.

Case_Spend_Account__c is the Child.

When I execute the following SOQL in Query Editor I am able to view the results (like shown below)

enter image description here

I was curious as to how I will be able to view the aggregated result (JUNCTION_TEXT__C coming from the child) and when I did the following anon apex I could not view the aggregated part at all.

My anon apex shown below.

enter image description here

Debug log output

enter image description here

What am I missing ?..can someone help ?

1
  • You do not have any SOQL aggregation in you code. Are you trying to use the Master-Detail Roll Up Summary functionality? May 10 '15 at 22:56
4

The subquery will return a list of "Case_Spend_Account__c" records under each "SPENDACCOUNT__C" record. If any "SPENDACCOUNT__C" does not have child records, "CASE_SPEND_ACCOUNTS__R" will be an empty list. In order to use the child records, you need to iterate over the child list.

       for(SPENDACCOUNT__C parent:[SELECT ID,NAME,(SELECT ID,JUNCTION_TEXT__C FROM SPENDACCOUNT__C.CASE_SPEND_ACCOUNTS__R) FROM SPENDACCOUNT__C]){
            //Since subquery is returning a list, if you want to check if child list is empty.
            if(!parent.SPENDACCOUNT__C.CASE_SPEND_ACCOUNTS__R.isEmpty()){
                for(Case_Spend_Account__c child:parent.SPENDACCOUNT__C.CASE_SPEND_ACCOUNTS__R){
                    System.debug(child);
                }
            }else{
                System.debug('No Child');
            }
        }

Please check the link for details - https://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm

1
  • I think OP wants to know why the same query done in the QueryEditor versus anonymous apex delivers different display results when comparing query editor output to system.debug output
    – cropredy
    May 10 '15 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.