1

I have a trigger to create two tasks in a case.

What I need here is to open a new task when both tasks are closed.

I tried some things and I just got opened a new task if just one of the first tasks is closed. And I need both tasks closed to create a new task.

List<Task> tasks = new List<Task>();
List<Case> cse = Trigger.new;

    for (Case c : cse) {
        if (c.motivo__c == 'Solicitação de Pagamento' && c.motivo_a__c == 'Laboratório sistema FF'){
      Task tsk = new Task
          (whatID = c.ID, 
           Subject = 'Malote',
           ActivityDate = Date.today(),
           Ownerid = '005U0000000EtIH');
  tasks.add(tsk);

This is my first trigger. Works fine. I basically do the same thing to create another task in the same case with the subject = "Lançar PP"

I tried create a new trigger in task to create a new task when "Malote" and "Lançar PP" are closed.

        if (t.subject == 'Malote' && t.subject == 'Lançar PP' && t.status == 'Completed'){
      Task tsk = new Task
          (whatID = t.WhatID, 
           Subject = 'Baixar PP',
           ActivityDate = Date.today(),
           Ownerid = '005U0000000EtIH');
  tasks.add(tsk);

And this doesn't work.

1
  • What have you tried? What exactly is not working? Sounds like you're not correctly checking that both tasks were closed.
    – jkraybill
    Commented May 6, 2013 at 13:20

1 Answer 1

1

You need to create some storage to keep information about whether both tasks are closed outside the loop, like:

Boolean firstClosed = false, secondClosed = false

You might also need a list to store your new tasks:

List<Task> newTasks = new List<Task>();

Passing throught the loop, you can then set whether the first task is closed with

firstClosed = true;

And the same for second task:

secondClosed = true

You can check if both tasks are closed, and if so, perform appropriate task:

if (firstClosed && secondClosed) {
    // Task newTask = ...
    // newTasks.add(newTask);
}

When you leave the loop, insert new tasks

insert newTasks;

You must log in to answer this question.

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