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
Ken sfdc1Ken sfdc1 

Rollup Summary for lookup of custom objects

trigger CountSpeakerEvaluationsnew on Speaker_Evaluation_AEGR__c(after insert, after delete, after undelete) {

    List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Speaker_Evaluation_AEGR__c con1 : Trigger.new){
            accIdList.add(con1.Medical_Event_vod__c.id);
        }
    }
    if(Trigger.isDelete){
        For(Speaker_Evaluation_AEGR__c con1 : Trigger.old){
            accIdList.add(con1.Medical_Event_vod__c.id);
        }
    }
    List<Account> accUpdateList = new List<Account>();
    For(Medical_Event_vod__c acc : [SELECT  No_of_Speaker_Evaluations__c,(SELECT id FROM Speaker_Evaluations__r) FROM Medical_Event_vod__c WHERE id =: accIdList]){
        acc.No_of_Speaker_Evaluations__c = acc.Speaker_Evaluations__c.size();
        accUpdateList.add(acc);
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}
Error: Compile Error: Invalid foreign key relationship: Speaker_Evaluation_AEGR__c.Medical_Event_vod__c at line 6 column 27
ArmouryArmoury
What is the relation between Speaker_Evaluation_AEGR__c and Medical_Event_vod__c?
jyothsna reddy 5jyothsna reddy 5
Hi Ken,
Please try the below sample code.I have  created a custom field NO_of_contacts in account object.when  i create a record in contact. No_of_contacts field in related account will be incremented  by 1.Replace standard object with your custom objects.
trigger ContactCount on Contact (after insert, after update, after delete) {
    Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> AcctList = new List<Account>();
    List<Contact> ConList = new List<Contact>();
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)){
                AcctIds.add(Con.AccountId);  
            }   
        }  
    }
    
    if(trigger.isDelete || trigger.isUPdate) {
        for(Contact Con : trigger.Old) {
            AcctIds.add(Con.AccountId);     
        }  
    }           
    
    if(AcctIds.size() > 0){
        ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
        
        for(Contact Con : ConList) {
            if(!AcctContactList.containsKey(Con.AccountId)){
                AcctContactList.put(Con.AccountId, new List<Contact>());
            }
            AcctContactList.get(Con.AccountId).add(Con);      
        }                           
        
        System.debug('Account Id and Contact List Map is ' + AcctContactList);
            
        AcctList = [SELECT No_of_contacts__c FROM Account WHERE Id IN : AcctIds];
        
        for(Account Acc : AcctList) {
            List<Contact> ContList = new List<Contact>();
            ContList = AcctContactList.get(Acc.Id);
            Acc.No_of_contacts__c= ContList.size();
        }    
        
        System.debug('Account List is ' + AcctList);
        update AcctList;    
    }

}

I hope it will help you.
Regards,
Jyothsna D