0

I have a Task being created automatically on Contact detail page whenever an email is fired to any contact.

I do not wish the task to be created for that e-mail.

Can we implement this using a trigger or any other way?

3
  • 1
    How are the Task records being created in the first place? It would be far better to alter that logic to never create them than to delete them after the fact.
    – Adrian Larson
    Commented Jan 28, 2016 at 18:15
  • The tasks are getting created from a managed package wherein bulk emails are send to the contacts based on some specific logic. Commented Jan 28, 2016 at 18:40
  • Do they expose any configuration options at all? It would be poor design if you couldn't control that behavior. What managed package is it?
    – Adrian Larson
    Commented Jan 28, 2016 at 18:46

1 Answer 1

1

Hopefully the package allows you to configure this behavior. If you must delete them yourself, you could delete all Task records looking up to a Contact fairly easily in an after insert trigger.

public with sharing class TaskServices
{
    public static void cullUnwantedTasks(List<Task> tasks)
    {
        // call this method from your trigger
        List<Id> unwantedTaskIds = new List<Id>();
        for (Task task : tasks)
        {
            if (task.WhatId != null && task.WhatId.getSObjectType() == Contact.sObjectType)
            {
                // I would strongly recommend augmenting the above criteria
                // hopefully something uniquely identifies your tasks
                // e.g. as having originated from the managed package
                unwantedTaskIds.add(task.Id);
            }
        }
        Database.delete(unwantedTaskIds);
    }
}

Proper error handling is advised around the delete operation, but is ommitted for brevity. You will need to determine what behavior is "proper" in this case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .