Activity Forums Salesforce® Discussions Help me in a trigger making rollup summary for lookup relation objects in salesforce?

  • Ajit

    Member
    September 29, 2016 at 2:00 pm

    Hi Mohit,

    Your problem is not clear with me, rather you want the total number of related records or you want to rollup some field value of the related records.

  • Vishant

    Member
    June 19, 2017 at 6:57 am

    Hi Mohit ,

    I am attaching a trigger to count no. of contacts on account (Roll up-action-count)

     

     

    Trigger countContacts on Contact (after insert){

    Set<Id> accIdset = new Set<Id>();
    List<Account> accList = new List<Account>();
    List<AggregateResult> arList = new List<AggregateResult>();
    Map<String,Double> acmap = new Map<String,Double>();
    //Added Account Id in Set
    for (Contact con: Trigger.new){

    accIdset.add(con.accountId);
    }
    // Query Contact Created
    if(accIdset.size()>0){
    arList = [select AccountId aid,Count(Id) i from Contact where AccountId IN :accIdset group by AccountId];
    }

    for(AggregateResult q : arList){
    acmap.put((String)q.get('aid'),(Double)q.get('i'));
    }

    for (Account acc:[Select Id,Total_Count__c From Account Where Id in: accIdset]){

    acc.Total_Count__c = acmap.get(acc.Id);
    accList.add(acc);
    }
    update accList;
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos