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
4i Apps4i Apps 

Rollup Summary field for Lookup relationship

I have two standard objects Quote and Order. There exists a lookup relationship between these two. I have a field on Order called Milestone_Amount__c and a field on Quote called Cumulative_Order_Value__c.

On insert, this field on Order will be blank. But I need an after update, after delete, after undelete trigger to rollup the sum of this field on order to the cumulative field on quote. Can someone help me?

I'm not very familiar with Apex Code development.

Thanks
NagendraNagendra (Salesforce Developers) 
Hi,

Roll-Up summary fields are a great way to perform calculations on a set of detail records in a master-detail relationship. For instance, if you have a sales order you can create a roll-up summary field to display the sum total of all sales order items (related detail records) for the sales order. The one drawback regarding roll-up summary fields is that they only work for master-details relationships. If you have a lookup relationship to your detail records from your sales order, then roll-up summary fields are not available.

So how do you perform this same type of functionality if you only have a lookup relationship? I ran across this same problem while doing some non-profit work for Medisend International and the solution (with a caveat) is to write a trigger to perform the roll-up.

Medisend ships medical products overseas to developing countries. Medisend has a Shipment custom object that has a related list of Inventory Items which are the actual products on the shipment. In their scenario, the Inventory Items cannot be a master-detail relationship since these items can live on their own until they are assigned to a shipment. So to display the total number of items on the shipment to the case managers, I created a numeric filed on the shipment and wrote a trigger to sum the total number of items assigned to the shipment each time an Inventory Item is added or removed from the shipment.

However, there is one caveat due to governor limits. If the shipment contains more than 1000 items, then the trigger will throw an error at line 19 below. This is due to governor limits specifying that a List can only contain 1000 records. There is an Idea regarding this functionality so please vote for it. However if you are confident that your collection will never contain more than 1000 records, this solution should work for you.

Please find the sample code below and modify it as per your requirement.

Apex Class:
trigger InventoryItemRollup on Inventory_Item__c (after delete, after insert, after update) {

    Set<id> shipmentIds = new Set<id>();
    List<shipment__c> shipmentsToUpdate = new List<shipment__c>();

    for (Inventory_Item__c item : Trigger.new)
        shipmentIds.add(item.Shipment__c);

    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Inventory_Item__c item : Trigger.old)
            shipmentIds.add(item.Shipment__c);
    }

    // get a map of the shipments with the number of items
    Map<id,Shipment__c> shipmentMap = new Map<id,Shipment__c>([select id, items__c from Shipment__c where id IN :shipmentIds]);

    // query the shipments and the related inventory items and add the size of the inventory items to the shipment's items__c
    for (Shipment__c ship : [select Id, Name, items__c,(select id from Inventory_Items__r) from Shipment__c where Id IN :shipmentIds]) {
        shipmentMap.get(ship.Id).items__c = ship.Inventory_Items__r.size();
        // add the value/shipment in the map to a list so we can update it
        shipmentsToUpdate.add(shipmentMap.get(ship.Id));
    }

    update shipmentsToUpdate;

}
Test Class:
@isTest
private class Test_InventoryItemRollup {

    static Shipment__c createShipment(String name, ID caseId) {

        Shipment__c s = new Shipment__c();
        s.Name = name;
        s.Aid_Case__c = caseId;
        insert s;

        return s;

    }

    static testMethod void addItems() {

        Set<id> shipmentIds = new Set<id>();
        Integer shipment1ItemCount = 10;
        Integer shipment2ItemCount = 7;

         Aid_Case__c case1 = new Aid_Case__c();
         case1.Name = 'ZZZ0101';
         insert case1;

        // create test shipments
        Shipment__c shipment1 = createShipment('Shipment 1',case1.id);
        Shipment__c shipment2 = createShipment('Shipment 2',case1.id);

        List<inventory_Item__c> items = new List<inventory_Item__c>();

        for (Integer i=0;i<shipment1ItemCount;i++) {

            Inventory_Item__c item = new Inventory_Item__c();
            item.Name__c = 'LP'+i;
            item.Shipment__c = shipment1.Id;

            items.add(item);

        }

        for (Integer i=0;i<shipment2ItemCount;i++) {

            Inventory_Item__c item = new Inventory_Item__c();
            item.Name__c = 'LP10'+i;
            item.Shipment__c = shipment2.Id;

            items.add(item);

        }

        insert items;

        // fetch the shipments
        shipmentIds.add(shipment1.Id);
        shipmentIds.add(shipment2.Id);

        // query for the shipments
        Map<id,Shipment__c> shipmentMap = new Map<id,Shipment__c>([select Id, Name, items__c from Shipment__c where Id IN :shipmentIds]);

        System.assertEquals(shipment1ItemCount,shipmentMap.get(shipment1.Id).Items__c);
        System.assertEquals(shipment2ItemCount,shipmentMap.get(shipment2.Id).Items__c);

        // now update an inventory item
        items.get(0).Shipment__c = null;
        update items.get(0);

        // query the shipment to find out the total items now
        Shipment__c shipment3 = [select items__c from Shipment__c where Id = :shipment1.id];

        // assert that the shipment is one less than the original
        System.assertEquals(shipment1ItemCount-1,shipment3.Items__c);

    }

}

For more information on this please refer to the below links.
https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries
https://andyinthecloud.com/2013/07/07/new-tool-declarative-rollups-for-lookups/


Please mark this post as solved if it helps.

Best Regards,
Nagendra.P


 
Susan Harris 6Susan Harris 6
If you haven't yet fulfilled your requirement, you can use an app if you are unfamiliar with code. Rollup Helper is available free on the AppExchange and can be used with Lookup relationships as well as master-detail: http://goo.gl/CqJrtH
The TechieThe Techie
Now its flow time.

I recently tried this - https://www.youtube.com/watch?v=hNCBJi-Nys0