I'm trying to create a custom field that will store the date of the next open activity for standard objects (Opportunity, Contact, Account). If a user completes the task, the date field should clear. If the due date on a task is moved, the field should update. If a task is created that is due earlier than the current task, the field should update to the earlier date.
Is this possible declaratively, perhaps with flows? What is the best approach to handle this requirement?
2 answers
Hi Rob,
I have done this before with a Flow to handle the logic that is called from a Process Builder on the Task object.
For the Flow you will want to pass the recordId in from the Process Builder and do a Get Records for all Tasks with the same WhatID and a status of Open and store those in a collection variable, then Get Record for the Parent Account and store that in a variable.
After getting the records, make a decision node with a decision for if the Query for open tasks was Null or if it returned a list of open tasks.
If it was null then you can make a second decision if the new field to track the date is already null, if so the flow can end there, if not then assign Null to the field and update the parent account.
For the orginial decision, if your query returned records you can then assign a temporary variable in the flow for what the orginial date was in your custom field, we will use this to track when the next activity was prior to this save action.
Next we can create a loop element to loop through the collection variable of the open tasks pulled in from the query and for each item evaluate a decision where if the next activity due date is less than the original activity date then assign that value to the field, if not evaluate the next item.
After looping through the collection you should now have the new temporary variable assigned to the most recent open activity due date.
When the loop ends we will route it to one final decision node that will just check if our new activity due date is different from the original activity due date, if it is update the record, if it isn't then end the flow.
Hopefully that helps, you may need to tweak it slightly for your use case.