logo

NJP

SOLVED: Issue with MRVS multi row variable set accessing the form values

Import · Oct 31, 2020 · article

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:
    • 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!!!

Labels:

image

View original source

https://www.servicenow.com/community/developer-articles/solved-issue-with-mrvs-multi-row-variable-set-accessing-the-form/ta-p/2319347