current.update() in workflow - alternate ?

Ash Ahuja
Tera Contributor

Hello,

Is there any performance issues, if we use "current.update()" on Requested Item table workflow and in run script activity ? the script gets executed after first approval.

Can you please suggest any alternate solution ?

Reason for using "current.update()" is we need TASK SLA on approvals.

-> Once the first approval is completed, I am changing - approval field to approved and again approval to 'requested' by using current.update() in workflow activity so that SLA definition condition gets matched and it will create new TASK SLA record.

But is there any alternate solution ? I read - Workflow Validator - ValidateScriptForCurrentDotUpdate - ServiceNow Wiki

3 REPLIES 3

Harsh Vardhan
Giga Patron

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

You dont need to use current.update() in a run script. All changes into current is automagically updated.


https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/administer/workflow-activities/r...


What the docs do not clearly explain is exactly when the update occurs.   Yes, the update is automatic, but it doesn't occur immediately.   So you could encounter a race condition if you triggered another workflow on a different record and expected it to find the updated value.



For example, assume that current.approval == "requested", and that parent's workflow is currently paused at a "Wait for Condition" activity.   Now consider this script:



current.approval = "approved";


parent = current.parent.getRefRecord();


new Workflow.runFlow(parent, 'update');



What value for approved will the parent's workflow see when it checks the GlideRecord of its child?   Answer: "requested".   Why?   Because the parent's workflow checked the value before the update to the child's record was applied.   This will happen even if you split the above script into two, one that updates the values and a second that triggers the foreign workflow.



To work around this, you have to add something to the workflow that forces the update to be applied.   A 1 second Timer activity is enough to do this, since pausing the workflow for any amount of time seems to cause the update to apply.