Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

Populate a multi-row variable set - in Workspace view

sk4408bc
Tera Contributor

In Workspace(Next Experience UI), how to populate a multi-row variable set of a record producer? 
I tried using a catalog client script, but that works only in portal view, not in Workspace. Please could someone help? 

1 ACCEPTED SOLUTION

HasanM
Kilo Sage

Hi @sk4408bc ,

You can populate a multi-row variable set (MRVS) from a catalog client script on the record producer. Pass the MRVS internal name and a JSON string of the rows to g_form.setValue(). Each key in a row must be the internal name of a variable inside the MRVS.

function onLoad() {
var rows = [
{ "item_name": "Laptop", "quantity": "1" },
{ "item_name": "Monitor", "quantity": "2" }
];
g_form.setValue('my_mrvs', JSON.stringify(rows)); // MRVS internal name
}

Replace 'my_mrvs' with your MRVS internal name, and 'item_name' / 'quantity' with the internal names of the variables inside it.

If it works in Service Portal but not in Workspace, check the following:

1. UI Type on the catalog client script is set to "All". A "Desktop" script will not run in Workspace.
2. "Applies on Catalog Item view" is ticked.
3. The script does not use Portal-only or DOM APIs such as spModal, $scope, jQuery, document, window, top or g_form.getControl(). These are not supported in Workspace and usually fail silently, so check the browser console.
4. The value is passed as a JSON string (JSON.stringify), not as an array object.
5. The keys match the variable internal names exactly (case-sensitive), not the labels.
6. You are using the MRVS internal name without a "variables." prefix.

If the data comes from the server, call a client-callable Script Include with GlideAjax, return JSON.stringify(array), and call g_form.setValue('my_mrvs', answer) in the callback.

Please test in a sub-production instance first. Workspace support for catalog client scripts can vary between releases.

Hope this helps. If it does, please mark it as helpful or accept it as the solution.

View solution in original post

3 REPLIES 3

HasanM
Kilo Sage

Hi @sk4408bc ,

You can populate a multi-row variable set (MRVS) from a catalog client script on the record producer. Pass the MRVS internal name and a JSON string of the rows to g_form.setValue(). Each key in a row must be the internal name of a variable inside the MRVS.

function onLoad() {
var rows = [
{ "item_name": "Laptop", "quantity": "1" },
{ "item_name": "Monitor", "quantity": "2" }
];
g_form.setValue('my_mrvs', JSON.stringify(rows)); // MRVS internal name
}

Replace 'my_mrvs' with your MRVS internal name, and 'item_name' / 'quantity' with the internal names of the variables inside it.

If it works in Service Portal but not in Workspace, check the following:

1. UI Type on the catalog client script is set to "All". A "Desktop" script will not run in Workspace.
2. "Applies on Catalog Item view" is ticked.
3. The script does not use Portal-only or DOM APIs such as spModal, $scope, jQuery, document, window, top or g_form.getControl(). These are not supported in Workspace and usually fail silently, so check the browser console.
4. The value is passed as a JSON string (JSON.stringify), not as an array object.
5. The keys match the variable internal names exactly (case-sensitive), not the labels.
6. You are using the MRVS internal name without a "variables." prefix.

If the data comes from the server, call a client-callable Script Include with GlideAjax, return JSON.stringify(array), and call g_form.setValue('my_mrvs', answer) in the callback.

Please test in a sub-production instance first. Workspace support for catalog client scripts can vary between releases.

Hope this helps. If it does, please mark it as helpful or accept it as the solution.

sk4408bc
Tera Contributor

Thank you for your response.
I was actually for a way to use Event handlers, State Parameters of a Catalog item component to populate these MRVS, but yes, this approach works too. 
Out of curiosity, is there a way to do this via UI builder?

Hi @sk4408bc ,

Glad the client script approach works for you.

On UI Builder: as far as I know, there isn’t a supported way to populate an MRVS only through the Catalog Item component’s event handlers or state parameters. The component keeps the form (g_form) inside itself. Its properties mainly control which item loads, and its events report what happens on the form (such as submit). None of them let the page write variable values or MRVS rows into it.

The pattern that usually works is to split the job:

  1. Use UI Builder to get the data you need (client state parameter, data resource or page parameter).
  2. Use a catalog client script with UI Type set to “All” to write the rows, with g_form.setValue(‘your_mrvs’, JSON.stringify(rows)).
  3. If the data comes from the server, call a client-callable Script Include from that client script with GlideAjax. Don’t try to pass state across the component boundary.

I’d avoid customising the out-of-the-box catalog item page or component for this, because it adds upgrade risk.

Component properties do change between releases, so it’s worth checking on your version. Select the Catalog Item component in UI Builder and look through the Config and Events panels, or check its entry in the Next Experience component reference on the developer site. If anyone has found a supported hook on a newer release, I’d be interested to hear about it too.

Hope this helps.