2

In a Task I have a custom field called "Time frame" of type Number.

I've made a workflow rule with criteria: When the priority of a Task is changed then the number in the "Time frame" field is also changed to particular value.

For example
For each Task priority there is a corresponding hour time frame:

Urgent: 4 hours time frame
High: 8 hours time frame
Normal: 24 hours time frame
Low: 72 hours time frame

I want when the task priority is changed then the field "Time frame" to be set with a different value depending on the task priority mentioned in the example.

Above are working hours but I need to set calendar hours.

I've tried to change the "Time frame" in the afterUpdate(SObject oldSo, SObject so) method - trigger but I get System.FinalException: Record is read-only and I cannot change the record.

Could you please explain how to change the task "Time frame" field when the task priority is changed?

Here is the code in the trigger afterUpdate(SObject oldSo, SObject so)

public void afterUpdate(SObject oldSo, SObject so) {
    Task task = (Task) so;
    System.debug('task.Time_Frame__c ' + task.Time_Frame__c);
    task.Time_Frame__c = 10;
    System.debug('task.Time_Frame__c ' + task.Time_Frame__c);
    update task;
}
2
  • can you post your trigger in this question – Santanu Boral Jul 15 '17 at 21:28
  • trigger is posted the way you requested – sfdev Jul 15 '17 at 21:46
1

Record is readonly in After Update trigger context when you try to assign values directly in those records available from Trigger.new.

So you need to query the Task record again and update the list of Tasks as below.

public void afterUpdate(SObject oldSo, SObject so) 
{
    if(checkRecursive.runOnce())
    {
        Set<Id> taskIds = new Set<Id>();
        for(Task taskObj:so)
        {
            if(/* check any valid conditions*/)
            {
                taskIds.add(so.Id);
            }
        }

        List<Task> lstTask = [SELECT Id, task.Time_Frame__c,..... FROM Task WHERE Id IN:taskIds];

        for(Task tk:lstTask)
        {
            tk.Time_Frame__c = 10;
        }
        update lstTask;
    }
}


public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
        if(run)
        {
             run=false;
             return true;
        }else{
            return run;
        }
    }
}
2
  • After I used the code you suggested I still get error message due to recursion: Review all error messages below to correct your data. Apex trigger TaskTrigger caused an unexpected exception, contact your administrator: TaskTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TaskTrigger: maximum trigger depth exceeded Task trigger event AfterUpdate – sfdev Jul 15 '17 at 22:31
  • there could be other after update operations going on may be other triggers, workflow field updates, or process builder's process which are firing this after update again. So, updated code handles recursion – Santanu Boral Jul 15 '17 at 22:38

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.