I need help to achieve the following:
When a Task priority is changed by a particular user, I must set a fixed amount of REAL (non-working) hours in a custom field in the same Task.

The purpose is to trigger and to send an e-mail alert to the task assignee after this fixed amount of REAL hours.

For each Task priority there is specific amount of working hours:
Urgent - 4 working hours
High - 8 working hours
Normal - 24 working hours
Low - 72 working hours

However I need the real hours.
For instance, when user select "Urgent" and the current date/time is Tuesday 17:00h, then I must set 4 working hours which means: 1 working hour from the current day (Tuesday) and 3 working hours from the next day (Wednesday).
Thus the end of the Task should be at 12:00h on Wednesday.

When calculated the REAL hours: from Tuesday 17:00h till Wednesday 12:00h there are 19 real hours.

I have to set this number 19 (REAL hours) into the custom field.

I also need to take the company working hours from the database table "BusinessHours" (each company may have different working week).

How can I calculate the REAL hours based on the selected Task priority?

Note: I have wrote an algorithm in apex which performs the mentioned calculations but...
1. If I invoke it from "beforeUpdate" trigger, then the new Task priority is not set (committed) yet.
2. If I invoke it from "afterUpdate" trigger, I have the new value but I can not make 'update' on the current Task (it is read-only because we are already in the update procedure/trigger).

Please advise.

Any help and appropriate suggestion is appreciated.

2 Answers 2


I would suggest to do it Before update since you'll save Dml operations.

Something like:


for(Task T: (List<Task>)Trigger.new) {
    if(T.Priority!=NULL && T.real_hours__c == NULL){
        T.Real_Hours__c = CalculateHours(T);

**1. If I invoke it from "beforeUpdate" trigger, then the new Task priority is not set (committed) yet.

But you can still work with it since the value it's on the trigger.new object. Therefore BeforeUpdate is the best solution. You just ignore all those who doesn't have priority and compute those who have it. If the changes from Priority aren't commited in the end(some exception/Error) the changes in RealHours wouldn't be neither.

If you what to do it afterUpdate just to ensure that the changes were committed you can just do the changes and send the List<Task> to a Queuable Method to update them after asynchronously.


@sfdev, As per my understanding you have all the calculations in your trigger but not getting the task update with respect priority of the task. It would be easy with formula field to calculate the hours but you mentioned that you need to query on 'BusinessHours' to get calculate the REAL hours. Here before trigger would be the good solution as you are updating the task field it self by changing the priority of the task. if possible please share your code so that can debug and let you know where it is not working. Or you can also debug to whether the trigger is getting fired or not, which is the line it is not getting run or the error.

  • Hi Sukanya, once I execute my code with Task task = (Task) oldSo I get an error. If however I use Task task = (Task) so, the is not committed yet. Please advise how to get the proper value of the task in beforeUpdate trigger?
    – sfdev
    Jul 25, 2017 at 14:44

You must log in to answer this question.

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