Hello I am Having two object One is Region(Name, totalCustomer) and another is Customer(Name,Region). Region is parent object and customer is child object means Cusotmer Object is having lookup relationship on Region object.

So i want to count how many customer is assigned to particula region and even i want to track when its region is updated everytime on customer.

I want result just Like Rollup summary in Lookup relationship Using triggers when record is inserted and updated in customer and Region is assigned to particular customer.

  • What exactly is the need for a trigger? Can't you use a simple rollup summary and count the child objects? (add a number = 1 field on the child object, and count it) – Saariko Oct 13 '13 at 5:59

Take a look at this Apex library "Salesforce Lookup Rollup-Summaries" by abhinavguptas, it can be called from an Apex Trigger and used to update rollup fields as you describe. Here is the example given with the library (minus the comments). If your interested in a pre-packaged version check this out.

trigger OppRollup on Opportunity (after insert, after update, after delete, after undelete) 
     Opportunity[] objects = null;       
     if (Trigger.isDelete) {
         objects = Trigger.old;
     } else {
        objects = Trigger.new;

     LREngine.Context ctx = new LREngine.Context(
            Account.SobjectType, // parent object
            Opportunity.SobjectType,  // child object                                                    
            Schema.SObjectType.Opportunity.fields.AccountId); // relationship field name

     ctx.add(new LREngine.RollupSummaryField(

    Sobject[] masters = LREngine.rollUp(ctx, objects);    
    update masters;

Note: The example above shows using Sum, however Count is also supported.

Trigger ContactTrigger on Contact(after insert,after update,after delete){
    List<ID> accIDList = new List<ID>();
        for(Contact con:trigger.new){
        Map<ID,Account> accMap = new Map<ID,Account>([Select id,total_Amount__c from account where id=:accIDList]); 
        For(account acc : accMap.Values()){
        for(Contact con:Trigger.new){

                Account temp = accMap.get(con.AccountID);
                temp.total_Amount__c = temp.total_amount__c +con.amount__c;


    update accMap.values();

        for(Contact con:trigger.old){
        Map<ID,Account> accMap = new Map<ID,Account>([Select id,total_Amount__c from account where id=:accIDList]);
        for(Contact con:Trigger.old){
                Account temp = accMap.get(con.AccountID);
                temp.total_Amount__c = temp.total_amount__c-con.amount__c;
        update accMap.values();
  • This is a bare "code dump". Would you mind adding a bit of explanation to your answer to make it more understandable for others? – Eduard Jul 15 '18 at 8:53
  • Welcome to SF.SE Shivakumar. You may not realize that you've posted an answer to a question that's nearly 5 years old. Your answer also doesn't explain how your code works and doesn't appear to me to be a valid answer. Please visit the Help center to better understand how this Form works. Right now your answer is in a review queue for users who have sufficient reputation to review "first posts". – crmprogdev Jul 15 '18 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.