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
pchowdarypchowdary 

Apex class to update a field with count

I need to update a field on a object with count of a field from another object (parent object) by writing a apex class. I'm stuck on this. Can anyone just guide me on this.
Best Answer chosen by pchowdary
Ajay K DubediAjay K Dubedi
You can not update the field on child object from parent object but you can "update count on Parent Object if child is inserted, updated, deleted or undeleted" :

trigger updateCountOnAccount on Contact(after insert, after update, after delete, after undelete){
    Set<Id> accIds = new Set<Id>();
    if(trigger.isDelete){
        for(Contact con : trigger.old){
            if(con.AccountId != null){
                accIds.add(con.AccountId);
            }
        }
    }
    else{
        for(Contact con : trigger.new){
            if(trigger.isInsert || trigger.isUndelete){
                if(con.AccountId != null){
                    accIds.add(con.AccountId);
                }
            }
            else{
                Contact oldCon = trigger.oldMap.get(con.id);
                if(oldCon.AccountId != con.AccountId){
                    if(oldCon.AccountId != null)
                        accIds.add(oldCon.AccountId);
                    if(con.AccountId != null)
                        accIds.add(con.AccountId);
                }
            }
        }
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([Select count__c from Account where id in :accIds]);
    
    if(trigger.isDelete){
        for(Contact con : trigger.old){
            if(accMap.containsKey(con.AccountId)){
                accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c - 1;
            }
        }
    }
    else{
        for(Contact con : trigger.new){
            if(trigger.isInsert || trigger.IsUndelete){
                if(accMap.containsKey(con.AccountId)){
                    if(accMap.get(con.AccountId).count__c == null){
                        accMap.get(con.AccountId).count__c = 1;
                    }
                    else{
                        accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
                    }
                }
                 
            }
            if(trigger.isUpdate){
                Contact oldCon = trigger.oldMap.get(con.id);
                if(oldCon.AccountId != con.AccountId){
                    if(oldCon.AccountId != null){
                        accMap.get(oldCon.AccountId).count__c = accMap.get(oldCon.AccountId).count__c - 1;
                    }
                    if(con.AccountId != null){
                        if(accMap.get(con.AccountId).count__c == null){
                            accMap.get(con.AccountId).count__c = 1;
                        }
                        else{
                            accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
                        }
                    }
                }
            }
        }
    }
    update accMap.values();
}


Please make sure to replace following variables :
1. Account: Your Parent Object
2. Contact: Your Child Object
3. Count__c: Field on Account where you want the no. of child records.
4. AccountId: API name of Parent Lookup field on Child object
 

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,
Can you share you code? It would be lost easier to help you that way
Ajay K DubediAjay K Dubedi
You can not update the field on child object from parent object but you can "update count on Parent Object if child is inserted, updated, deleted or undeleted" :

trigger updateCountOnAccount on Contact(after insert, after update, after delete, after undelete){
    Set<Id> accIds = new Set<Id>();
    if(trigger.isDelete){
        for(Contact con : trigger.old){
            if(con.AccountId != null){
                accIds.add(con.AccountId);
            }
        }
    }
    else{
        for(Contact con : trigger.new){
            if(trigger.isInsert || trigger.isUndelete){
                if(con.AccountId != null){
                    accIds.add(con.AccountId);
                }
            }
            else{
                Contact oldCon = trigger.oldMap.get(con.id);
                if(oldCon.AccountId != con.AccountId){
                    if(oldCon.AccountId != null)
                        accIds.add(oldCon.AccountId);
                    if(con.AccountId != null)
                        accIds.add(con.AccountId);
                }
            }
        }
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([Select count__c from Account where id in :accIds]);
    
    if(trigger.isDelete){
        for(Contact con : trigger.old){
            if(accMap.containsKey(con.AccountId)){
                accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c - 1;
            }
        }
    }
    else{
        for(Contact con : trigger.new){
            if(trigger.isInsert || trigger.IsUndelete){
                if(accMap.containsKey(con.AccountId)){
                    if(accMap.get(con.AccountId).count__c == null){
                        accMap.get(con.AccountId).count__c = 1;
                    }
                    else{
                        accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
                    }
                }
                 
            }
            if(trigger.isUpdate){
                Contact oldCon = trigger.oldMap.get(con.id);
                if(oldCon.AccountId != con.AccountId){
                    if(oldCon.AccountId != null){
                        accMap.get(oldCon.AccountId).count__c = accMap.get(oldCon.AccountId).count__c - 1;
                    }
                    if(con.AccountId != null){
                        if(accMap.get(con.AccountId).count__c == null){
                            accMap.get(con.AccountId).count__c = 1;
                        }
                        else{
                            accMap.get(con.AccountId).count__c = accMap.get(con.AccountId).count__c + 1;
                        }
                    }
                }
            }
        }
    }
    update accMap.values();
}


Please make sure to replace following variables :
1. Account: Your Parent Object
2. Contact: Your Child Object
3. Count__c: Field on Account where you want the no. of child records.
4. AccountId: API name of Parent Lookup field on Child object
 
This was selected as the best answer
pchowdarypchowdary
OK Ajay. Thanks for the help