ashishsamant
Kilo Guru

Recently came across a limitation of MRVS multi-row variable set that it cannot access the form variables. Meaning if you have a Catalog item form with 'Requested For' variable and an MRVS multi-row variable set, and would like to have the requested_for field value to be carried forward to MRVS scope, it's not possible and that creates limitation for scripting, setting filter criteria for your MRVS variables. Most of you who have worked with MRVS and tried to fetch the values from the form will be able relate better:

I found the answer in using the browser session for storing catalog form values and retrieving them on your MRVS:

//Store the value in the session storage:
sessionStorage.setItem("requested_for", ""+g_form.getValue('requested_for'));
//Retrieving the session value
var req_for = sessionStorage.getItem("requested_for");

 

 

Let's get into details of our use case about 'Requested For' and accessing its value in the 'MRVS'. I have attached the updateset for the explanation below.


After you created the catalog item, variable(requested_for), and the MRVS variable set, start with the below:

  • Step1: Write a Catalog Client Script to store value onChange
    • Name: Store Value req_for (onChange)
    • UI Type: All
    • Type: OnChange
    • Applies to: A Catalog item
    • Catalog item: <Your catalog item name>
    • Variable name: requested_for
    • Script:
      function onChange(control, oldValue, newValue, isLoading) {
         if (isLoading || newValue == '') {
            return;
         }
      
      if (typeof(Storage) !== "undefined") {
        sessionStorage.setItem("requested_for", ""+g_form.getValue('requested_for'));
      } else {
       alert("Sorry, your browser does not support Web Storage...");
      }
         
         
      }​
  • Step2: Write another Catalog Client Script to store value onLoad
    • Name: Store Value req_for (onLoad)
    • UI Type: All
    • Type: OnLoad
    • Applies to: A Catalog item
    • Catalog item: <Your catalog item name>
    • Script:
      function onLoad() {
      	
      if (typeof(Storage) !== "undefined") {
        sessionStorage.setItem("requested_for", ""+g_form.getValue('requested_for'));
      	
      } else {
       alert("Sorry, your browser does not support Web Storage...");
      }
      }​
  • Step 3:Write one last Catalog Client Script to Retrieve value onLoad
    • Name: Retrieve Value req_for (onLoad)
    • UI Type: All
    • Type: OnLoad
    • Applies to: A Variable Set
    • Variable Set: <Your MRVS name>
    • Script:
      function onLoad() {
         
      	var req_for = sessionStorage.getItem("requested_for");
      	g_form.setValue('tb_requested_for',req_for);
      	
      }​
      //Your MRVS should have a variable named 'tb_requested_for', and the value will appear

The above example shows you how to store value from the form and then retrieve it on the MRVS page, you can save multiple form values and retrieve them in MRVS, Once you retrieve these values on the MRVS, you will be able to use it for MRVS.variables filter criteria, or for multiple other reasons.

If you find this article helpful. Please comment and hit like.

HAPPY SCRIPTING!!!

 

Comments
harshald
Tera Contributor

This has helped me. Thanks

Brenda Lichtenb
Mega Explorer
Great option!
kevclark
Tera Contributor

great little solution!  Do you have any idea what limitations the browser session method might have?

ashishsamant
Kilo Guru

Hi kevclark,

Glad the solution helped you and thanks for marking it helpful.

About the session storage limitation, few that are top of my mind are:

  • Its like cache memory, The data is deleted when the user closes the specific browser tab. But that should not affect in most cases, as with the tab closure, the form data also is lost and has to be refilled

  • I have not tested with the 'same browser, multiple tabs' working on the same form.

Thanks,

Ashish

Lahari
Kilo Contributor

Hi All,

I want to display table on Service Catalog. I have tried using Multi-Row variable Set. By default MRVS Order is 100 then  it is visible on form, but the MRVS has to visible in middle of service catalog form. So I have changed order number of MRVS variable set then it is not visible on form. Can any one help me out with this problem.

Suhail4
Tera Contributor

Not sure if you have already got the solution, but if it helps, just number the MRVS in sequence where you want it. For example if you want it between variables with order 100 and 300, make your variable set order as 200.

 

find_real_file.png

JC S_
Mega Guru

Will this solution work on the Now Mobile app?

grzegorzch
Kilo Expert

Hello all, are there any ways to store variables in session using NowMobile app?

grzegorzch
Kilo Expert

Hello all, are there any ways to store variables in session using NowMobile app?

