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?
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.
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.