0

I have my triggerhandler for Inventory, what I am doing in that triggerhandler is storing aggregate results here and querying Quantity's sum From Inventory I'm putting into Items.Quantity utilised.

Hereafter onAfterInsert and onAfterUpdate is working fine, but OnAfterDelete is not working.

public class InventoryObjectTriggerHandler{

    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
   Map<Id,Items__c> UpdateMap = new Map<Id,Items__c>();


    Set<Id> InvenIds = new Set<Id>();


    public InventoryObjectTriggerHandler(boolean isExecuting, integer size){
        m_isExecuting = isExecuting;
        BatchSize = size;
    }

    public void OnBeforeInsert(List<Inventory__c> newInventory){

    }
     public void OnAfterInsert(List<Inventory__c> newInventory){


       for(Inventory__c record : newInventory) {
            if (record.Items__c != null){
                InvenIds.add(record.Items__c);
            }
        }

        List<AggregateResult> arlist = [SELECT Items__c , SUM(Quantity__c)sum FROM Inventory__c WHERE Items__c IN : InvenIds GROUP BY Items__c];

        for(AggregateResult ar : arlist){
            Items__c q=new Items__c(Id=(Id)ar.get('Items__c'), Quantity_Utilised__c=(Decimal)ar.get('sum'));
            UpdateMap.put(q.id,q);
        }

        if(!UpdateMap.isEmpty()){
           update UpdateMap.values();
        }


      }


    public void OnBeforeUpdate(Map<ID, Inventory__c> oldInventory, List<Inventory__c> updatedInventory, Map<ID, Inventory__c> InventoryMap){

    }

    public void OnAfterUpdate(List<Inventory__c> oldInventory, List<Inventory__c> updatedInventory,Map<ID, Inventory__c> oldInventoryMap,Map<ID, Inventory__c> InventoryMap){


             for(Inventory__c record : updatedInventory) {
            if (record.Items__c != null){
                InvenIds.add(record.Items__c);
            }
        }

        List<AggregateResult> arlist = [SELECT Items__c , SUM(Quantity__c)sumMax FROM Inventory__c WHERE Items__c IN : InvenIds GROUP BY Items__c];

        for(AggregateResult ar : arlist){
            Items__c q=new Items__c(Id=(Id)ar.get('Items__c'), Quantity_Utilised__c=(Decimal)ar.get('sumMax'));
            UpdateMap.put(q.id,q);
        }

        if(!UpdateMap.isEmpty()){
           update UpdateMap.values();
        }

        }        

    public void OnBeforeDelete(List<Inventory__c> qliToDelete, Map<ID, Inventory__c> quoteLineItemMap){


    }

    public void OnAfterDelete(List<Inventory__c> deletedInv, Map<ID, Inventory__c> InventoryMap){


        for(Inventory__c record : deletedInv) {
            if (record.Items__c != null){
                InvenIds.add(record.Items__c);
            }
        }
        system.debug(InvenIds);// here the Id is coming 

        List<AggregateResult> arlist = [SELECT Items__c , SUM(Quantity__c)sumMax FROM Inventory__c WHERE Items__c IN : InvenIds GROUP BY Items__c];
         system.debug(arlist);//I am not getting result here, can you please find error   
        for(AggregateResult ar : arlist){
            Items__c q=new Items__c(Id=(Id)ar.get('Items__c'), Quantity_Utilised__c=0);
           system.debug(ar);
            UpdateMap.put(q.id,q);
        }
        system.debug(UpdateMap);
        if(!UpdateMap.isEmpty()){
           update UpdateMap.values();
        }


    }
    public void OnUndelete(List<Inventory__c> restoredQuoteLineItem){

    }

        }
1

This is because if your Items__c don't have any other Inventory__c record then in your after delete part you won't get any result. So in that case it will keep the old value.

So you need to change it here and need to use Internal query like

List<Items__c> itemList = new List<Items__c>();
for(Items__c it : [Select Id,Quantity_Utilised__c, (Select Quantity__c from Inventories__r where Quantity__c != null) from Items__c WHERE ID IN : InvenIds  ]) {
    decimal quant = 0;
    for(Inventory__c record : it.Inventories__r ) //you need to fix child relationship name here
             quant+= record.Quantity__c ;
    it.Quantity_Utilised__c = quant;
    itemList .add(it);
}
update itemList;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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