You need to sign in to do that
Don't have an account?
SeanCeno
Send Email Notification On Task To A Contact's Internal Owner
All,
I'm writing a trigger to send an email notification to our Internal Owner (a custom field - Internal_Owner__c) of the Contact if a User chooses the Type 'Inbound Queue'. I'm unsure how to:
1.) Add in the SOQL 'Where Task.Type = 'Inbound Queue'.
2.) Send the notification to our Internal Owner of the Contact instead of whom the task is assigned to. Not sure if I need to create a formula field.
Here is my code thus far:
trigger LogACall_Send_Email on Task (before insert) { Set<Id> ownerIds = new Set<Id>(); for(Task tsk: Trigger.New) ownerIds.add(tsk.ownerId); Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds]); for(Task tsk : Trigger.New) { User theUser = userMap.get(tsk.ownerId); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {theUser.Email}; mail.setToAddresses(toAddresses); mail.setSubject('Inbound Queue Call'); String template = 'Hello {0}, \n\nAn Inbound Queue call was taken for you. Here are the details. \n\n'; template+= 'Subject - {1}\n'; template+= 'Due Date - {2}\n'; template+= 'Type - {3}\n'; template+= 'Comments - {4}\n'; String duedate = ''; if (tsk.ActivityDate==null) duedate = ''; else duedate = tsk.ActivityDate.format(); List<String> args = new List<String>(); args.add(theUser.Name); args.add(tsk.Subject); args.add(duedate); args.add(tsk.type); args.add(tsk.Description); String formattedHtml = String.format(template, args); mail.setPlainTextBody(formattedHtml); Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail}); } }
All Answers
Here is your updated code. I put comments on it explaining my steps. You were 3 lines away from having this!
Good Luck!
Can you please follow the below mentioned steps:
1. Iterate over inserted Task records and collect the WhoIds in a set where WhoId is not null && WhoId type is contact && Task.Type = 'Inbound Queue'. Use one more list to store Task records(filtered Task where WhoId is not null && WhoId type is contact && Task.Type = 'Inbound Queue')
2. fetch the Contact and their related Internal Owners using below mentioned query:
Map<id, Contact> mapIdToContact = new Map<Id,Contact>([select Id, Internal_Owner__c,Internal_Owner__r.Email, Internal_Owner__r.Name from Contact where Id In : setcollected in step 1 AND Internal_Owner__c!=null]);
3. Iterate over filetered list collected in step 1 and do the following:
for(Task objTask : filteredlist)
{
if(mapIdToContact.containskey(objTask.WhoId))
{
Contact objContact = mapIdToContact.get(WhoId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {objContact.Internal_Owner__r.Email};
//similar code upto line 24...
At line no 25 use
args.add(objContact.Internal_Owner__r.Email);
//similar code...
}
}
Please let me know if this helps you out.
Luis, your code helped with the 1st problem and now the email only sends when type = 'Inbound Queue', thanks for that!
Pankaj, I'm trying to add this to my code, but I'm still not sure how to properly write it.
1.) In the first step, I'm not sure how to make sure the Who.Type = ContactId
2.) And then I'm getting a "Variable does not exist: WhoId" on line 25.
Here is my code thus far:
At line no.4, you can do following:
for(Task objTask : Trigger.new)
{
if(objTask.WhoId!=null && (objTask.WhoId).getSobjectType == Contact.SobjectType && objTask.Type == 'Inbound Queue')
{
setContactId.add(objTask.WhoId);//declare this set
lstTask.add(objTask);//declare this list
}
}
Map<id, Contact> mapIdToContact = new Map<Id,Contact>([select Id, Internal_Owner__c, Internal_Owner__r.Email, Internal_Owner__r.Name from Contact where Id In : setContactId ANDInternal_Owner__c!=null]);
Now, iterate over lstTask(filtered list not the complete list which is coming from trigger to avoid irrelevent iterations)
for(Task objTask : lstTask)
{
if(mapIdToContact.containskey(objTask.WhoId))
{
Contact objContact = mapIdToContact.get(objTask.WhoId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {objContact.Internal_Owner__r.Email};
//similar code upto line 24...
At line no 25 use
args.add(objContact.Internal_Owner__r.Email);
//similar code...
}
}
I think you need to send email to internal owners of the Contact not the assignTo(owner). If so, you can remove line 6 to 9 from the code.
Please declare setContactId and lstTask before using them like you have declared ownerIds at line no.2
Now I'm getting "Initial term of field expression must be a concrete SObject: Id at line 10
At line no. 10 replace this (objTask.WhoId).getSobjectType == Contact.SobjectType with this one objTask.WhoId.getSobjectType() == Contact.SobjectType()
at line no. 28, use mapIdToContact.containskey(objTask.WhoId)
at line no. 29, use mapIdToContact.get(objTask.WhoId)
Still getting errors. Lines 28 & 29 will compile if I use mapIdToContact.containskey('WhoId').
Line 10 = Method does not exist or incorrect signature: Contact.SobjectType() so I changed it to Contact.SobjectType and it compiles.
Line 28 & 29 = Variable does not exist: objTask.WhoId
Use
tsk.WhoId instead of using objTask.WhoId since you are using tsk as loop variable.
Still, there is redundancy in the code. At line no 2, the set which you have declared in order to contain details of all the owners of Tasks, does not make any use.
You are welcome. I like to help others and to be a helping hand.
One more suggestion,
For checking emptiness of the list, always use isEmpty() method instead of using size() > 0 as it consumes more CPU time.
Sorry to revisit this but I'm having a hell of a time trying to get the send single message part of the trigger covered in my test class. I'm only at 37%. Any way you can help me with that part? The following snippet throws the error:
At this line:
At line no. 25 you are creating an instance of Messaging.SingleEmailMessage class named as mailToSend. At line no. 28 you are using mailToSend.handleInboundEmail(email, env); which is 100% incorrect since Messaging.SingleEmailMessage does not contain such method.
Within Test.startTest() and Test.StopTest() you should create an instance of class(which is your email service) and use that instance to call this method.
Test.startTest();
classname obj = new classname();
obj.handleInboudEmail(email,env);
Test.stopTest();
Line 5 is:
Line 196 in my test class is the insert of the task:
Can you please deploy the code which has been selected as Best answer here?
Please remove code snippet from line no 24 to 36 in your test class since there is no need of this. Have you used seeAllData = true annotation in test class? If so, please remove it from there.
Are you still using this line in your code:
List<Task> taskRecords = new List<Task>([Select Id, WhoId, Who.Type From Task Where WhoId !=null AND Task.Type = 'Inbound Queue']);
If so, Please remove this line and redeploy the artifacts to Production.
So, now is it working fine? If you want you can contact me on facebook:
https://www.facebook.com/pankaj.ganwani.9
I can discuss on your concerns.
Thanks,
Panakj
1.) The page break '\n' in the template works in the Sandbox, but not in Production.
Sandbox Email Example:
Production Email Example:
2.) Is there a way to make the Contact the task is related to a hyperlink to the Contact's page? Here is what I have so far, but it's not working.
At line no. 3 Use:
'Contact -'<a href="'+URL.getSalesforcebaseURL()+'/'+contact.Id'" >'+{1}+'</a>'\n';
expecting a semi-colon, found 'href'
Please use this line instead:
template += 'Contact - '+ '<a href="'+URL.getSalesforceBaseUrl().toExternalForm()+'/'+contact.Id+'">{1}</a>\n';
I've been trying to modify it with this code, but no luck so far as it displays this:
Contact - https://cs8.salesforce.com/WhoId
I just tried below mentioned code in dev console, It is working fine:
Please replace contact.Id with task.WhoId and then try.
Can you please put the system.debug statement to check what you are getting there?
template += 'Contact - '+ '<a href="'+URL.getSalesforceBaseUrl().toExternalForm()+'/'+contact.Id+'">Test</a>\n';
System.debug('========================'+template);
Can you please paste your complete code of the trigger, if possible?
Please try below mentioned code. I made some corrections in there, the contact.Id does not seem to be declared anywhere in the code, so I have added one more string parameter there.
Please replace the code between line no. 35 to 41 with the following:
I got the issue. At line no.92 write this line instead:
mail.setHtmlBody(formattedHtml);
Since we are using setPlainTextbody function so it was not being rendered in the form of html.
Is this possible, but instead of being triggered for a new task, being sent every day at a set time, and to include all open tasks of the recipient (regardless of when they were opened or who owns the record they're related to)?
Thanks,
Elle
try using flow like-
https://www.youtube.com/watch?v=FtjLloAtdlQ