You need to sign in to do that
Don't have an account?
Null Check and Task WhoID
I am writing a trigger to update a duration field on the lead, after the first sales activity is logged under a lead.
I had the code working in the sandbox and deployed to production. I started getting the dreaded referencing a null object error on this statement.
string.valueOf(newTask.WhoId).startsWith('00Q')
so i added the null check in front of it. Now I am no longer getting the error but my code not populating the field on the lead record anymore. It is almost as if the null check is booting all records and non get to the internal if's.
Thoughts? I have copied my entire code as it exists right now below.
Phil
trigger ActivityFirstContact on Task (after insert) {
List<Lead> updatedLeads= new List<Lead>(); Datetime d = datetime.newInstance(2011, 8, 10); for(Task newTask:Trigger.new){ boolean notmarketuser = TRUE; if (newTask.Created_By_Role__c.contains('Marketing')) notmarketuser = FALSE; if(newTask.WhoID != NULL){ if(string.valueOf(newTask.WhoId).startsWith('00Q') &&
newTask.Subject != 'Lead Source Details' && notmarketuser) { Lead changedLead = [select id, First_Contact_Number__c, CreatedDate from lead where id=:NewTask.WhoId]; if(changedLead.CreatedDate > d && changedLead.First_Contact_Number__c==NULL) { changedLead.First_Contact_Number__c= decimal.valueof((newTask.CreatedDate.getTime()-changedLead.CreatedDate.getTime()))/(1000*60*60); updatedLeads.add(changedLead); } } } } update updatedLeads; }
@MikeGill thank you for the help but that did not seem to solve the problem.
It ends up that my code was correct I have inactivated the rule before testing. Stupid mistake.
So the code in my previous message works. I also added a null check on the line
Somehow I was getting the null reference error there also. Im not sure how a task can be created without a created by role, but I guess it is possible.
Phil
All Answers
May be try this
String whoId = newTask.WhoId;
String isLead = whoId.substring(0,2);
if(isLead == '00Q' &&
@MikeGill thank you for the help but that did not seem to solve the problem.
It ends up that my code was correct I have inactivated the rule before testing. Stupid mistake.
So the code in my previous message works. I also added a null check on the line
Somehow I was getting the null reference error there also. Im not sure how a task can be created without a created by role, but I guess it is possible.
Phil