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
Amanda ReamAmanda Ream 

Trigger to Create a Roll-up Summary Field on a Hierarchial Lookup Relationship.

I am an APEX newbie trying super hard to teach myself to code but this challenge is proving pretty difficult for me. The situtation is that we have a hierarchial lookup relationship in Opportunities where child Opportunities can be linked to parents. I have found that it is possible to write a trigger to do a roll-up summary from a lookup to another object but I have not found anything that says it is possible in the context of hierarchial lookups. So my first questions is, is this possible?

If so, I found some sample code on this site (http://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/) that demostrates how to do this between two separate objects but I am having a little trobule translating. This is pretty complex compared to the triggers I have been able to write before so my second question pertains to a section of the code I don't understand. What exactly is the bolded part of the code doing? I have read about for loops and if statements but this is more complex than anything I have ever seen. Any help is much appreciated :)

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;
}
Best Answer chosen by Amanda Ream
Cloud_forceCloud_force
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);
}

here in first for loop it is iterating thrugh all the records that invoked the trigger. Trigger.new is a list that contains all the records that have invoked the trigger code.

in second part if statement will be executed if the trigger was invoked either by update or by delete dml.

You can use relationship query to build roll up summary.
http://www.forcexplore.com/2014/07/relationship-query-in-salesforce.html
 

All Answers

Cloud_forceCloud_force
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);
}

here in first for loop it is iterating thrugh all the records that invoked the trigger. Trigger.new is a list that contains all the records that have invoked the trigger code.

in second part if statement will be executed if the trigger was invoked either by update or by delete dml.

You can use relationship query to build roll up summary.
http://www.forcexplore.com/2014/07/relationship-query-in-salesforce.html
 
This was selected as the best answer
Amanda ReamAmanda Ream
Ok, got it. Thank you!