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 

Custom Relationship in Apex Trigger

I am writing an Apex trigger to perform a rollup sum of two objects related by a lookup relationship. The Account (parent) object is supposed to have a number field (Individual Engagement Score) that has a value of the roll-up sum from the EngagmentContact custom (child) object. 

I keep getting 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.

Everything I am seeing online says there needs to be a Master-Detail relationship, but that's not an option. Any help would be great. Thank you! 

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);
        }

	// 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 EngagementContact__r) from Account where Id IN :acctIds]) {
        accountMap.get(acct.Id).Individual_Engagement_Score__c = acct.EngagementContact__r.size();
        // add the account in the map to a list so we can update it
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}
Amit Singh 1Amit Singh 1
The Error is due to that you are using Related List Label in the query for child object but you need to use Relationship Name like EngagementContacts__r and you are using EngagementContact__r.

Use below code.
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.<b>EngagementContact__r</b>.size();
        // add the account in the map to a list so we can update it
        accountsToUpdate.add(accountMap.get(acct.Id));
    }
    
    update accountsToUpdate;
}
Let me know the outcomes.
Thanks!
Amit Singh