You need to sign in to do that
Don't have an account?
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
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
List<Asset> assets = new list<Asset>();
assets = trigger.newMap.values();
I think this solves your Issue
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
for (Asset a : trigger.new){
accountIds.add(a.accountId);
}
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".
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>;
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);
}
}