function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LuciferLucifer 

Email notification for a task created through a trigger

Hey all,

 

I wrote a trigger which would create a task assigned to a particular field in a record. But I wanted to also send an email notification which I couldn't do like how I did the task.priority, task.subject etc..

 

Below is my code 

 

 

rigger createTask On Annual_Enrollment_Request__c (after insert)
{

List <task> taskToInsert = new List <task> ();

for (Annual_Enrollment_Request__c Are : Trigger.new) {

 

Task task = new Task ();

task.ActivityDate = Are.Date1__c; // and so on so forth untill you map all the fields.
task.Subject = 'Pre-Meeting';
task.OwnerId = Are.Project_Manager__c;
//task.WhoId = Are.Project_Manager__c ;
task.whatId = Are.id;
task.status = 'Not Started';
task.priority = 'Normal';
task.Description = ' Meeting ahead. Got to discuss things!!';


System.debug('***********'+task);
taskToInsert.add(task);

 

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.
try {
insert taskToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}

}

 

 

I found out that apex do not support this way and we need to code it again? If so how? Do I need to include here in the same trigger or write an other trigger? 

Coco_SdyneyCoco_Sdyney

please refer to : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound.htm?SearchType=Stem&Highlight=email

You can send email from trigger, but using EmailMessage instead of task.

 

 

kiranmutturukiranmutturu

to whom that you want to assign this task?

LuciferLucifer

Yes the asignee which is task.ownerid = project_manager__c should get an email notification once the task has created. It would also be great if the same person gets an email alert again may be 2 or 3 days before due date 

kiranmutturukiranmutturu

so you want to send an email to task owner then you can do one thing

 

1. create a workflow and set task creation as action for that and fill in the details in that as you required.......you can select the assigned to in the task action as onwer...and in workflow you will have an option to select send email notfication.

 

then write a before insert trigger on task and identify the task by one unqiue filed like subject in your case to get the exact task that you wnat to work and then change the task owner to project.,manager__c ....(but this should be bulkified in the logical process)  then you can make use of the same for timed actions too.... otherwise you need to use schelable inerface to implement the same.....hope you got what i mean...

LuciferLucifer

Hi Kiran,

 

Its making sence what you tried to tell me but I doubt it if its in my capacity to do it. But I tried to create it in an other way.

 

My first Question is I'm writing two triggers on an object.  The first trigger is creating tasks when ever a record is created. Second trigger is creatng email notifications .

I' putting my second trigger here and its throwing me an error. Could u pls help me..

 

 

 Invalid field activitydate for SObject Annual_Enrollment_Request__c at line 27 column 23

 

 

 

trigger createEmail on Annual_Enrollment_Request__c (after insert)
{
Set<Id> ownerIds = new Set<Id>();

for(Annual_Enrollment_Request__c tsk: Trigger.New)
ownerIds.add(tsk.ownerId);

// Build a map of all users who are assigned the tasks.
Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds]);
for(Annual_Enrollment_Request__c tsk : Trigger.New)
{
User theUser = userMap.get(tsk.ownerId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {theUser.Email};
mail.setToAddresses(toAddresses); // Set the TO addresses
mail.setSubject('A task owned by you has been created'); // Set the subject
// Next, create a string template. Specify {0}, {1} etc. in place of actual values.
// You can replace these values with a call to String.Format.
String template = 'Hello {0}, \nYour task has been created. Here are the details - \n\n';
template+= 'Subject - {1}\n';
template+= 'Due Date - {2}\n';
template+= 'My Test Field - {3}\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.MyTestField__c);

// Here's the String.format() call.
String formattedHtml = String.format(template, args);

mail.setPlainTextBody(formattedHtml);
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}

 

LuciferLucifer

Hi,

 

I managed to get it worked. I wrote one trigger on my annual enrollment and other on task object. Its cool now. But Problem is with the above code I'm unable to get an URL for my email where the user can click it and go to the exact location. 

 

I want the email to be this way

 

To: Lucifer

You  had been assigned the following new task:

Subject:  Meeting
Annual Enrollment Request: ARQ-001045
Due Date: 11/22/2012
Priority: Normal
Comments: Pre-Meeting with All Teams - 3rd week of January Initiate Request - 2/1 Deadline for AE Request Information Submission (Collect Deadline)

For more details, click the following link:

https://cs3.salesforce.com/00TQ000000D4W8J

 

 

But my trigger gives me only following details missing the one's in red

 

Hello Lucifer

Your task has been created. Here are the details -

 

Subject - Pre-Meeting1

Due Date - 11/27/2012

 

 

Could you help frame that.