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
CmoutesCmoutes 

Rollup Summary Trigger Help

I am trying to create an Apex Trigger that performs a Rollup from a custom object to Contacts. Here is the error that I am receiving: Didn't understand relationship 'Assignments__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.

Below is the code:
 
trigger RollupActiveAssigmentLevelsToContact on pse__Assignment__c (after insert, after update, after delete) {
    
    set<Id> set_id = new set<Id>();
    
    List<Contact> con_list = new List<Contact>();
    
    if(trigger.isInsert || trigger.isUpdate){
        for(pse__Assignment__c myAssignment : trigger.New) {
            set_id.add(myAssignment.pse__Resource__c);
        }
    }
    else if(trigger.isDelete){
        for(pse__Assignment__c myAssignment : trigger.Old){
            set_id.add(myAssignment.pse__Resource__c);
        }
    }
    
    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isDelete)){
        con_list = [SELECT id,Total_Levels__c, (SELECT id,name FROM Assignments__r WHERE Active_Assignment__c = TRUE) FROM Contact WHERE id IN :set_id];
        
        for(Contact con : con_list){
            if(con.Assignments__r.size()>0)
                con.Total_Levels__c = con.Assignments__r.size();
            else
                con.Total_Levels__c = 0;
        }
        if(!con_list.isEmpty())
        update con_list;
    }
}

Here is a snip of the Child Relationship name:

User-added image

Any and all help would be greatly appreciated!
SFDC GuestSFDC Guest
Hi Cmoutes,

Please refer the below trigger to count child records and make changes according to your requirement.
In the below example, I am counting the contacts of an Account. Here "Number_of_Contacts__c" is custom field of Account object.

trigger CountContacts on Contact (after update, after insert, after delete, after undelete)
{
    Set<id> accountIds = new Set<id>();
    if(Trigger.IsInsert || Trigger.IsUndelete || Trigger.IsUpdate)
    {
        for(Contact con: Trigger.new)
        {
            accountIds.add(con.AccountId);
        }
    }
    if(Trigger.isDelete)
    {
        for(Contact con:Trigger.old)
        {
            accountIds.add(con.AccountId);
        }
    }
    List<Account> accList = [select  Id, Number_of_Contacts__c, (select Id from Contacts) from Account
    
                            where Id IN :accountIds];
    for(Account acc:accList)
    {
        acc.Number_of_Contacts__c=acc.contacts.size();
    }
    update accList;
    
}

Please mark it as best answer if it is helped you on resolving the issue.

Thank You,
Sohel Mohd
cloudstreamercloudstreamer
The query and the syntax looks correct. Can you double check the profile level permission of the 'Assignments' relationship field ( The contact lookup field on the Assignment object) ?
YuchenYuchen
Is the "Assignments__r" the correct API name in your org? You can double check that part. Also, do we need to add "pse__" in front of the Object name, so that it will be "pse__Assignments__r"?
CmoutesCmoutes
Sorry for the late response.

I have verified that I have access to the object and to the Contact lookp field on the Assignment obect. And no Yuchen it is supposed to be Assigments__r the pse_ does not need to be on the front. Any other ideas lol I am lost