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
zAkszAks 

trigger to sum all Quantity if status is 'avalible', and update parents total Quantity field.

Hi All,
Beloe is the snippet:
trigger UpdateQuantity on SVMXC__Product_Stock__c (after insert,after update)
 {
   Set<Id> productIds = new Set<Id>();
    Map<Id, SVMXC__Product_Stock__c> productToStock = new Map<Id, SVMXC__Product_Stock__c>();
    for (SVMXC__Product_Stock__c stock : Trigger.new) 
    {
        if (stock.SVMXC__Product__c != null) 
        {
            productIds.add(stock.SVMXC__Product__c);
            productToStock.put(stock.Id, stock);
        }
    }
    
    Map<Id, Product2> productMap = new Map<Id, Product2>([SELECT Total_Available_Quantity__c FROM Product2 WHERE Id IN :productIds]);
     List<Product2> productsToUpdate = new List<Product2>();
      for(SVMXC__Product_Stock__c stock : trigger.new)
     {
     AggregateResult[] results = [Select SVMXC__Product__c,SUM(SVMXC__Quantity2__c) FROM SVMXC__Product_Stock__c GROUP BY SVMXC__Product__c];
     system.debug('tttt' + stock);
       //SVMXC__Product_Stock__c oldStock = trigger.oldMap.get(stock.Id);
           // System.debug('oldStockId'+ oldStock);
           Product2 product = productMap.get(stock.SVMXC__Product__c);
          if(stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
           product.Total_Available_Quantity__c =    stock.SVMXC__Quantity2__c;
            system.debug('test' + product.Total_Available_Quantity__c);
             productsToUpdate.add(product);
     
         }
        
       /* if (oldStock.SVMXC__Status__c.equalsIgnoreCase('Available') && !stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
        product.Total_Available_Quantity__c = product.Total_Available_Quantity__c - stock.SVMXC__Quantity2__c;
         productsToUpdate.add(product);
         system.debug('kkkk' + productsToUpdate);
         
        }*/

     }
     
      try {
        update productsToUpdate;
    } catch (Exception ex) 
    {
        System.debug(ex.getMessage());
    }
  
 }
Best Answer chosen by zAks
rajat Maheshwari 6rajat Maheshwari 6
zAks,

Please follow below snippet for your use case - 






trigger UpdateQuantity on SVMXC__Product_Stock__c (after insert,after update, afterDelete)
 {
   Set<Id> productIds = new Set<Id>();

if(Trigger.isAfter && Trigger.isInsert)
   {
         
    for (SVMXC__Product_Stock__c stock : Trigger.new) 
    {
        if (stock.SVMXC__Product__c != null && stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
            productIds.add(stock.SVMXC__Product__c);
      
        }
    }
}

if(Trigger.isAfter && Trigger.isUpdate)
  {
       for (SVMXC__Product_Stock__c stock : Trigger.new) 
        {
        if (stock.SVMXC__Product__c != null && stock.SVMXC__Status__c ! = Trigger.oldMap.get(stock.id).stock.SVMXC__Status__c  &&  stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
            productIds.add(stock.SVMXC__Product__c);
      
        }
    }
}

if(Trigger.isAfter && Trigger.isDelete)
   {
          for (SVMXC__Product_Stock__c stock : Trigger.old) 
        {
        if (stock.SVMXC__Product__c != null &&          stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
            productIds.add(stock.SVMXC__Product__c);
      
        }
    }
}

Map<Id,Double> Quantity_Map = new Map<Id,Double>();
    

  List<Product2> productsToUpdate = new List<Product2>();
    
     for(AggregateResult results = [Select SVMXC__Product__c,SUM(SVMXC__Quantity2__c) FROM SVMXC__Product_Stock__c where SVMXC__Product__c IN: productIds GROUP BY SVMXC__Product__c]
     {
          Quantity_Map.put((Id)results.get('SVMXC__Product__c'), (Double)results.get('expr0'));
    
      }

   for(Product2 prd : [SELECT Id, Total_Available_Quantity__c FROM Product2 WHERE Id IN :productIds])

       {
           if( Quantity_Map !=null && Quantity_Map.containsKey(prd.Id))
               {
                   prd .Total_Available_Quantity__c = (Double)Quantity_Map.get(prd.id);
                    productsToUpdate.add(prd);
                }
       }
        
    

   if(productsToUpdate!=null && productsToUpdate.size()>0)
          update productsToUpdate;
 
  
 }

please mark as best answer, if it works, otherwise let me know the same

Thanks

 

 


 

All Answers

EldonEldon
Hi zAks

What is the error you are getting and at what line?

Regards
rajat Maheshwari 6rajat Maheshwari 6
zAks,

Please follow below snippet for your use case - 






trigger UpdateQuantity on SVMXC__Product_Stock__c (after insert,after update, afterDelete)
 {
   Set<Id> productIds = new Set<Id>();

if(Trigger.isAfter && Trigger.isInsert)
   {
         
    for (SVMXC__Product_Stock__c stock : Trigger.new) 
    {
        if (stock.SVMXC__Product__c != null && stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
            productIds.add(stock.SVMXC__Product__c);
      
        }
    }
}

if(Trigger.isAfter && Trigger.isUpdate)
  {
       for (SVMXC__Product_Stock__c stock : Trigger.new) 
        {
        if (stock.SVMXC__Product__c != null && stock.SVMXC__Status__c ! = Trigger.oldMap.get(stock.id).stock.SVMXC__Status__c  &&  stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
            productIds.add(stock.SVMXC__Product__c);
      
        }
    }
}

if(Trigger.isAfter && Trigger.isDelete)
   {
          for (SVMXC__Product_Stock__c stock : Trigger.old) 
        {
        if (stock.SVMXC__Product__c != null &&          stock.SVMXC__Status__c.equalsIgnoreCase('Available')) 
        {
            productIds.add(stock.SVMXC__Product__c);
      
        }
    }
}

Map<Id,Double> Quantity_Map = new Map<Id,Double>();
    

  List<Product2> productsToUpdate = new List<Product2>();
    
     for(AggregateResult results = [Select SVMXC__Product__c,SUM(SVMXC__Quantity2__c) FROM SVMXC__Product_Stock__c where SVMXC__Product__c IN: productIds GROUP BY SVMXC__Product__c]
     {
          Quantity_Map.put((Id)results.get('SVMXC__Product__c'), (Double)results.get('expr0'));
    
      }

   for(Product2 prd : [SELECT Id, Total_Available_Quantity__c FROM Product2 WHERE Id IN :productIds])

       {
           if( Quantity_Map !=null && Quantity_Map.containsKey(prd.Id))
               {
                   prd .Total_Available_Quantity__c = (Double)Quantity_Map.get(prd.id);
                    productsToUpdate.add(prd);
                }
       }
        
    

   if(productsToUpdate!=null && productsToUpdate.size()>0)
          update productsToUpdate;
 
  
 }

please mark as best answer, if it works, otherwise let me know the same

Thanks

 

 


 
This was selected as the best answer
rajat Maheshwari 6rajat Maheshwari 6

zAks,

Does your issue get solved ? Please mark as best answer if it works 

Thanks
Rajat Maheshwari

zAkszAks
HI,
Thanks for responding ,

i came up with the solution.