Maik Skoddow
Tera Patron
Tera Patron

find_real_file.png

 

Until today, there is no way to modify the value of catalog variables in Flow Designer using an Action.

But with the help of a small self-developed Flow Action this functionality can be implemented quickly, and this article shows how to do that.

 

At Flow Designer create a new Action "Update Catalog Variable" and then configure it as follows.

 

        
Table of Contents

Inputs

 

find_real_file.png

We need mandatory 3 input variables for this Action: the Requested Item, which holds all catalog variables and the name, as well as the new value.

 

 

Script Step

 

Insert a script step with the following parts:

 

Input

 

find_real_file.png

 

 

Script

 

Enter the following code into the script field:

 

 

(function execute(inputs, outputs) {

  try { 
    var variables = inputs.requested_item.variables.getElements(); 
    
    for (var i = 0; i < variables.length; i++) { 
      var question = variables[i].getQuestion(); 
      
      if (question.getName() == inputs.variable_name) {
        question.setValue(inputs.variable_value);
      }  
    }
    
    inputs.requested_item.update();
    
    outputs.state = 'success';
  }
  catch (e) {
    gs.error(e);
    
    outputs.state = 'error';
  }
  
})(inputs, outputs);

 

 

As you can see, there is a try-catch block to protect the flow from unexpected errors. Furthermore, the script returns an output variable "state", which helps the calling Flow to react on the respective behavior. 

 

 

Outputs

 

The mentioned "state" output has to be configured at this section:

 

MaikSkoddow_0-1666409911625.png

 

 

Testing and Publishing

 

You can easily test that Action with a given Requested Item. And then don't forget to publish your new Action!

 

 

Integration into a Flow

 

After setting the "Service Catalog" trigger you can configure the new Action:

 

find_real_file.png

 

Note:
It is not necessary to insert a "Get Catalog Variables" action, as the new Action updates the Requested Item directly

 

Optionally, you can query the status after executing the Action and branch accordingly:

 

find_real_file.png

 

Comments
OlaN
Giga Sage
Giga Sage

Great post! I will use this one.

Two questions though, should there not be a dot after variables?
What does the bracket notation mean in this case, is variables an array of objects?

  inputs.requested_item.variables[inputs.variable_name] = inputs.variable_value;
//                  like this -->
  inputs.requested_item.variables.[inputs.variable_name] = inputs.variable_value;
Maik Skoddow
Tera Patron
Tera Patron

Hi @OlaN 

nice to hear that this article is helpful for you.

To answer your question: object key "variables" represents a so-called "associative array". So everything is right.

For more information please read https://flexiple.com/associative-array-javascript/

Kind regards
Maik

Jon
Tera Contributor

Hi,

Thank you for this description, it's working on existing RITM's created from Service catalog.

But my issue is to create a RITM in Flow and set some Catalog Item variables.

The RITM created in flow is without any variables and, no matter how much I try, I am not able to set any variables. The Flow reports success, but the log says undefined variable.

The variable section of the RITM created is empty. Is this impossible via Flow?

find_real_file.png

I have tried several options, with and without step 3 with no effect.

find_real_file.png

User332204
Kilo Guru

Hi Jon!

Use the flow action "Submit catalog item request" instead of creating a RITM in step two. This way you will be able to have the variables already "set" in your ritm when you are trying to update them in step 4. Please mark my comment as helpful if it solved your issue.

Rajini2
Mega Sage

Awesome. It helped a lot. Thanks for posting this. 

In my requirement, I need to auto populate 20 variables when a Request is created via Integration. So, should I just make 20 copies of this Action and populate 20 variables or is there way to do all in one action?

User332204
Kilo Guru

Hello Rajin,

 

You still need help with this? 

Shane J
Tera Guru

Hey @Maik Skoddow   - I've tried using this solution (I've seen a similar solution on 3 Community posts) and this part doesn't seem to work properly (the variable is not updated and the log shows that what is being passed is undefined):

inputs.requested_item.variables[inputs.variable_name] = inputs.variable_value;

I have tried the following and it works fine when hard-coded:

inputs.requested_item.variables.variableName = 'bleh';

Not sure what I'm missing here.

 

Gerrity
Tera Expert

Hello All,

 

I too couldn't get this to work when using:

 

 

inputs.requested_item.variables[inputs.variable_name] = inputs.variable_value;

 

 

Instead I had to hard code [inputs.variable_name] to be the name of the variable I needed updated (which removes the flexibility of this action) so for example, I had to set it to:

 

inputs.requested_item.variables["my_variable_name"] = inputs.variable_value;

 

Great solution and would appreciate it if anyone can figure out why [inputs.variable_name] is not working in order to make this more re-usable and dynamic.

 

Cheers!

Tomi Corigliano
Kilo Sage

Hello,

 

The only way I can get this to work is with the following line of code

 

inputs.requested_item.variables.my_variable_name = inputs.variable_value;
Maik Skoddow
Tera Patron
Tera Patron

Hi @Shane J / @Tomi Corigliano / @Gerrity 

 

finally I had the time to check your raised issues and was able to reproduce it.

I have never seen such a weird behavior and have no idea why the codes doesn't work anymore, as it did before. Maybe there is a bug in the Flow engine.

But I found another approach which is now working fine for me. Please let me know if still you have any issues with that new solution.

Maik

Shane J
Tera Guru

The funny/not so funny thing is that your script worked for everyone but me.  ServiceNow support wasn't really able to explain it either but their slimmed down approach in the Script Step does work for me:

 

 

(function execute(inputs, outputs) {
inputs.ritm.variables[inputs.myvar] = inputs.myvalue;
inputs.ritm.update();
})(inputs, outputs);

 

 

