You need to sign in to do that
Don't have an account?
Wes 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
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; }
Use below code. Let me know the outcomes.
Thanks!
Amit Singh