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
DeptonDepton 

issue with trigger that changes closed tasks owner

I have this trigger on accounts wich is based on a custom look up field (users) Gestor_a__c

 

If Gestor_a__c has any open task related to an account,  when I change the acount Gestor the ownership of the open task related to him/her also change to the new Gestor.

 

It is working fine, but it also changes completed tasks??

 

what I am mising?

 

 

trigger reAssignTask on Account (after update) 
{
    Set<Id> accId = new Set<Id>();
    MAP<Id , ID> accId_NewMangID  = new MAP<Id , ID>();
    Set<Id> oldManagerId = new Set<Id>();
    for(Account a : trigger.new)
    {
        if(a.Gestor_a__c != trigger.oldMap.get(a.id).Gestor_a__c)
        {
            accId.add(a.id);
            oldManagerId.add(trigger.oldMap.get(a.id).Gestor_a__c);  
            accId_NewMangID.put(a.id , a.Gestor_a__c);
        }
    }
    
    List<Task> listTask = [Select id , OwnerID , whatID From Task where whatID in: accId and OwnerId in: oldManagerId];
    
    for(Task t : listTask)
    {
        t.OwnerID = accId_NewMangID.get(t.whatID);
    }
    update listTask;
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

try this

 

trigger reAssignTask on Account (after update) 
{
    Set<Id> accId = new Set<Id>();
    MAP<Id , ID> accId_NewMangID  = new MAP<Id , ID>();
    Set<Id> oldManagerId = new Set<Id>();
    for(Account a : trigger.new)
    {
        if(a.Gestor_a__c != trigger.oldMap.get(a.id).Gestor_a__c)
        {
            accId.add(a.id);
            oldManagerId.add(trigger.oldMap.get(a.id).Gestor_a__c);  
            accId_NewMangID.put(a.id , a.Gestor_a__c);
        }
    }
    
    List<Task> listTask = [Select id , OwnerID , whatID From Task where whatID in: accId and OwnerId in: oldManagerId And Status != 'Completed'];
    
    for(Task t : listTask)
    {
        t.OwnerID = accId_NewMangID.get(t.whatID);
    }
    update listTask;
}

 

 

 

All Answers

MVJMVJ

Try adding a condition to you select to only include open tasks.

Shashikant SharmaShashikant Sharma

try this

 

trigger reAssignTask on Account (after update) 
{
    Set<Id> accId = new Set<Id>();
    MAP<Id , ID> accId_NewMangID  = new MAP<Id , ID>();
    Set<Id> oldManagerId = new Set<Id>();
    for(Account a : trigger.new)
    {
        if(a.Gestor_a__c != trigger.oldMap.get(a.id).Gestor_a__c)
        {
            accId.add(a.id);
            oldManagerId.add(trigger.oldMap.get(a.id).Gestor_a__c);  
            accId_NewMangID.put(a.id , a.Gestor_a__c);
        }
    }
    
    List<Task> listTask = [Select id , OwnerID , whatID From Task where whatID in: accId and OwnerId in: oldManagerId And Status != 'Completed'];
    
    for(Task t : listTask)
    {
        t.OwnerID = accId_NewMangID.get(t.whatID);
    }
    update listTask;
}

 

 

 

This was selected as the best answer
DeptonDepton

This time I was not too far away, i did it like you said before u told me but didn´t work because the org instead of completed the value had a similar but different name

 

Now it works like a charm!!

 

Thanks for your help my friend!!

Shashikant SharmaShashikant Sharma

Your welcome my friend,

la bienvenida a mi amigo :)