I've got a trigger that was working perfectly in my sandbox but once pushed into production we receive:
Update_DUA_Last_Activity_Date: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.Update_DUA_Last_Activity_Date: line 11, column 1
L11C1 is the line beginning: if (!t.Subject.startsWith(subjectToExclude) Any ideas where something could have gone wrong in prod?
Trigger in question:
trigger Update_DUA_Last_Activity_Date on Task (after insert, after update) {
//Do not consider tasks with this subject
String subjectToExclude = '[Outreach] [Email] [Opened]';
//Only consider tasks that are related to leads
String leadPrefix = Lead.sObjectType.getDescribe().getKeyPrefix();
//Find leads for tasks in trigger
Set<Id> taskLeadIds = new Set<Id>();
for (Task t : trigger.new){
if (!t.Subject.startsWith(subjectToExclude) && String.valueOf(t.WhoId).startsWith(leadPrefix)){
taskLeadIds.add(t.WhoId);
}
}
//Query using WhoIds to get lead records
List<Lead> taskLeads = [SELECT Id, rWeb_Domain__c, LastActivityDate
FROM Lead WHERE Id IN :taskLeadIds];
//Account for possibility of multiple leads with different LastActivityDate
Map<String, List<Date>> rWebDateMap = new Map<String, List<Date>>();
for (Lead lead : taskLeads) {
if (!rWebDateMap.keyset().contains(lead.rWeb_Domain__c)) {
rWebDateMap.put(lead.rWeb_Domain__c,new List<Date>());
rWebDateMap.get(lead.rWeb_Domain__c).add(lead.LastActivityDate);
} else {
rWebDateMap.get(lead.rWeb_Domain__c).add(lead.LastActivityDate);
}
}
//Find affected DUAs
List<Domain_User_Assignment__c> duas = [SELECT Id, rWeb_Domain__c, Last_Activity_Date__c
FROM Domain_User_Assignment__c
WHERE rWeb_Domain__c
IN :rWebDateMap.keyset()];
//Pick most recent/furthest future Last Activity Date
for (Domain_User_Assignment__c dua : duas) {
List<Date> leadDates = rWebDateMap.get(dua.rWeb_Domain__c);
leadDates.sort(); //Sorts ascending
dua.Last_Activity_Date__c = leadDates[leadDates.size()-1]; //Use last index
}
update duas;
}
t.whoId != null && t.whoId.getSobjectType() == Lead.SobjectTypeis better form than the string startsWith approach