Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Count the number of child records on the each parent object

trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {
    //---> above handling all states which could see a contact added to or removed from an account
   
    //---> on delete we use Trigger.Old, all else, Trigger.new
    List<Contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;

    //---> the Set class rocks for finding the unique values in a list
    Set<Id> acctIds = new Set<Id>();
   
    for (Contact c : contacts) {
     //yes, you can have a contact without an account
        if (c.AccountId != null) {
            acctIds.add(c.AccountId);
        }
    }
   
    List<Account> acctsToRollup = new List<Account>();
    
    //****** Here is the Aggregate query...don't count in loops, let the DB do it for you*****
    for (AggregateResult ar : [SELECT AccountId AcctId, Count(id) ContactCount 
                               FROM Contact 
                               WHERE AccountId in: acctIds 
                               GROUP BY AccountId]){
        Account a = new Account();
        a.Id = (Id) ar.get('AcctId'); //---> handy trick for updates, set the id and update
        a.Contact_Count__c = (Integer) ar.get('ContactCount');
        acctsToRollup.add(a);
    }
    
    //----> probably you'll want to do a little more error handling than this...but this should work. 
    update acctsToRollup;

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Call this API in order to fetch the user data. API: https://jsonplaceholder.typicode.com/users. 
Javascript :: javascript concat two htmlcollection 
Javascript :: navbar permanently in react router dom v6 
Javascript :: condition rendering using if-else 
Javascript :: Access models in ExpressJS 
Javascript :: what is the syntax of putting an event listener in javascript mdn 
Javascript :: rpirvate router react 
Javascript :: Self Invoking Functions Can Also be Used To Make Variables Global In JavaScript 
Javascript :: postgresql nodejs 
Javascript :: async await js 
Javascript :: convert json to csv npm 
Javascript :: javascript break with while Loop 
Javascript :: react time picker 
Javascript :: event listener 
Javascript :: blur javascript 
Javascript :: js unshift vs push 
Javascript :: check if string contains url 
Javascript :: what is diffrence between redux and context 
Javascript :: smooth scroll jquery 
Javascript :: javascript sort object by value descending 
Javascript :: how to map over arrays vuejs 
Javascript :: autofocus is not working in react native 
Javascript :: Javascript print/output 
Javascript :: destroy method 
Javascript :: use state in className 
Javascript :: graphql json schema 
Javascript :: javascript find ip and information 
Javascript :: npm mongoose findorcreate 
Javascript :: how can we access the data from array object in javascript 
Javascript :: find element vs find elements 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =