function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Everton CP7Everton CP7 

Update Owner's Task

I have a WF that creates one task for me "Teste1".

I made one trigger creating task by task.

And I created another trigger to update one of this tasks with same owner's case.

 

It could be the same owner of the first task, cause in my WF is the same user that creates the case.

 

Code task by task:

 

trigger CriarTarefa_Nova_separacao on Task (after update) {
    
    List<Task> taskNova = new List<Task>();       
    for (Task tt: Trigger.New)
        if (tt.subject == 'Teste1' && tt.status == 'Concluído'){
                 taskNova.add (new Task(
                     Subject = 'Teste2',
                     Status = 'Nenhum',
                     WhatID = tt.WhatID,
                     Description = tt.description,
                     ActivityDate = Date.today(),
                     OwnerId = '005K00000015gHE'));
        }
        for (Task tt: Trigger.New)
        if (tt.subject == 'Teste2' && tt.status == 'Concluído){
            taskNova.add (new Task(
                     Subject = 'Teste3',
                     Status = 'Nenhum',
                     WhatID = tt.WhatID,
                     Description = tt.description,
                     ActivityDate = Date.today(),
                     OwnerId = '005K00000015gHE'));
            }
        for (Task t: Trigger.New)
            if (t.subject == 'Teste3' && t.status == 'Concluído'){
                taskNova.add (new Task(
                         Subject = 'Teste4',
                         Status = 'Nenhum',
                         WhatID = t.WhatID,
                         Description = t.description,
                         ActivityDate = Date.today()));
            }
    insert taskNova;
}

 

Code Update OwnerID:

 

trigger Impressao_de_album_task on Task (before insert) {
    
        Map<Id,Case> caseMap = new Map<Id,Case>();
        Set<id> Ids = new Set<id>();
        for (Task tk: Trigger.new) {
        Ids.add(tk.OwnerId);
        }
        
        Map<id,Case> caseMap2 = new Map<id,Case>([Select Id, CreatedbyID from Case Where Id in :Ids]);
        
        for (Task t: Trigger.new)
        if(t.Subject == 'Teste4')
        {
        Case c = caseMap2.get(t.OwnerId);
        
        caseMap.put(c.id,c);
        }
        update caseMap.values();
        
        }

 

My error when I will put "status = concluído" in my task "Teste 3" is:

 

CriarTarefa_Nova_separacao: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Impressao_de_album_task: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Impressao_de_album_task: line 16, column 1: [] Trigger.CriarTarefa_Nova_separacao: line 33, column 1

 

Thanks !

Shiv ShankarShiv Shankar

Work Flow executes after the trigger. since your task is not created through the workflow. may be this region you are getting this error message..

 

You can tract the order in Debug log.

Everton CP7Everton CP7

Thanks Shiv !

 

I open my cases manually and then my task "Teste1" is created with WF.

When I completed task "Teste1" my trigger is fired.

 

How I made my trigger execute task "Teste4" first than my trigger to update owner?

 

I put debug log.

You want to see?