1

We have a workflow that creates tasks and assigns them to a specific user when an opportunity is closed. It's salesforce standard behavior that when the account owner is changed, the open tasks are also reassigned to the new account owner. I'm trying to write a trigger that:

  • checks if the account owner and/or task owner is changed FROM 00590000000a3xU
  • if yes, reassign the tasks back to 00590000000a3xU
  • only open tasks and tasks where Orb Team Task (checkbox) is true will be reassigned

I was getting all sorts of errors from the old code even after making the changes sfdcfox's pointed out. So I decided to change my approach. Here's the new and working code:

trigger AssignTaskBackToWarehouse on Account (after update){
List<Task>TasksToUpdate = new List<Task>();
Map<Id,Account>amap = new Map<Id,Account>([SELECT Id, OwnerId,(SELECT Id, Status, Warehouse_Task__c
    FROM tasks
    WHERE Status != 'Completed' AND
    Warehouse_Task__c = true)
    FROM Account
    WHERE Id =: Trigger.newmap.keyset()]);
for(Account a : Trigger.new){
    for(Task t : amap.get(a.Id).tasks){
        t.OwnerId = '00590000000XXXX';
        TasksToUpdate.add(t);
    }
    }
if(TasksToUpdate.size() > 0){
    update TasksToUpdate;
}
}
1

You never re-assigned the ownerid back to the original user. Add kid.OwnerId = '00590000000a3xU'; before adding it to the list. Also, the original query to get the tasks should actually filter by isclosed = false so only open tasks are re-assigned (closed ones won't move).

0
trigger AssignTaskBackToWarehouse on Account (after update){
List<Task>TasksToUpdate = new List<Task>();
Map<Id,Account>amap = new Map<Id,Account>([SELECT Id, OwnerId,(SELECT Id, Status, Warehouse_Task__c
    FROM tasks
    WHERE Status != 'Completed' AND
    Warehouse_Task__c = true)
    FROM Account
    WHERE Id =: Trigger.newmap.keyset()]);
for(Account a : Trigger.new){
    for(Task t : amap.get(a.Id).tasks){
        t.OwnerId = '00590000000XXXX';
        TasksToUpdate.add(t);
    }
    }
if(TasksToUpdate.size() > 0){
    update TasksToUpdate;
}
}

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.