Hi! I'm Cathy, a Web Designer/Developer/WordPress Fanatic/Salesforce.com Aficionada. I'm from the Philippines and now based in Singapore ...

Sending Email Notification for Task Owner via Apex Trigger

Published by under Blogging,Salesforce.com on Tuesday, March 15th, 2016 at 4:29 pm

I have this business requirement which is when a Salesforce record is inserted and has this specific status, it will auto-create a task and notify the assigned owner via email that he has a new task. So I’ve written a trigger to automate it, during testing I saw that the task has been created and it properly assigns the user until I noticed that it’s not actually sending a notification email unlike when you create a task manually using the standard UI.

I consulted Mr. Google and I stumbled upon this 2-year old developer forum post which discusses the same issue, I tried out the sample code and it seems to work correctly.

For simplicity’s sake, I will be using the standard Opportunity object for the sample code.

Here’s the code snippet:

Trigger:

trigger OpportunityTrigger on Opportunity (before insert, before update, after insert, after update) 
{
   if(trigger.isAfter)
   { 
      if(trigger.isInsert)
      {
         // TriggerHelper is a separate utility class where you can store your static variables 
         // This is used to prevent trigger recursions when there are workflow updates
         if(TriggerHelper.bAfterInsertRun == null) 
         {
            OpportunityHandler.createTask(trigger.new);
            TriggerHelper.bAfterInsertRun = true; // flag the static variable so that it will execute this block of code only once
         }
      }
   }
 ...
}

Class Handler:

public without sharing OpportunityHandler
{
    public static void createTask(List<Opportunity> triggerNew)
    {
       List<Task> listTasksToInsert = new List<Task>();
 
       for(Opportunity objOppty: triggerNew)
       {
           if(objOppty.StageName == 'Follow Up')
           {
              Task newTask = new Task();
              newTask.WhatId = objOppty.Id;
              newTask.Type = 'Follow Up';
              newTask.Description = 'Please follow up this opportunity';
              newTask.Priority = 'Normal';
              newTask.IsReminderSet = true;
              newTask.ReminderDateTime = System.now().addHours(2);
              newTask.Status = 'Not Started';
              newTask.Subject = 'You have a task';
              newTask.ActivityDate = date.Today(); //Due Date
              newTask.OwnerId = Userinfo.getUserId();
              listTasksToInsert.add(newTask);
           }
        }
 
        try 
        {
            if(listTasksToInsert.size() < 0)
            {
               List<Id> listInsertedTaskIds = new List<Id>();
 
               Database.SaveResult[] srList = Database.insert(listTasksToInsert, false);
 
               for(Database.SaveResult sr: srList)
               {
                  if(sr.isSuccess())
                  {
                     listInsertedTaskIds.add(sr.getId());
                  }
               } 
               triggerTaskNotification(listInsertedTaskIds); 
           }
       } 
       catch(DMLException ex)
       {
          system.debug('error : ' + ex.getMessage());
       }
    }
 
    // Mark this method as a future call to prevent it from sending the email twice
    @future 
    public static void triggerTaskNotification(List<Id> taskIds)
    {
       List<Task> taskClone = [Select Id From Task Where Id=:taskIds];
       //Set EmailHeader.triggerUserEmail to true
       Database.DMLOptions dmlo = new Database.DMLOptions();
       dmlo.EmailHeader.triggerUserEmail = true; 
       System.debug('##taskClone: ' + taskClone); 
       //Update Task 
       database.update(taskClone, dmlo);
    }
}

Feel free to comment if it does work or not. 🙂

No responses yet

New Trailhead Release: Navigate the Salesforce Advantage

Published by under Blogging,Salesforce.com on Thursday, March 3rd, 2016 at 11:54 pm

Salesforce has just launched a new Trailhead trail named “Navigate the Salesforce Advantage” which consists of four modules. The objective of this trail is to walk you through Salesforce’s key differentiators that drive their success including their core values, innovative technology, and vibrant ecosystem.

For those who haven’t heard about Trailhead, it is the fun and easier way to learn Salesforce. It is an interactive learning path through the basic building blocks of the Salesforce Platform where you can test your knowledge through challenges, earn points and badges to celebrate your achievements. If you want to learn more about Salesforce, check out this Trailhead link to start your journey.

Now, let’s take a deeper look into each of the modules of the new trail.

trailhead_module_advantage_salesforce_success_model

Salesforce Success Model – Learn who Salesforce is and our vision for driving customer success.

Units: (1) Getting to Know Salesforce; (2) Introducing our Four Core Differentiators – (Innovation, Customer Success, Leadership, Giving Back with 1-1-1)

 trailhead_module_advantage_salesforce_cloud_benefits

Salesforce Cloud Benefits  – Understand the flexibility of a “complete” CRM that evolves with your business needs.

Units: (1) Succeeding with a Complete CRM; (2) Propelling Your Business with the Cloud

 trailhead_module_advantage_salesforce_technology_basics

Salesforce Technology Basics – Get a peek under the hood of our core technology model based on trust and innovation.

Units: (1) Getting Behind the Trusted Cloud; (2) Learning the Value of Multitenancy; (3) Understanding the Power of Metadata; (4) Experiencing Fast App Development and Customization

 trailhead_module_advantage_salesforce_ecosystem

Salesforce Ecosystem – Witness the awesomeness of our incredible partner ecosystem and customer community.

Units: (1) Leveraging our Community, Resources, and Events; (2) Stopping, Collaborating, and Listening;

 

This new trail is beneficial to everyone. It is a great resource for us who have been working in the platform for years and frequently asked by our friends and relatives about what our job is all about.
More importantly, it gives those who are new to Salesforce a great overview of the company, product, technology and the platform ecosystem.

If you are already curious enough to know what sets Salesforce apart from other CRM or on-premise/cloud platforms, head on to Trailhead now!

 

No responses yet

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: