You need to sign in to do that
Don't have an account?
ckellie
How to limit Task Trigger to run only when related to PCN object
The rtrigger below should be sending an email message only for tasks with a status completed and related to the PCN object. If the task is not related to the PCN object, then the trigger should not send an email. But when a task is marked completed and the task is not related to the PCN record, the trigger is throwing an error. I have not been able to write the condition so the trigger does not throw an error for tasks not related to the PCN record. Please help?
Below is the trigger:
trigger PCNQuoteRequestComplete on Task (before Update) { Set<Id> taskid = new Set<Id>(); Set<Id> uid = new Set<Id>(); String pcnid; string first3Letter; List<user> marketo = [select id from user where lastname = 'marketo' limit 1]; for(task pcnids: trigger.new){ pcnid=pcnids.whatid; system.debug('/////////' + pcnid); if (pcnid!= null) first3Letter=pcnid.substring(0,3); system.debug('+++++++++' + first3Letter); } for(task pcnact: trigger.new){ taskid.add(pcnact.id); uid.add(pcnact.createdbyid); } Task completedtask = [select id, createdbyid, BV_PCN_Quote_Request_Complete_Ckbx__c, whatid, subject from task where id =: taskid]; system.debug('id -----' + completedtask); List<User> uu = New List<User>([select Email from user where isactive=:true and BV_SO_Admin_Assist__c =: true limit 1]); System.debug('###User'+UU); Map<id, User> fromu = New Map<Id, User>([select id, email, Full_Name__c, isactive from user where isactive=:true and Id =:completedtask.createdbyid limit 1]); system.debug('id -----' + pcnid); Map<id, pcn_s__c> pwhat = New Map<Id, pcn_s__c>([select id, name from pcn_s__c where id =:pcnid]); integer i=0; for(Task Emailto: trigger.new){ if(first3Letter=='a09' && fromu.get(completedtask.createdbyid).id <> marketo[0].id ){ system.debug('>>>>>>>>>>>>>>>>>>>>>' + first3Letter); If(emailto.status=='Completed' && emailto.subject=='PCN Requires Quote' && emailto.BV_PCN_Quote_Request_Complete_Ckbx__c==false && uu.size()>0){ emailto.BV_PCN_Quote_Request_Complete_Ckbx__c = true; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); string EmailAddress; EmailAddress = uu[i].email; System.Debug('ToEmailAddress###########'+EmailAddress ); System.Debug('ToEmailAddress###########'+EmailAddress ); String[]toAddresses = new string[]{EmailAddress}; mail.setToAddresses(toAddresses); mail.setReplyTo(fromu.get(completedtask.createdbyid).email); mail.setSenderDisplayName(fromu.get(completedtask.createdbyid).full_name__c); mail.setSubject('PCN Quote Completed'); mail.setBccSender(false); mail.setUseSignature(false); mail.setHtmlBody('Quote request for ' + pwhat.get(pcnid).name + ' has been completed.' ); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); }i = i + 1; } } }
Thank you
Hi,
Try this, I modified your trigger slightly
trigger PCNQuoteRequestComplete on Task (before update){
Set<Id> taskIds = new Set<Id>();
Set<Id> userIds = new Set<Id>();
Set<Id> pcnIds = new Set<Id>();
for (Task task : Trigger.new){
// Checks task is related to pcn_s__c object
if (task.WhatId.getsObjectType() == pcn_s__c.sObjectType){
taskIds.add(task.Id);
userIds.add(task.CreatedById);
pcnIds.add(task.WhatId);
}
}
if (!pcnIds.isEmpty()){
Map<Id, User> mapUsers = new Map<Id, User>([SELECT id, email, Full_Name__c,isactive FROM user WHERE isactive=:true and Id IN:userIds]);
Map<Id, pcn_s__c> mapPcn = new Map<Id, pcn_s__c>([SELECT id, name from pcn_s__c where IN : pcnIds]);
List<User> rsUsers = [SELECT Email FROM user WHERE isactive=:true and BV_SO_Admin_Assist__c =: true limit 1];
for (Task task : Trigger.new){
if (task.WhatId.getsObjectType() == pcn_s__c.sObjectType){
if(task.status=='Completed' && task.subject=='PCN Requires Quote' && task.BV_PCN_Quote_Request_Complete_Ckbx__c==false && rsUsers.size()>0){
task.BV_PCN_Quote_Request_Complete_Ckbx__c = true;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
string EmailAddress;
EmailAddress = rsUsers[0].Email;
String[]toAddresses = new string[]{EmailAddress};
mail.setToAddresses(toAddresses);
mail.setReplyTo(mapUsers.get(task.CreatedById).email);
mail.setSenderDisplayName(mapUsers.get(task.CreatedById).Full_Name__c);
mail.setSubject('PCN Quote Completed');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody('Quote request for ' + mapPcn.get(task.WhatId).name + ' has been completed.' );
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
}
}
I recieved this error:
PCNQuoteRequestComplete: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.PCNQuoteRequestComplete: line 9, column 1".
this is pointing to line: if (task.WhatId.getsObjectType() == pcn_s__c.sObjectType){
Here is the entire trigger: