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
rajjjjrajjjj 

not able to understand this

trigger calculate on Item__c (after insert, after update, after delete) {

// Use a map because it doesn't allow duplicate values 
    

Map<ID, Shipping_Invoice__C> updateMap = new Map<ID, Shipping_Invoice__C>();

// Set this integer to -1 if we are deleting 
    
Integer subtract ;

// Populate the list of items based on trigger type 
    
List<Item__c> itemList;
    if(trigger.isInsert || trigger.isUpdate){
        itemList = Trigger.new;
        subtract = 1;
    }
    else if(trigger.isDelete)
    {
        // Note -- there is no trigger.new in delete 
    
        itemList = trigger.old;
        subtract = -1;
    }

// Access all the information we need in a single query  
    
// rather than querying when we need it. 
    
// This is a best practice for bulkifying requests 
    

set<Id> AllItems = new set<id>();

for(item__c i :itemList){
// Assert numbers are not negative.   
    
// None of the fields would make sense with a negative value 
    

System.assert(i.quantity__c > 0, 'Quantity must be positive');
System.assert(i.weight__c >= 0, 'Weight must be non-negative');
System.assert(i.price__c >= 0, 'Price must be non-negative');

// If there is a duplicate Id, it won't get added to a set 
    
AllItems.add(i.Shipping_Invoice__C);
}

// Accessing all shipping invoices associated with the items in the trigger 
    
List<Shipping_Invoice__C> AllShippingInvoices = [SELECT Id, ShippingDiscount__c, 
                   SubTotal__c, TotalWeight__c, Tax__c, GrandTotal__c 
                   FROM Shipping_Invoice__C WHERE Id IN :AllItems];
                   
// Take the list we just populated and put it into a Map.   
    
// This will make it easier to look up a shipping invoice 
    
// because you must iterate a list, but you can use lookup for a map,  
    
Map<ID, Shipping_Invoice__C> SIMap = new Map<ID, Shipping_Invoice__C>();

for(Shipping_Invoice__C sc : AllShippingInvoices)
{
    SIMap.put(sc.id, sc);
}

// Process the list of items 
    
    if(Trigger.isUpdate)
    {
        // Treat updates like a removal of the old item and addition of the          
    
        // revised item rather than figuring out the differences of each field  
    
        // and acting accordingly. 
    
        // Note updates have both trigger.new and trigger.old 
    
        for(Integer x = 0; x < Trigger.old.size(); x++)
        {
            Shipping_Invoice__C myOrder;
            myOrder = SIMap.get(trigger.old[x].Shipping_Invoice__C);

            // Decrement the previous value from the subtotal and weight. 
    
            myOrder.SubTotal__c -= (trigger.old[x].price__c * 
                                    trigger.old[x].quantity__c);
            myOrder.TotalWeight__c -= (trigger.old[x].weight__c * 
                                       trigger.old[x].quantity__c);
                
            // Increment the new subtotal and weight. 
    
            myOrder.SubTotal__c += (trigger.new[x].price__c * 
                                    trigger.new[x].quantity__c);
            myOrder.TotalWeight__c += (trigger.new[x].weight__c * 
                                       trigger.new[x].quantity__c);
        }
        
        for(Shipping_Invoice__C myOrder : AllShippingInvoices)
        {
            
            // Set tax rate to 9.25%  Please note, this is a simple example.   
    
            // Generally, you would never hard code values. 
    
            // Leveraging Custom Settings for tax rates is a best practice.   
    
            // See Custom Settings in the Apex Developer's guide  
    
            // for more information. 
    
            myOrder.Tax__c = myOrder.Subtotal__c * .0925;
            
            // Reset the shipping discount 
    
            myOrder.ShippingDiscount__c = 0;
    
            // Set shipping rate to 75 cents per pound.   
    
            // Generally, you would never hard code values. 
    
            // Leveraging Custom Settings for the shipping rate is a best practice. 
    
            // See Custom Settings in the Apex Developer's guide  
    
            // for more information. 
    
            myOrder.Shipping__c = (myOrder.totalWeight__c * .75);
            myOrder.GrandTotal__c = myOrder.SubTotal__c + myOrder.tax__c + 
                                    myOrder.Shipping__c;
            updateMap.put(myOrder.id, myOrder);
         }
    }
    else
    { 
        for(Item__c itemToProcess : itemList)
        {
            Shipping_Invoice__C myOrder;
    
            // Look up the correct shipping invoice from the ones we got earlier 
    
            myOrder = SIMap.get(itemToProcess.Shipping_Invoice__C);
            myOrder.SubTotal__c += (itemToProcess.price__c * 
                                    itemToProcess.quantity__c * subtract);
            myOrder.TotalWeight__c += (itemToProcess.weight__c * 
                                       itemToProcess.quantity__c * subtract);
        }
        
        for(Shipping_Invoice__C myOrder : AllShippingInvoices)
        {
            
             // Set tax rate to 9.25%  Please note, this is a simple example.   
    
             // Generally, you would never hard code values. 
    
             // Leveraging Custom Settings for tax rates is a best practice.   
    
             // See Custom Settings in the Apex Developer's guide  
    
             // for more information. 
    
             myOrder.Tax__c = myOrder.Subtotal__c * .0925;
             
             // Reset shipping discount 
    
             myOrder.ShippingDiscount__c = 0;
    
            // Set shipping rate to 75 cents per pound.   
    
            // Generally, you would never hard code values. 
    
            // Leveraging Custom Settings for the shipping rate is a best practice. 
    
            // See Custom Settings in the Apex Developer's guide  
    
            // for more information. 
    
            myOrder.Shipping__c = (myOrder.totalWeight__c * .75);
            myOrder.GrandTotal__c = myOrder.SubTotal__c + myOrder.tax__c + 
                                    myOrder.Shipping__c;
                                       
            updateMap.put(myOrder.id, myOrder);
    
         }
     }    
     
     // Only use one DML update at the end. 
    
     // This minimizes the number of DML requests generated from this trigger. 
    
     update updateMap.values();
}

 Can anybody say what he was doing there....am not able to know he is dng with 'Allitems'

That is the example in documentation under appendices......shipping invoice example.....please help me with that....am not to understnad that. I want to know how he is approaching to write that trigger.

SeAlVa_VFTabulatorSeAlVa_VFTabulator

It is a bulk trigger, 

 

What he is doing is 

a) Iterate over all the elements in the itemList (trigger.new or trigger.old)

b) store in a set all the set AllItems, all the ID's of the lookup field.

c) quering the system to get a Map of ID (lookup field we talked about before) and Object and stores it in AllShippingInvoices

 

By doing it this way, there is no need of quering the system once per record, you just have to get it from the Map.

 

Is this what you needed to get clarified?

 

Regards