Business Rule to set Actual Start Time when the Assigned to is set

JP Marshall
Tera Contributor

I am looking for assistance on how to create a business rule that will populate the "Actual Start (work_start)" time with the current time when the "Assigned to (assigned_to)"  field is populated for both a RITM and an INC. I would ideally like the work_start time to update if/when there is a change to the assigned_to field. I intend on using this to help me identify stale tasks that have not been updated since the actual work_start time. Please help if you have ideas or can assist with the process. 

I will also need a fix-script to update all the records that have already been created. 

Thank you in advance!!!

9 REPLIES 9

Shaqeel
Mega Sage

Hi @JP Marshall 

 

You could try this:

(function executeRule(current, previous /* previous changes */) {
    // Check if the Assigned to field has changed and is not empty
    if (current.assigned_to.changes() && current.assigned_to) {
        // Set the Actual Start field to the current time
        current.work_start = new GlideDateTime();
        current.work_start.update();
    }
})(current, previous);

 

Regards

Shaqeel


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

@Shaqeel some pointers;

Never suggest to use a current.update in a business rule.

You risk ending up with a neverending loop.

 

The if statement in the script could instead be set as part of a condition filter, or condition on when to run the business rule, instead of being part of the script itself. That way the script is only run when needed.

 

Also, on a side note, best practice is to use current.setValue('field_name', 'value') instead of using dot-walked notation.

JP Marshall
Tera Contributor

So is the suggestion I used from Shaqeel not a great solution? If not, what would you suggest instead of this? 

I suggested to use a Flow instead of a business rule, because it's a no-code-solution that does the same thing.

If you still would want to go with the business rule solution, some minor tweaks would be in order (providing example below).

And you would still need to consider the scenario if you really want to reset the actual start value, if a record is first assigned, then emptied from assigned_to then assigned again.

business-rule-set-work-start-condition.png

Do note the additional condition I've added, to only execute if the Actual start value is empty, to prevent an overwrite of existing value.

business-rule-set-work-start-script.png

Script for reference:

(function executeRule(current, previous /*null when async*/) {

 	current.setValue('work_start', new GlideDateTime());
		
})(current, previous);