shabbir5
Tera Guru

Hi Ashish , this helped me a lot to populate info into MRVS variable from outside, i tried to apply reference qualifiers using this field , but it is not working . please let me know can we apply reference qualifiers in MRVS ?

Thank you,

Shabbir

michelhanna
Kilo Guru

Starting Quebec, you can now use the g_service_catalog API which is available in all environments, such as, Service PortalNow PlatformWorkspace, and Now® Mobile

https://developer.servicenow.com/dev.do#!/reference/api/rome/client/g_service_catalogClientAPI#g_service_catalog-getValue_S?navFilter=g_service_catalog

michelhanna
Kilo Guru

Starting Quebec, you can now use the g_service_catalog API which is available in all environments, such as, Service PortalNow PlatformWorkspace, and Now® Mobile

https://developer.servicenow.com/dev.do#!/reference/api/rome/client/g_service_catalogClientAPI#g_service_catalog-getValue_S?navFilter=g_service_catalog

GeoffreyOptumOp
Tera Expert

A MRVS Cannot Appear in a Form Section, so if you moved it into a position which would force it into a Form Section, that would cause it to disappear.

radt
Kilo Guru

Hi, I have a it different scenario. I have cost in the MRVS where we add cost as per rows. I want to add the cost and populate in other variable whenever a new row is added with cost.

find_real_file.png

As you can see 100 + 300 should populate in Total cost.

Lets suppose if I add new row with cost 200 then it should automatically calculate 100+300+200 and show it in Total cost.  Is it possible? 

Shaik nabi khaj
Tera Expert

Hi,

It is helped me lot.

It is working in legacy but not in user portal.

Can u help me on this.

Best Regards,

Shaik Nabi Khaja.

jamesmcwhinney
Giga Guru

Did you find a good solution to this?

jamesmcwhinney
Giga Guru
Brijmohan
Tera Contributor

Thanks Ashish for this Solution. It is really helpful. 

I just want to know is there any way to access MRVS variables value in the main form?? 

Sharath Kumar V
Tera Contributor

Mentioned solution works well. Since the post is from 2020, I would suggest an alternative solution using g_service_catalog - getValue(String variableName)

 

Official documentation can be found here

Rashmi02
Tera Explorer

One of the ways of doing this can be:

 

Write a onload client script on the multi row variable set.

Use the method  g_service_catalog.parent.getValue("main_form_variable_name");

Please note you will have to create a hidden variable on the mrvs for storing the value

 

For example, if there is field on the main form which has a name technology.

You want to get the value of the supplier field on mrvs to do any kind of operations.

 

you can write below on load client script in mrvs.

function onLoad() {
   var technologyDetails = g_service_catalog.parent.getValue("technology");
    g_form.setValue('technology_name',technologyDetails);
   
}

 

Community Alums
Not applicable

this is not working iam getting a javascript error in console. with the 3rd script

 

rafaelalves4337
Tera Contributor

@Rashmi02 Thanks for your script, however it's showing the user's Sys id and not the name of the user, how to fix that?

 

function onLoad() {
   var technologyDetails = g_service_catalog.parent.getValue("technology");
    g_form.setValue('technology_name',technologyDetails);
   
}
phil_bool_unifi
Tera Guru

Thanks for this!  Even like 5 years later, this still seems to be the best way to get data from variables on a record producer or catalog item into a multi-row variable set.

phil_bool_unifi
Tera Guru

@rafaelalves4337 , if you want the display name instead you've got two options:  Use the sys_id you're receiving to set the value of a reference field, that way the display value of that reference record will show (ie the Display Name).  Alternatively, if you're just putting the value into a string field, then in the two scripts that set the values in the Storage, replace 'getValue' with 'getDisplayValue', and that'll mean the value stored in the browser cache is the display value instead.

SanketS39847259
Tera Explorer

Will this work only onLoad or onChange too ?

phil_bool_unifi
Tera Guru

@SanketS39847259 you can have this method run whenever a change is made at the catalog item / record producer level.  The real challenge comes when you want to have a decision made in the MRVS be reflected back on the main form.  I was struggling to have any kind of onChange method on the main form that responded to changes in the MRVS (for example, counting the number of rows and displaying that back on the form).  You can't choose the MRVS as a single variable in a client script form.  In the end, I added a custom widget to the form that contained a 'watcher', and used that to monitor for changes to the MRVS record and to set the value.

Version history
Last update:
‎10-31-2020 02:33 PM
Updated by: