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
Alex Sherwood 1Alex Sherwood 1 

Update Task Priority

I'm completely new to Apex (or coding of any kind) and have been trying to figure out how to write a trigger to update the priority of certain tasks for the past couple of days but I don't think I've come close.

The problem that I have is that I can't find a basic enough example of a trigger, which only performs the one action and isn't referening another object, to use as a template.

Ideally I'd like the trigger to just update the task, without referencing accounts or opportunities etc. so that I can adapt it to update any field on any object.

The task will be triggered using the new process builder feature. The new priority should be high. If it is necessary to reference another object, then these tasks will be related to opportunities.

I have written the rest of the trigger but it's just fragments of code pulled from various sources and probably makes no sense. The part that I'm really struggling with is selecting the list of selected records from my SOQL query so that I can update them.
 
trigger TaskPriorityUpdate on Task (before update) {
    
    for (Task myTasks : Trigger.new)  {
        
        List<Task> tasksToUpdate = [SELECT Priority FROM Task WHERE RecordTypeId = '012700000005ofQ' AND Status != 'Completed' AND Activity_Type__c = 'Administration' AND Subject = 'Opportunity Close Date Check'];
             
        if (tasksToUpdate.size() > 0) {
                 
            update tasksToUpdate.Priority__c = 'High';
             }
    }
}

I'd really appreciate any guidance!
Waqar Hussain SFWaqar Hussain SF
Hello Alex..
try the following code, it will solve your problem.
trigger TaskPriorityUpdate on Task (before update) {
 
    List<Task> tasksToUpdate = new List<Task>();
  
    for (Task myTasks : Trigger.new)  {
        
             if (myTask.RecordTypeId == '012700000005ofQ' && myTask.Status != 'Completed' && myTask.Activity_Type__c == 'Administration' && myTask.Subject == 'Opportunity Close Date Check')
			{
            myTask.Priority__c = 'High';
            tasksToUpdate.add(myTask);
			}
			
    }
	if(tasksToUpdate.size() > 0){
		update tasksToUpdate;
	}
}

pls let me know if it works
StephenKennyStephenKenny
Hi Alex,

Have you considered not using a trigger? Remember you will also have to write a test method to go along with the trigger. From what you have described above you may well be able to achive the same outcome using a simple workflow rule and field update? This would mean that you dont need to write any code at all.

Take a look at the help docs, which do have some pretty good examples and tutorials for various business processes to help get you going:
https://help.salesforce.com/HTViewHelpDoc?id=workflow_examples.htm&language=en_US

Let us know how you get on.
 
Alex Sherwood 1Alex Sherwood 1
Hi Vickey,

Thanks a lot for this - to translate this is saying; if the task matches my criteria, then update it's priority and add it to the list tasksToUpdate and if the size of that list is greater than 0, save the tasks with the updated priority (please let me know if I've missed any key elements)?

It'll probably take me a while to write the test class but once I do, I'll let you know if it's worked!
Alex Sherwood 1Alex Sherwood 1
Hi Stephen,

Thanks for the suggestion, the reason why I didn't use a workflow is that the process I've created is linked to the Opportunity object, as opposed to Tasks. It needs to be set up this way because I'm testing the number of days between today & the close date, along with the opportunity stage and then triggering actions - mainly in workflows - based on the results.

This Apex Trigger will be triggered at the Task Complete? step in the process - I want the task which I've created to then be marked as high priority. Please do let me know if I'm missing something here though!

Process Builder Stage & Close Date Query

 
StephenKennyStephenKenny
Hi Alex,

Now that I understand the context, this makes it easier to help you. Did you know that you can use the process builder to update the related task records? I would recommend that you do this which means that you do not need to write the trigger. There is a basic example here to get you going but essentially you just need to create a new action to update related records. Then input the filter criteria you have mentioned above (Status != completed etc...)
https://help.salesforce.com/HTViewHelpDoc?id=process_example.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=process_example.htm&language=en_US)

Let me know how you get on
Alex Sherwood 1Alex Sherwood 1

Thanks again Stephen, to be precise, the limitation of the process builder (I think) is that I can't query the related task at the Task Complete? step to check whether or not it's been marked as complete. Which is why Status != 'Completed' needed to be included in the code and why I also need to check the title in the Apex query. This allows me to make sure that I'm not updating all open tasks on the opportunity - usually there will be an open follow up activity and the administration task.
Alex Sherwood 1Alex Sherwood 1
I'm amazed that Salesforce haven't found a way to enable this functionality though, as it seems like a very obvious limitation of the process builder when it comes to updating related records which were created as part of the process..
StephenKennyStephenKenny
HI Alex,

Ah, I see what you mean now and appologies for sending you down the wrong track. Yes the process builder is stil quite young and limited in this regard but I am sure will improve over the coming realeases. For now I guess that you will indeed have to write a trigger to achieve this.
 
Alex Sherwood 1Alex Sherwood 1
No worries Stephen, it's good to have someone else trouble shoot my assumptions and make sure that using Apex really is necessary, especially when I need to learn so much in order to use it!