ShAn21
Tera Guru

Hi @Maik Skoddow 

Same here. Suddenly stopped working. Very weird behavior. But I did see it working in some rare cases. Not sure what the issue is. Keep us posted if you figure that out.

I tried the new code as well. It does not work for me in Flow Action. But it works in background script

Regards

s_renjarnbo
Tera Expert

Hi @Maik Skoddow 

 

I know your post is old but I have been able to use/re-use most of it.

I would like to be able to update date values as well as string values and are stuggling a bit with it (I am rather new to javascript - sorry)

Here is what I have so far in the action:

s_renjarnbo_0-1688554950139.png

 

s_renjarnbo_1-1688554980124.png

s_renjarnbo_2-1688555033817.png

 

Example of input from ticket:

s_renjarnbo_3-1688555083914.png

 

When I look in the logs I see the following error: "Unparseable date: "01.01.1900": java.text.ParseException: Unparseable date: "01.01.1900": java.base/java.text.DateFormat.parse(DateFormat.java:395)
com.snc.commons.datetime.SNCSimpleDateFormatEx.parse(SNCSimpleDateFormatEx.java:131)"

 

Not really sure what it means and how to get around it.

 

Any help will be appreciated 🙂

 

Regards
Søren

 

 

Shane J
Tera Guru

I'd suggest sending your problematic values to the Log so you know exactly what is being passed.  You may end up changing your Date input to a String instead of Date when all is said and done.  I'm not entirely sure what your end goal is though, since I don't see a .update() anywhere in your script to actually change the variable.

You may need to look at my previous post as well, since I think you're using the original post which myself and others have had issues with.

s_renjarnbo
Tera Expert

Thanks @Shane J 

In my script in line 14 there is an update.

Shane J
Tera Guru

Good call.  😅

 

 

s_renjarnbo
Tera Expert

Can anyone help me out here ?

I have pasted the log error in the original post

Shane J
Tera Guru

Your error reads like the issue is specific to the Date value you're trying to pass.  This was in part why I suggested you change it from a Date type variable to String.  You haven't offered up why you're feeding the Flow '1900-01-01' and your error is coming back with '01.01.1900' as the value.  

I did a Google search for it and found a few things:
https://www.servicenow.com/community/developer-forum/glidedate-converstion/m-p/1686084

https://www.servicenow.com/community/developer-forum/java-text-parseexception/m-p/1931221

 

You may need to manipulate your value at the end of line 7 before trying to use it.

Can you provide screen shots from a test execution?  You've given us what you're doing, but only a part of the result. 

 

 

 

s_renjarnbo
Tera Expert

@Shane J 

Thanks for the links - I will look at them.

I did originally pass the date field as a string and that did not work either hence I changed the passing to a date field.

I am suspecting that the code - as it is - cannot handle dates but only string values.

Thanks again

Dakker
Tera Guru

Thanks for the post, however, I could not make the script part work so I wrote my own.

 

Dakker_0-1691770482545.png

 

Dakker_1-1691770493525.png

Here is the script:

 

(function execute(inputs, outputs) {
try {
outputs.state = 'fail';
var recordFound = false;
var grRitm = new GlideRecord('sc_req_item');
grRitm.get(inputs.ritm);

var grItemOptionNew = new GlideRecord('item_option_new');
grItemOptionNew.addQuery('name', inputs.cat_var);
grItemOptionNew.addQuery('cat_item', grRitm.cat_item);
grItemOptionNew.query();

if (grItemOptionNew.next()) {
var grItemOptionMtom = new GlideRecord('sc_item_option_mtom');
grItemOptionMtom.addQuery('request_item', grRitm.sys_id);
grItemOptionMtom.query();

while (grItemOptionMtom.next() && !recordFound) {
var grItemOption = new GlideRecord('sc_item_option');
grItemOption.addQuery('item_option_new', grItemOptionNew.sys_id);
grItemOption.addQuery('sys_id', grItemOptionMtom.sc_item_option);
grItemOption.query();

if (grItemOption.next()) {
grItemOption.value = inputs.var_value;
grItemOption.update();
recordFound = true;
outputs.state = 'success';
}
}

}

outputs.state = 'success';
}
catch (e) {
gs.error(e);
outputs.state = 'error';
}

})(inputs, outputs);

 

 

 

Melinda Boone
Tera Explorer

Looks like the catalog variables cannot be reached on RITM.  Change code to use a reference to sc_task and it works.  I changed code to use the syntax from Shane's post:

MelindaBoone_0-1695741493421.png

 

inputs.catalog_task.variables[inputs.variable_name] = inputs.variable_value;
inputs.catalog_task.update();

Vijay Venkatara
Tera Expert

I have a similar question like lot of you has asked above. I do have a custom table which contains couple of fields in an variable set.

It is basically a table inherited from CustomerService Table called "x_custserv_case". It has field called "cust_id".

We are able to use the Get Catalog variables to get the current value of that field. We want to update it once we get the value as we want to keep the field clean.

Does anyone have any suggestion on we can achieve that goal?  

Please note the record we are trying to update is not a request item or service catalog task. It is a table that belongs to the Customer Service Module inherited from the sn_customerservice table.

James Fricker
Tera Guru

I have found that I don't need the for loop. I can just do this

 

inputs.requested_item.variables[inputs.variable_name].setValue(inputs.variable_value);
inputs.requested_item.update();

 

this alternative version also works

inputs.requested_item.variables[inputs.variable_name] = inputs.variable_value;
inputs.requested_item.update();

 

Version history
Last update:
‎10-21-2022 08:39 PM
Updated by:
Contributors