0

I have two triggers:

  1. First trigger fires on lead update. It creates a task when lead is converted and links the created task to the contact resulting from the conversion.
  2. Second trigger fires on Task update. It closes all opportunities related to the account of the contact linked in the task.

The problem is that the second trigger must only do its job on tasks created by the first trigger, not tasks created through the UI or any other how.

How can I detect in the second trigger that a task was created by the first trigger, knowing that I don't have enough permissions to :

  • add custom fields to the task object
  • create custom objects that can refer to the task object

Any help is of tremendously appreciated.

2
  • 1
    Question: How do you as a user know that task was created by first the trigger assuming you don't have the knowledge about the process of task creation, without any indicative field.
    – manjit5190
    Apr 22, 2022 at 18:40
  • @manjit5190 I can open the list of all tasks, compare before and after firing the trigger and view the ones I created, but my second trigger can't do this so I need a way to do this knowing I can't create a custom field (I don't have the "new" button in the fields list of the Task object) Apr 25, 2022 at 8:49

1 Answer 1

1

Use a static variable.

// Static variable class
public class SystemFlags {
  public static Boolean tasksCreatedFromTrigger = false;
}

...

// Lead trigger logic
SystemFlags.tasksCreatedFromTrigger = true;
insert taskList;

...

// Task trigger logic
if(SystemFlags.tasksCreatedFromTrigger) {
  updateRelatedOpportunities(taskList);
}

Obviously, this is all just generic code, but the idea is that we can communicate across triggers by using static memory in a shared utility class.

5
  • 2
    Static variables may not work probably because the operations in question are distinct and not within same transaction otherwise there might not be a need for second trigger.
    – manjit5190
    Apr 22, 2022 at 18:39
  • @manjit5190 I made an educated guess regarding this based on the description of the situation. I feel like I'm probably right, but you are correct in saying there may be a chance that these are two separate transactions; the OP didn't specifically state so, though, so I felt it was a safe assumption. We'll see either way.
    – sfdcfox
    Apr 22, 2022 at 18:44
  • What does it mean for the two triggers to be within the same transaction? @sfdcfox Apr 25, 2022 at 8:51
  • @Dr.Simplisist You have a Lead trigger and a Task trigger, correct? So if the Lead trigger creates Task records, the Task trigger will also execute during the execution of the Lead trigger. A Salesforce transaction starts at the very start of a request, such as a Visualforce page action or an API call, called the top level request, and concludes when that request completes. Everything that happens in that request is a transaction.
    – sfdcfox
    Apr 25, 2022 at 10:29
  • @sfdcfox Ah I see now, thank you for the explanation but unfortunately that's not my case, the lead trigger fires when the lead is updated and creates a new task when the update is a lead conversion only and the task trigger fires when the task is updated and closes related opportunities when the task is closed, so they're not in the same transaction. Apr 25, 2022 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.