+ Start a Discussion

Trigger Update Need some idea

I'm having scenario in task object,
If the checkbox "Send Notification Email " checkbox is checked and  "Status" is updated, then send an email to user. If the checkbox is not checked, the user will not receive an email.How to achieve this ?

Right now I'm thinking that through workflow is not possible.
So my second option is trigger.
But here my code is working only if status is updated then user will get mail. In trigger, don't have idea to set condition for checkbox is checked or not..

I'm new to trigger concept pls help to achieve this....

trigger Task_Email on Task (before update) {
    Set<Id> ownerIds = new Set<Id>();
    for(Task tsk: Trigger.New)
    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);    // Set the TO addresses
        mail.setSubject('A task owned by you has been updated');    // 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 modified. Here are the details - \n\n';
        template+= 'Status- {1}\n';
         List<String> args = new List<String>();
        String formattedHtml = String.format(template, args);
        Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});

Please share your ideas.

Thank you
Carolina Ruiz MedinaCarolina Ruiz Medina
Hi sfdcfresher,
You are totally right, as far as I know there is no such field available in API and as you said you can get the result that you are searching by workflows , because even through workflow the "send email" alert / field is not available.  Even if you try to remove it from the Layout to be replace for a custom one it doesn't allow you :( 
Then thinking about it, might be an option is to create a VF page that overrides your task ( insert/ update/ delete ) and emulates the behaviour you would like to get.  I know it is not great but I don't think that there are other options.. I think.
However might be, the option is continue as you are doing at this moment, every time that the user update status  send the notification.  
Lets hope for more answers, I really would like this feature available trough API or workflow too.
Because at this moment what I use when I create a Task through code is this : Database.DMLOptions notifyOption = new Database.DMLOptions();
notifyOption.EmailHeader.triggerUserEmail = true;