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 OpportunityTrigger on Opportunity (before insert, before update, after insert, after update) 
         // 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) 
            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();
            if(listTasksToInsert.size() < 0)
               List<Id> listInsertedTaskIds = new List<Id>();
               Database.SaveResult[] srList = Database.insert(listTasksToInsert, false);
               for(Database.SaveResult sr: srList)
       catch(DMLException ex)
          system.debug('error : ' + ex.getMessage());
    // Mark this method as a future call to prevent it from sending the email twice
    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

APEX Data Loader Date Format Upload Issue (Salesforce.com)

Published by under Blogging,Salesforce.com on Thursday, June 12th, 2014 at 10:00 pm

I really have a bad memory, so I’m blogging this for my future reference.

If you have googled with the keywords in my blog title, then I exactly know the reason why you are here in my post. πŸ™‚ In order to properly upload the correct date using APEX Data Loader, please be mindful of the following settings:

  • Use European date format (dd/mm/yyyy) – Check this if you are using this date format
  • Time Zone – Set this to “GMT”


No responses yet

Invoking Apex from a Custom Button using a Visualforce Page

Published by under Salesforce.com on Monday, February 22nd, 2010 at 4:42 pm

This post is about creating a custom button to be placed in a detail page using Visualforce page instead of an S-control (deprecated). I will be using Opportunity object for this example.

The Controller:

public class VFController
    private final ApexPages.StandardController theController;
    public List<Opportunity> listOpps = new List<Opportunity>();
    public VFController(ApexPages.StandardController stdController)
          theController = stdController;
    // Code we will invoke on page load.
    public PageReference autoRun()
        for (Opportunity opp:[select id, name from Opportunity where id =:theController.getId()])
	        opp.Name = opp.name + ' test';
	return theController.view().setRedirect(true);

The Visualforce Page:

You don’t need to do anything special here. The most important part here is the parameter action = “{!autoRun}”.

<apex:page standardController="Opportunity"
<apex:sectionHeader title="Invoking Apex from a Button"/>
You shoudn't see this page...

After that, go to Setup –> App Setup –> Customize –> Opportunities –> Buttons and Links. Add a new custom button with the following configuration.

This example simply appends “test” to the Opportunity name when you click on that custom button. This is only to show you that it works. Hope this helps. πŸ™‚

One response so far


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

Join other followers: