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
jha praveen 13jha praveen 13 

update stock inventory on delete line items

How can we update stock inventory on delete line items using salesforce trigger
SecondID PatelSecondID Patel
Do you mean there is a parent(Contains fields of quantity to indicate the stock) and child object has line items which also have some quantity?

Master Record: Laptops with Quantity =10
Child Record: Laptop1 with quantity =3
                       Laptop1 with quantity =5
                       Laptop1 with quantity =2
 So the sum of 3+5+2=10 ultimately which will be your stock.

If it is your requirement then I would highly suggest create the  Master-Detail Relationship between two objects and then use roll-up summary fields.

A trigger is also an option, where a trigger will be created on a child object on Insert, Delete and Undelete Events.
Please let me know if you want a trigger on this.
jha praveen 13jha praveen 13
This is warehouse application. Update stock only work every time record is created and edited. But i need to update delete also
SecondID PatelSecondID Patel
Change the relationship between objects to Master-detail and create a Roll Up summary field on Master objects which is taking the sum of Quantity field which is in Child objects. This will help you on insert, Update and delete event as well.
SecondID PatelSecondID Patel
Use this trigger to update your stock quantity on insert, update,delete and undelete event.
 
trigger UpdateStock on Child_Obj__c (after insert,after Update,after delete,after undelete) {
    Set<Id> parentIds = New Set<Id>();
    List<Child_Obj__c> tgrList = (trigger.isInsert || trigger.isUndelete || trigger.isUpdate) ?  trigger.new : trigger.Old;
    
    for(Child_Obj__c chd: tgrList){
        if(chd.ParentFieldOnChildObj__c != null){
            parentIds.add(chd.ParentFieldOnChildObj__c); 
            
        }
    }
    
    if(!parentIds.isEmpty()){
        Map<Id,Child_Obj__c> chdMap = New Map<Id,Child_Obj__c>([SELECT Id,Name,ParentFieldOnChildObj__c,Child_Quantity__c 
                                                                FROM Child_Obj__c WHERE 
                                                                ParentFieldOnChildObj__c IN : parentIds]); 
        
        Map<Id,Parent_Obj__c> parentloop = New Map<Id,Parent_Obj__c>([SELECT Id,Name,P_Quantity__c 
                                                                      FROM Parent_Obj__c 
                                                                      WHERE ID IN :parentIds]);
        for(Parent_Obj__c prec : parentloop.values()){
            prec.P_Quantity__c = 0;
        } 
        
        for(Child_Obj__c cRec : chdMap.values()){
            parentloop.get(cRec.ParentFieldOnChildObj__c).P_Quantity__c = parentloop.get(cRec.ParentFieldOnChildObj__c).P_Quantity__c+crec.Child_Quantity__c ;
        }
        UPDATE parentloop.values();
    }
    
}