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
Cameron BumsteadCameron Bumstead 

Need an APEX trigger for a roll up summary on a custom object with lookup relationship

I have a field "MRR" on the Opportunity object. I also have a lookup relationship field on the Opportunity object to relate the opportunity to a custom object "Vendor Product". I have created a field on the Vendor Product object named "Current MRR". I need to write a trigger to update the "Current MRR" field on the Vendor Product object. Could someone help me out with this? I've read a lot of answers on here but haven't been able to wrap my head around it without it being in terms that I am familiar with. Thanks!
pconpcon
Does this trigger need to pick the most recent opportunity associated with it?  What should cause the Current MRR field to be updated?
Cameron BumsteadCameron Bumstead
No, I've put perameters on the MRR field in the opportunity for it to only include active contracts. It just needs to be essentially a roll-up summary of all MRR because of the rules I've already put in place. Basically, the Current MRR field should be updated when a deal goes effective.
pconpcon
I'll be frank with you, I live my life in the Service Cloud and have no knowledge of Opportunities, Contracts or even what an MRR means.  I can however help you write a trigger that sums/counts/etc related objects.  However, I'm going to need you to be overly verbose in helping me understand your criteria and your end goal (don't be afraid to use small words and/or pictures).
Cameron BumsteadCameron Bumstead
To simplify, we don't have to use my specific variables. Let's call the field on the Opportunity object as "Amount" and the field on the Vendor Product (custom object) that I would like the roll-up summary as "Total". Basically, the "Total" field should be the roll-up summary of "Amount", no extra perameters are needed. The Vendor Product object is the parent in the lookup relationship with Opportunity. I think that should make a little more sense
pconpcon
That makes much more sense.  So to do this we would want to have a trigger on the Opportunity that whenever it is inserted or updated, and we have a Vendor Product set then we will query all of the Opportunities associated with that product, sum them, and then save that on to the Vendor Product.
 
trigger VendorProductSum on Opportunity (after insert, after update, after delete) {
    Set<Id> vendorProductIds = new Set<Id>();

    if (!Trigger.isDelete) {
        for (Opportunity opp : Trigger.new) {
            vendorProductIds.add(opp.Vendor_Product__c);
        }
    }

    if (!Trigger.isInsert) {
        for (Opportunity opp : Trigger.oldMap.values()) {
            vendorProductIds.add(opp.Vendor_Product__c);
        }
    }

    vendorProductIds.remove(null);

    if (!vendorProductIds.isEmpty()) {
        Vendor_Product__c vendorProductsToUpdate = new List<Vendor_Product__c>();

        for (AggregateResult ar : [
            select sum(MRR__c),
                Vendor_Product__c
            from Opportunity
            where Vendor_Product__c in :vendorProductIds
            group by Vendor_Product__c
        ]) {
            vendorProductsToUpdate.add(new Vendor_Product__c(
                Id = ar.Vendor_Product__c,
                Current_MRR__c = ar.get('expr0')
            ));
        }

        if (!vendorProductsToUpdate.isEmpty()) {
            update vendorProductsToUpdate;
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors.