use current.varable_name and previous.variable_name functionality in a workflow?

khurdle
Kilo Contributor

I am scripting within a workflow and am trying to trigger an action based on if a boolean field value has changed.   Since workflow activities do not have the built in "change" condition like a business rule, I am attempting to do this with a condition.   In an "IF" workflow activity, I have " if (current.variable_name == true &&   previous.variable_name == false) {run script} ".   The function is not able to enter this statement, because it does not recognize the use of current.variable_name or previous.variable_name.   Can I use these key words in a workflow script?

ifstatement.PNG

Any insight appreciated!

5 REPLIES 5

Rama Chandra D
Kilo Guru

Hi Katie,



Did you try the condition builder to see if   'changes' criteria is already exists? I don't think, previous object is defined on workflow context, since the workflow is instantiated later or on a condition. You can implement your requirement in a BR and the desired workflow can be triggered for the BR.



Darshak


Check out the API for variables using Xplore.


You can get the appropriate object as follows:



var gVariable = current.variables.variable_name.getGlideObject()



Then, to get the previous value you can do this:



gVariable.previousValue



I haven't tested this code in practice, as it's not documented, but it just might work!



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

The condition builder does not have "changes" as an option for Workflow scripts.   I believe I have only seen this as a built-in condition option on Business Rules.   I think I will have to go with a BR for simplicity sake to get the functionality I am looking for.


amlanpal
Kilo Sage

Hi Katie,



You must try as current.variables.variable_name. This is the syntax. Also I doubt that you can not use 'previous' in the Workflow. Please have a look at this wiki LINK.



I hope this helps. Please mark correct/helpful based on impact