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
Alex Raine 9Alex Raine 9 

Update related record after deletion

Hi,

I'm new to creating apex triggers and so was looking to see if anyone could assist with the below problem:

I have an object called Warehouse Picking (Warehouse_Picking__c) which has a lookup to an object called Location (SVMXC__Site__c) via a field called "Engineer Location" (SVMX_Engineer_Location__c). I need to be able to update the field "Count of In Progress Warehouse Picks" (Count_of_In_Progress_Warehouse_Picks__c) on the related location record whenever a Warehouse Picking record with a "Status" (Status__c) is deleted. The field being updated is a number field and the trigger needs to reduce its value by 1.

I have included the trigger i have created so far below and whilst it does not contain the condition of the warehouse pick record's status i have tested it as is but get the following error:

System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateVanLocationwhenWarehousePickdeleted: line 18, column 1

Please can you therfore assisit with:

1) rectifying the trigger so that the above error no longer occurs
2) modifying the trigger to include the condition that the Location record is only updated when an "In Progress" warehouse picking record is deleted
 
trigger UpdateVanLocationwhenWarehousePickdeleted on Warehouse_Picking__c (after delete) {

   List<Id> idSiteList = new List<Id>();
   List<SVMXC__Site__c> siteListToUpdate = new List<SVMXC__Site__c>();
  
    {    
        for (Warehouse_Picking__c wp : trigger.old) {
                     
            idSiteList.add(wp.SVMX_Engineer_Location__c);
 }   
}

if(idSiteList.isEmpty()) return;

for(Id id : idSiteList) {

    SVMXC__Site__c site = new SVMXC__Site__c(Id = id); 
    site.Count_of_In_Progress_Warehouse_Picks__c= site.Count_of_In_Progress_Warehouse_Picks__c - 1;
    
    siteListToUpdate.add(site);
    
}

if(siteListToUpdate.size() > 0)
Database.update(siteListToUpdate);
}

Thank you in advance for any assistance you can render.
Best Answer chosen by Alex Raine 9
Shamsi 110Shamsi 110
Add this section   

trigger UpdateVanLocationwhenWarehousePickdeleted on Warehouse_Picking__c (after delete) {
    List<Id> idSiteList = new List<Id>();
    List<SVMXC__Site__c> siteList = new List<SVMXC__Site__c>();
    List<SVMXC__Site__c> siteListToUpdate = new List<SVMXC__Site__c>();
       for (Warehouse_Picking__c wp : trigger.old)
        {
               if(wp.status == 'In Progress')
              {
                idSiteList.add(wp.SVMX_Engineer_Location__c);
               }
        }   

    siteList = [select id, Count_of_In_Progress_Warehouse_Picks__c from SVMXC__Site__c where id in : idSiteList];
    for(SVMXC__Site__c location : siteList)
    {
        location.Count_of_In_Progress_Warehouse_Picks__c = location.Count_of_In_Progress_Warehouse_Picks__c - 1;
        siteListToUpdate.add(location);
    }
    
    if(siteListToUpdate.size() > 0){
        Database.update(siteListToUpdate);
        }
    
}

Kindly mark this answer as solution to help others, if it helps you.

Thanks,
Shamsi

All Answers

Shamsi 110Shamsi 110
Hello Alex,

Test apparently this should work.

trigger UpdateVanLocationwhenWarehousePickdeleted on Warehouse_Picking__c (after delete) {
    List<Id> idSiteList = new List<Id>();
    List<SVMXC__Site__c> siteList = new List<SVMXC__Site__c>();
    List<SVMXC__Site__c> siteListToUpdate = new List<SVMXC__Site__c>();
        for (Warehouse_Picking__c wp : trigger.old)
        {
                idSiteList.add(wp.SVMX_Engineer_Location__c);
        }   
    siteList = [select id, Count_of_In_Progress_Warehouse_Picks__c from SVMXC__Site__c where id in : idSiteList];
    for(SVMXC__Site__c location : siteList)
    {
        location.Count_of_In_Progress_Warehouse_Picks__c = location.Count_of_In_Progress_Warehouse_Picks__c - 1;
        siteListToUpdate.add(location);
    }
    
    if(siteListToUpdate.size() > 0){
        Database.update(siteListToUpdate);
        }
    
}

