2

Case Object has a lookup relationship to Parent_Project_if_applicable__c

The relationship name is Cases1.

I receive the error message:

Line: 15, Column: 28Id, AccountId, Account.Name, Cases1__r.Implementation_status__c ^ ERROR at Row:1:Column:38 Didn't understand relationship 'Cases1__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

My code

List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
                                         FROM Case
                                         WHERE CreatedDate = LAST_N_DAYS:5 
                                         GROUP BY AccountId, Account.Name
                                         HAVING COUNT(Id)  >= 1];

                         Set<Id> AcctIds = new Set<Id>();
                for(AggregateResult aggr: AggregateResultList){

                    Id accId = (Id)aggr.get('AccountId');
                        AcctIds.add(accId);
                }


                List<Case> caseList = [SELECT Id, AccountId, Account.Name,  Cases1__r.Implementation_status__c,
                Cases1__r.Client_Advisor_Email__c FROM Case WHERE AccountId IN :AcctIds];

            //System.debug('This is CaseList ' + caseList);


            for(Case cl:caseList){ 

             //   System.debug('Implementation Status: ' + (String)cl.get('Cases1__r.Implementation_status__c'));
               // System.debug('cl: '+ cl);


            }

Case Object

Here is the Parent_Project_if_applicable__c

2

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm

You've added a lookup field on the Case object. Creating a lookup field is always done on the child object.

Looking at the documentation provided above, you can see the difference in how you go about the relationship in SOQL (child-to-parent vs. parent-to-child).

enter image description here

To figure out your relationship name, I'd use workbench. Select Info --> Standard & Custom Object. Select Your "Case" object. Go into the custom object under fields (Mileston1_Project__c, select it, and then note the relationship name). Use this with __r appended as shown below.

enter image description here

List<Case> caseList = [SELECT Id, AccountId, Milestone1_Project__r.Implementation_status__c FROM Case WHERE AccountId in: AcctIds];
1

That's not how this type of join works. You need to include a subquery using that name in its FROM clause:

SELECT Fields__c, (SELECT SubFields__c FROM Children__r) FROM MyObject__c

Always double check the Relationship Name programmatically. I find this habit to be the most reliable way to obtain it:

for (ChildRelationship relation : SObjectType.Parent__c.getChildRelationships())
    if (relation.getChildSObject() == Child__c.sObjectType)
        system.debug(relation.getRelationshipName());

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.