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
Wes Reed 27Wes Reed 27 

Error: Didn't understand relationship 'EngagementContact__r' in FROM part of query call

I am curious how to resolve this error: Didn't understand relationship 'EngagementContact__r' in FROM part of query call. 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 goal is to have this trigger perform a rollup summary between to objects based on their 'Lookup Relationship'. Any guidance or advice would be greatly appreciated. 

Thanks - Wes
trigger IndvidualEngagementScore on EngagementContact__c (after delete, after insert, after update) {
    Set<id> acctIds = new Set<id>();
    List<account> accountsToUpdate = new List<account>();
    
    for (EngagementContact__c item : Trigger.new)
        acctIds.add(item.Account__c);
        
        if (Trigger.isUpdate || Trigger.isDelete) {
            for (EngagementContact__c item : Trigger.old)
                acctIds.add(item.Account__c);
        }

    
    Map<id,Account> accoutMap = new Map<id,Account>([select id, Individual_Engagement_Score__c from Account where id IN :acctIds]);
    
    
    for(Account acct : [select Id, Name, Individual_Engagement_Score__c, (select id from EngagementContact__r) from Account where Id IN :acctIds]) {
        accountMap.get(acct.Id).Individual_Engagement_Score__c = acct.EngagementContacs__r.size();
        
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}

 
John TowersJohn Towers
The error means that 'EngagementContact__r' isn't the name of the relationship field for the Account.

You should use what you set for the 'Child Relationship Name' when you created the field. If you look the field setup for the lookup field on the child object you can see the value you entered for that field. Make sure to append __r to it still.
 
rajat Maheshwari 6rajat Maheshwari 6
Hi Wes,

Please use this "EngagementContacts__r" instead "EngagementContact__r" in query

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Wes Reed 27Wes Reed 27
Hey John - thanks. The child object (EngagementContacts) has a lookup field on it called Account. So I changed that and still getting the same error. Thoughts?
 
trigger IndvidualEngagementScore on EngagementContact__c (after delete, after insert, after update) {
	Set<id> acctIds = new Set<id>();
    List<account> accountsToUpdate = new List<account>();
    
    for (EngagementContact__c item : Trigger.new)
        acctIds.add(item.Account__c);
        
        if (Trigger.isUpdate || Trigger.isDelete) {
            for (EngagementContact__c item : Trigger.old)
                acctIds.add(item.Account__c);
        }

	// get a map of the Account with the number of Individual Engagements
	Map<id,Account> accoutMap = new Map<id,Account>([select id, Individual_Engagement_Score__c from Account where id IN :acctIds]);
    
    // query the Accounts and the related Individual Engagements and add the number of Individual Engagements to the Accounts's Individual_Engagement_Score__c
    for(Account acct : [select Id, Name, Individual_Engagement_Score__c, (select id from Account__r) from Account where Id IN :acctIds]) {
        accountMap.get(acct.Id).Individual_Engagement_Score__c = acct.Account__r.size();
        // add the account in the map to a list so we can update it
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}

 
rajat Maheshwari 6rajat Maheshwari 6

Hey Wes,

Use this : - 

trigger IndvidualEngagementScore on EngagementContact__c (after delete, after insert, after update) {
	Set<id> acctIds = new Set<id>();
    List<account> accountsToUpdate = new List<account>();
    
    for (EngagementContact__c item : Trigger.new)
        acctIds.add(item.Account__c);
        
        if (Trigger.isUpdate || Trigger.isDelete) {
            for (EngagementContact__c item : Trigger.old)
                acctIds.add(item.Account__c);
        }

	// get a map of the Account with the number of Individual Engagements
	Map<id,Account> accoutMap = new Map<id,Account>([select id, Individual_Engagement_Score__c from Account where id IN :acctIds]);
    
    // query the Accounts and the related Individual Engagements and add the number of Individual Engagements to the Accounts's Individual_Engagement_Score__c
    for(Account acct : [select Id, Name, Individual_Engagement_Score__c, (select id from EngagementContacts__r ) from Account where Id IN :acctIds]) {
        accountMap.get(acct.Id).Individual_Engagement_Score__c = acct.Account__r.size();
        // add the account in the map to a list so we can update it
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}

Thanks
Wes Reed 27Wes Reed 27
Sovled. Had the wrong Child Relationship Name. FYI For anyone stuck with the issue - be sure to use the Child Relationship Name on the Lookup Field.