Kindly mark this answer as solution to help others, if it helps you.

Thanks,
Shamsi
Alex Raine 9Alex Raine 9
Hi Shamsi,

Thanks very much for the help this is now working but i also need the trigger to only work for Warehouse Picking records that are deleted with a status of "In Progress". The trigger you posted runs whenenver any Warehouse Picking record is deleted, regardless of ststus.
Shamsi 110Shamsi 110
Add this section   

trigger UpdateVanLocationwhenWarehousePickdeleted on Warehouse_Picking__c (after delete) {
    List<Id> idSiteList = new List<Id>();
    List<SVMXC__Site__c> siteList = new List<SVMXC__Site__c>();
    List<SVMXC__Site__c> siteListToUpdate = new List<SVMXC__Site__c>();
       for (Warehouse_Picking__c wp : trigger.old)
        {
               if(wp.status == 'In Progress')
              {
                idSiteList.add(wp.SVMX_Engineer_Location__c);
               }
        }   

    siteList = [select id, Count_of_In_Progress_Warehouse_Picks__c from SVMXC__Site__c where id in : idSiteList];
    for(SVMXC__Site__c location : siteList)
    {
        location.Count_of_In_Progress_Warehouse_Picks__c = location.Count_of_In_Progress_Warehouse_Picks__c - 1;
        siteListToUpdate.add(location);
    }
    
    if(siteListToUpdate.size() > 0){
        Database.update(siteListToUpdate);
        }
    
}

Kindly mark this answer as solution to help others, if it helps you.

Thanks,
Shamsi
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Alex,
Try this code:
Trigger:
trigger UpdateVanLocationwhenWarehousePickdeleted on Warehouse_Picking__c (before delete) {
    if(trigger.isDelete && trigger.isBefore) {
        UpdateVanLocation_handler.updateCount(trigger.old);
    }
}
Trigger Handler:
public class UpdateVanLocation_handler {
    public static void updateCount( List<Warehouse_Picking__c> wPList ) {
        try {
            system.debug('wPList------ ' + wPList);
            Set<Id> idSiteSet = new Set<Id>();
            List<SVMXC_Site__c> siteList = new List<SVMXC_Site__c>();
            List<SVMXC_Site__c> siteListToUpdate = new List<SVMXC_Site__c>();
            for ( Warehouse_Picking__c wp : wPList ) {
                if(wp.SVMX_Engineer_Location__c != null && wp.Status__c == 'In Progress') {
                    idSiteSet.add(wp.SVMX_Engineer_Location__c);
                }
            }
            system.debug('idSiteSet------ ' + idSiteSet);
            
            if(idSiteSet.size() > 0) {
                siteList = [SELECT Id, Count_of_In_Progress_Warehouse_Picks__c FROM SVMXC_Site__c WHERE ID IN : idSiteSet LIMIT 10000];
            }
            
            system.debug('siteList------ ' + siteList);
            
            for(SVMXC_Site__c sSobj : siteList)
            {
                sSobj.Count_of_In_Progress_Warehouse_Picks__c = sSobj.Count_of_In_Progress_Warehouse_Picks__c - 1;
                siteListToUpdate.add(sSobj);
            }
            system.debug('siteList------ ' + siteList);
            
            if(siteListToUpdate.size() > 0){
                Database.update(siteListToUpdate);
            }
        } catch (Exception ex) {
            system.debug('Exception---ofLine--->' + ex.getLineNumber());
            system.debug('Exception---Message--->' + ex.getMessage());
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Alex Raine 9Alex Raine 9
Thanks very much for all your help Shamsi.
Shamsi 110Shamsi 110
Glad it worked for you.
 Happy coding.