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
das61das61 

System.NullPointerException on before delete

Hello all.

 

I'm new to apex so apologies for the ignorant questions.  I'm getting a System.NullPointerException on the following trigger.  After insert and after update work fine.  I don't think it's failing on the method as it still occurs when I comment it out.  It seems to fail on List<Asset> assets = trigger.newMap.values();  Any help would be greatly appreciated.

 

trigger Customer_Since on Asset (after insert, after update, before delete) {
        List<Asset> assets = trigger.newMap.values();
        List<String> accountIds = new List<String>();

        for (Asset a : assets){
        accountIds.add(a.accountId);
        }
    System.Debug (accountIds);
    UpdateCustomerSince.updateCustomerSince(accountIds);       
 
}

 

Thanks in advance.

 

dave

 

Best Answer chosen by Admin (Salesforce Developers) 
das61das61

Thanks for the help everyone.  Here is a solution that works.  It uses oldMap for after delete.

 

trigger Customer_Since on Asset (after insert, after update, after delete) {
    if (trigger.isDelete) {
        List<Asset> assets = new list<Asset>();
        assets = trigger.oldMap.values();
        List<String> accountIds = new List<String>();
        for (Asset a : assets){
        accountIds.add(a.accountId);
        }
    System.Debug (accountIds);
    UpdateCustomerSince.updateCustomerSince(accountIds);      
    }
    Else {
        List<Asset> assets = new list<Asset>();
        assets = trigger.newMap.values();
        List<String> accountIds = new List<String>();
        for (Asset a : assets){
        accountIds.add(a.accountId);
        }
    System.Debug (accountIds);
    UpdateCustomerSince.updateCustomerSince(accountIds);      
    }
}

All Answers

neeedhelpneeedhelp
Can you give it a try

List<Asset> assets = new list<Asset>();
assets = trigger.newMap.values();

I think this solves your Issue
das61das61

Thanks for the quick reply.  I'm still getting the same error.  Here is the updated code per your suggestion and the full error message.

 

trigger Customer_Since on Asset (after insert, after update, before delete) {

        List<Asset> assets = new list<Asset>();
        assets = trigger.newMap.values();
        List<String> accountIds = new List<String>();

        for (Asset a : assets){
        accountIds.add(a.accountId);
        }
    System.Debug (accountIds);
    UpdateCustomerSince.updateCustomerSince(accountIds);       
 
}

 

Customer_Since: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Customer_Since: line 4, column 1".

 

 

I really appreciate you help with this.

 

dave

 

 

sushant sussushant sus
try this


for (Asset a : trigger.new){
accountIds.add(a.accountId);
}
das61das61

Thanks for the help everyone.  Still no luck.  Below is the latest trigger code and resulting error.

 

trigger Customer_Since on Asset (after insert, after update, before delete) {

 

        List<Asset> assets = new list<Asset>();
        List<String> accountIds = new List<String>();

 

        for (Asset a : trigger.new){
        accountIds.add(a.accountId);
        }
 
    System.Debug (accountIds);
    UpdateCustomerSince.updateCustomerSince(accountIds);       
}

 

Customer_Since: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Customer_Since: line 6, column 1".

 

sushant sussushant sus
change it
List<String> accountIds = new List<String>();
to
List<id> accountIds = new List<id>();

and in class you have to make parameter of function list<id>;
das61das61

Thanks for the help everyone.  Here is a solution that works.  It uses oldMap for after delete.

 

trigger Customer_Since on Asset (after insert, after update, after delete) {
    if (trigger.isDelete) {
        List<Asset> assets = new list<Asset>();
        assets = trigger.oldMap.values();
        List<String> accountIds = new List<String>();
        for (Asset a : assets){
        accountIds.add(a.accountId);
        }
    System.Debug (accountIds);
    UpdateCustomerSince.updateCustomerSince(accountIds);      
    }
    Else {
        List<Asset> assets = new list<Asset>();
        assets = trigger.newMap.values();
        List<String> accountIds = new List<String>();
        for (Asset a : assets){
        accountIds.add(a.accountId);
        }
    System.Debug (accountIds);
    UpdateCustomerSince.updateCustomerSince(accountIds);      
    }
}

This was selected as the best answer