Setting, or updating variables in flow designer

Derek C
Tera Guru

Hi all,

I'm trying to update a variable on a task (or ritm) from within a flow in flow designer. What I have done is:

1. Create a custom action called "update TASK variable"

2. created three inputs, one reference type called task that references the sc_task table, one string type called variable, another string type called new value

3. run a simple script to update the variable typed in the inputs

(function execute(inputs, outputs) {
  
  var task = new GlideRecord('sc_task');
  task.get(inputs.task.getUniqueValue());
  task.variables.inputs.variable = inputs.newValue;
  task.update();

  
})(inputs, outputs);

This does not work because you can not use the inputs.variable in the line "task.variable.inputs.variable = inputs.newValue", I also tried "task.variables. + inputs.variable = inputs.newValue" but that does not work either. If I hard code it like "task.variables.approver_instructions = inputs.newValue" that works. I'd rather this action be re-usable for many flows, so I'd rather not hard code it as that makes it limited to a small scope of flows.

Is it possible to pass in a value in the inputs variables and dynamically set variables like this? Or am I out luck here.

Any thoughts / ideas / suggestions are appreciated!

Thanks!

- Derek

1 ACCEPTED SOLUTION

Thanks for the reply, I have had the joy of figuring out the complicated way variables are saved in the past so fortunately what you're saying makes sense.

I didn't want to do it this way, but what I ended up doing is (bonus it's no code!)

1. do a look up record on the sc_item_option_mtom table where "Parent Item" is the RITM (use data pill). Optional AND "Dependent Item.Question.Name" IS ${name value}

2. another look up record on the sc_item_option table where "sys_id" is "Dependent Item.sys_id" of the record found in step 1.

 

That will return the sc_item_option record with the value of the variable that needs to be updated and can be used through the rest of the flow.

Not ideal, but works and is no code. 

I really wish they'd have an OOB action item to update variables, maybe in a future release.

Thanks,

- Derek

View solution in original post

10 REPLIES 10

Hey Baggies,

It looks like when I originally did this I did it as an action rather than a subflow, although either way should work.

I have three inputs:

1. RITM, reference to Requested Item

2. Variable Name, string

3. New Value, string

 

Then three steps:

1. Lookup Record (finds the mtom record matching the RITM input and the Variable Name input)

2. Lookup Record (finds the sc_item_option via the mtom record from step 1). This is the actual variable.

3. Updates the value on the actual variable record found in step 2

 

I have no outputs.

 

See screenshots.

 find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

 

Hopefully this is helpful!

Hi Baggies,

I completely missed your question a few months ago, sorry. So yes, I agree with Derek, outputs are not required. However, as I implemented my solution as a Subflow and not as an Action, I decided to output a simple String type variable called "Result" to indicate if the variable update was successful or if there was an issue encountered.

Like this:

find_real_file.png

If I implemented it today, I would probably do what Jan Kølbæk proposed in this thread: I would implement it a as single-step Action instead of a Subflow. But then I would have to put all such error handling into the Script step itself. In Flow Actions, you cannot do this kind of conditional branching you can see above, so all these checks would have to be scripted.

I hope this helps.

Hi Derek,

 I have a query, My requirement is to set the variable value in RITM for the specific Catalog Item.

I created the trigger as Service Catalog, Action Get Catalog Variable from HR Job Profile (Catalog Item). And selected all the variables there too. But got stuck in the step to set the variable's value.

Suppose, I have a checkbox variable i.e. "ABC" and like to set "True".

I tried with Run Script but still no luck.

var nameOfABCVariable = fd_data._1__get_catalog_variables.abc.getName();

gs.log("abc name: " + nameOfABCVariable);

In log, it's printing as undefined.

However, if I used like this then it returns as true even it is not defined by the user in the RITM record.

var nameOfABCVariable = fd_data._1__get_catalog_variables.abc;

gs.log("abc name: " + nameOfABCVariable);

Therefore, could you please guide or help in setting this up or is there any example available then kindly share.

Quick help must be appreciated.

 

Hello Manish123,

Unfortunately the "Get Catalog Variable" action does not allow you to update the value of a variable. That is because variables store values in a complicated way with several tables being tied together by M2M tables. As Andrew mentioned in a reply above:

"When a catalog item is created, you create Variables assigned to that item, which are stored on a table. There is then a Many 2 Many table, which stores INSTANCES of these variables, and their data. I can never remember exactly what all the tables are called, but start by looking at:

sc_item_option (instances of questions and their values)
item_option_new (what the variable definition is)
sc_item_option_mtom (reference between RITMs and Options)"

Therefore you must do some lookup records on these tables to finally get the table that holds the actual value and then update that record. That is where these lookup record actions come in:

"1. do a look up record on the sc_item_option_mtom table where "Parent Item" is the RITM (use data pill). Optional AND "Dependent Item.Question.Name" IS ${name value}

2. another look up record on the sc_item_option table where "sys_id" is "Dependent Item.sys_id" of the record found in step 1."

Your best bet to accomplish this is to familiarize yourself with those tables and how they work, then follow this post on how to properly look up the records and pass them into custom actions that update the actual value of the referenced variable.

This post may also be helpful in figuring the table hierarchy 

https://community.servicenow.com/community?id=community_question&sys_id=003f5037dbcf23409540e15b8a9619ff 

Good luck!

Ryan66
Tera Contributor

This is awesome, thank you for posting!  Worked like a charm and would've taken me forever to do myself.