11

Is there anyway to update a rollup summary field with lookup relation ship for a custom object. How can we achieve this using triggers ?

  • Sorry, I've deleted my answer, because I misunderstood the question. Here you have a good article to follow: blog.jeffdouglas.com/2009/07/30/… – Martin Borthiry Apr 25 '14 at 6:25
  • Normally I would flag this as asking someone to do your work for you or a duplicate... But it is a good chance to highlight tools for this purpose and I couldn't find a canonical question to this effect. – Adrian Larson May 29 '15 at 22:46
11

https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries

The above link from Abhinav solves your problem

If you need for more than one object would recommend to try the excellent tool from Andrew

http://andyinthecloud.com/2013/07/07/new-tool-declarative-rollups-for-lookups/

And if you have requirement for only one object ,you can prefer jeff solution as well

http://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/

  • 2
    Yeah I would generally put Fawcett tops on the list. – Adrian Larson May 29 '15 at 22:42
1

Here is an example :

  1. Account (Parent Object)
  2. Contact (Child Object).
  3. Contact_Recs__c (Roll up summary field/Custom Field).
  4. accountid (Lookup field).

Code sample:

trigger CountContactsnew on Contact (after insert, after delete, after undelete) {

    List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Contact con1 : Trigger.new){
            accIdList.add(con1.accountid);
        }
    }
    if(Trigger.isDelete){
        For(Contact con1 : Trigger.old){
            accIdList.add(con1.accountid);
        }
    }
    List<Account> accUpdateList = new List<Account>();
    For(Account acc : [SELECT Contact_Recs__c,(SELECT id FROM Contacts) FROM Account WHERE id =: accIdList]){
        acc.Contact_Recs__c = acc.Contacts.size();
        accUpdateList.add(acc);
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}
  • did you mean to use the in 'operator' instead of the 'is'? in as opposed to = – Sam Kamensky Jan 5 '17 at 23:31
0
public class OpportunityCustomRollup {

    public static void CountRollup(List<Opportunity> lstOpportunity){

        set<id> oppIds = new set<id>();
        map<string, integer> classroomIDToDeskCountMap = new map<string, integer>();
        id objrecordtypeid = [SELECT Id FROM RecordType WHERE DeveloperName ='Fund_Raising'].Id;
        double amount = 0;

        try {
                for (Opportunity objOpportunity : lstOpportunity){
                    oppIds.add(objOpportunity.Fund__c);
                }

                Fund__c objfund = [SELECT Id, Total_opportunity_amount__c  FROM Fund__c WHERE Id = :oppIds];
                List<Opportunity> list_Opportunity = [SELECT Id, Amount FROM Opportunity WHERE Fund__c = :objfund.Id and StageName = 'Closed Won' and RecordTypeId =: objrecordtypeid];


                 for(Opportunity AmountOpportunity : list_Opportunity) {
                        amount += AmountOpportunity.amount; 
                 }


                  objfund.Total_opportunity_amount__c = amount;
                  update objfund;   
            } 


        catch (Exception e) {
                System.debug(e);
            }

    }

}

Trigger:

trigger newLeadTrigger on Opportunity (after insert , after update, after delete , after undelete) {

    if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)){
        OpportunityCustomRollup.CountRollup(Trigger.new);
    }

    if(Trigger.isDelete)
    {
        OpportunityCustomRollup.CountRollup(Trigger.old);
    }
}
0

Account-->Total Amount

Contact--> Amount

Rollup contact amount to account total amount field.

trigger  rollUpAmount on Contact (after Insert,after Update) {
map<id,decimal> accountMap=new map<id,decimal>();
List<account>acList=new List<Account>();
for(contact ct:trigger.new){
    system.debug('trigger is calling:::::');
    if(accountMap.containskey(ct.accountId)){
        accountMap.put(ct.accountId,accountMap.get(ct.accountId)+ct.Amount__c);
    }
    else
    {
        accountMap.put(ct.accountId,ct.Amount__c);
    }
}
system.debug('account Id #####'+ accountMap.keyset());
for(Account ac:[select id,Total_Amount__c from account where id in:accountMap.keyset()]){
    if(ac.Total_Amount__c ==null){ 
        ac.Total_Amount__c = accountMap.get(ac.Id);
    }
    else
    {
        ac.Total_Amount__c = ac.Total_Amount__c +accountMap.get(ac.Id);
    }
    acList.add(ac);

}
system.debug('acList values#######'+acList);
map<id,account> duplicateMap=new map<id,account>();
duplicateMap.putall(acList);
if(duplicateMap.size()>0){
    update duplicateMap.values();
}
}

protected by Community Apr 3 '18 at 17:41

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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