You need to sign in to do that
Don't have an account?
Mike Simmen
Trigger to Update Account from Task when whatID is null
Hello all,
I have been working on writing an apex trigger to update an account date field when a task that meets certain criteria is entered. The code works great when you have a whatID. The problem though is when you use email to salesforce, the emails are only attached to the whoID and leaves the whatID null. I need to find a way to modify my code to look at the whoID and determine what account it is associated with and then update that account. I need some help. Take a look and let me know your thoughts. Thanks.
trigger updateAccountEngagement on Task (before insert, before update) {
List<Id> accountIds=new List<Id>();
for(Task t:trigger.new){
if(t.Status=='Completed'){
if(t.whatId != null && (String.valueOf(t.whatId).startsWith('001')==TRUE || String.valueOf(t.whatId).startsWith('006')==TRUE) && (String.valueOf(t.subject).startsWith('Email: Re:')==TRUE || String.valueOf(t.subject).startsWith('Email: Fw:')==TRUE || t.Engagement__c==TRUE)){//check if the task is associated with an account
accountIds.add(t.whatId);
}//if 2
}//if 1
}//for
List<Account> accountsToUpdate=[SELECT Id, Last_Engagement_Date__c FROM Account WHERE Id IN :accountIds];
For (account a:accountsToUpdate){
a.Last_Engagement_Date__c=date.today();
}//for
try{
update accountsToUpdate;
}catch(DMLException e){
system.debug('Accounts were not all properly updated. Error: '+e);
}
}//trigger
I have been working on writing an apex trigger to update an account date field when a task that meets certain criteria is entered. The code works great when you have a whatID. The problem though is when you use email to salesforce, the emails are only attached to the whoID and leaves the whatID null. I need to find a way to modify my code to look at the whoID and determine what account it is associated with and then update that account. I need some help. Take a look and let me know your thoughts. Thanks.
trigger updateAccountEngagement on Task (before insert, before update) {
List<Id> accountIds=new List<Id>();
for(Task t:trigger.new){
if(t.Status=='Completed'){
if(t.whatId != null && (String.valueOf(t.whatId).startsWith('001')==TRUE || String.valueOf(t.whatId).startsWith('006')==TRUE) && (String.valueOf(t.subject).startsWith('Email: Re:')==TRUE || String.valueOf(t.subject).startsWith('Email: Fw:')==TRUE || t.Engagement__c==TRUE)){//check if the task is associated with an account
accountIds.add(t.whatId);
}//if 2
}//if 1
}//for
List<Account> accountsToUpdate=[SELECT Id, Last_Engagement_Date__c FROM Account WHERE Id IN :accountIds];
For (account a:accountsToUpdate){
a.Last_Engagement_Date__c=date.today();
}//for
try{
update accountsToUpdate;
}catch(DMLException e){
system.debug('Accounts were not all properly updated. Error: '+e);
}
}//trigger
All Answers
Since you are performing DML on account object inside a task trigger, i would suggest you update the trigger context to 'after insert,after update'.
The code works great when you are logging activity manually from the contact level. However whenever you try to use email to salesforce, the trigger does not fire and does not update the Last_Engagement_Date__c field at the account. That is the ultimate goal. Is this possible?
I hope this should help you
Review all error messages below to correct your data.
Apex trigger updateAccountEngagement caused an unexpected exception, contact your administrator: updateAccountEngagement: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateAccountEngagement: line 16, column 1