logo

NJP

Create dynamic Multi-Row Variable Set form by accessing data in the Catalog Item

Import · Feb 15, 2021 · article

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

Hi there,

Wouldn't it be awesome, to create a dynamic Multi-Row Variable Set form, based of the contents of the Catalog Item? Up to the Paris release, this was limited possible. Reading through the Quebec release notes again and again I noticed… "Create a dynamic MRVS form by accessing all data in the catalog item form".

That sounds interesting, though also what is it exactly about? Let's have a closer look!

Accessing Catalog Item data within Multi-Row Variable Set

Paris release

When setting up a Multi-Row Variable Set, you'll notice that within the Catalog UI Policy and Catalog Client Script, there's no option to select a Variable from outside the Multi-Row Variable Set. Also, scripting like g_form.getValue() won't get data from Variables outside the Multi-Row Variable Set.

So basically, if you would stick with the above methods, accessing Catalog Item data within a Multi-Row Variable Set is not possible. There are some exceptions, one for example when automatically populating Multi-Row Variable Set records.

At least… this is how it is up to the Paris release.

Quebec release

So what has changed with the Quebec release then, changed so you can access Catalog Item data within a Multi-Row Variable Set? Let's skip Catalog UI Policies (no changes that a noticed there), and going straight to Catalog Client Scripts. The Docs mentions a new API, "g_service_catalog". An API that also works in all environments, such as, Service Portal, Now Platform, Workspace, and Now Mobile. The API is described as "The g_service_catalog API enables you to access data in a multi-row variable set (MRVS) when a model is open. This API is available in all environments, such as, Service Portal, Now Platform, Workspace, and Now® Mobile".

The g_service_catalog_api has only one method available (or one documented, there might be more, who knows), method "getValue()" which can be used like:

g_service_catalog.parent.getValue("your_variable")

Example usage

Let's give it a go. For example, having a Catalog Item with a "Decision" Variable (of type Yes / No), and based on the Decision set in the Catalog Item, having certain Variables within the Multi-Row Variable Set modal visible or maybe even containing certain pre-filled data.

Multi-Row Variable Set Variables

Adding two Variables to the Multi-Row Variable Set:- Show Yes

- Show No

Both of type Single Line Text. Let's immediately tick the "Hidden" checkbox on these Variables. This would make the Variable concerned hidden onLoad (hiding a Variable onLoad is new in the Quebec release, read about it in one of my recent articles: New "Hide" variable field (onLoad)).

Basic onLoad Catalog Client Script

A basic onLoad Catalog Client Script within the Multi-Row Variable Set, responding to the Decision Variable from the Catalog Item, could be something like:

function onLoad() {

    var decision = g_service_catalog.parent.getValue("decision");

    if(decision == "Yes") {
        ...
    } else if(decision == "No") {
        ...
    }

}

Creating a Dynamic Multi-Row Variable Set form (1)

Let's enrich the If statement, making the Variables concerned visible.

if(decision == "Yes") {
    g_form.setVisible('show_yes', true);
} else if(decision == "No") {
    g_form.setVisible('show_no', true);
}

Just simply using good old g_form.setVisible().

Creating a Dynamic Multi-Row Variable Set form (2)

Can we do more? Just for fun, just exploring… let's test with pre-filling the Variable and adding an Info or Error Message.

if(decision == "Yes") {
    g_form.setVisible('show_yes', true);
    g_form.setValue('show_yes', 'Showing the Yes Variable…');
    g_form.addInfoMessage('YES!');
} else if(decision == "No") {
    g_form.setVisible('show_no', true);
    g_form.setValue('show_no', 'Hope this will show the No Variable…');
    g_form.addErrorMessage('NO!');
}

Result

Nice all this theory, though what is the actual result of this?!

image

After selecting Yes, and opening the Multi-Row Variable Set modal:

image

After selecting No, and opening the Multi-Row Variable Set modal:

image

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

Kind regards,

Mar k Roethof

ServiceNow Technical Platform Architect @ Quint Technology

2x ServiceNow Developer MVP

2x ServiceNow Community MVP

---

LinkedIn

image

View original source

https://www.servicenow.com/community/developer-articles/create-dynamic-multi-row-variable-set-form-by-accessing-data-in/ta-p/2302529