How to populate mrvs variable values into a catalog item variable.

SNLearnAll
Tera Contributor

I have a multi-row variable set with internal name "appuser_mrvs". The MRVS has a reference variable "appuser_var" that pulls record from the sys_user tables. I wanted to drive approvals based on the users identified in the "appuser_var" variable, however I couldn't achieve that. 

 

Now I would like to pass values from each of the rows entered on the "appuser_var" mrvs variable onto a catalog item list collector variable "fetched_appuser_var". 

 

I've tried two onChange client scripts as seen below but didn't get any result.

_

Trial 1 (Using mrvs internal name): var gr = JSON.parse(g_form.getValue('appuser_mrvs'));

Trial 2 (Using mrvs sys_id): g_form.addInfoMessage(g_form.getValue("IO:5a43330ddb0d0294d76cccfa13961941"));

_

Any guide on how to achieve expected result would be much appreciated.

1 ACCEPTED SOLUTION

You can do this with an onSubmit Catalog Client script that applies to the Catalog Item, not the MRVS.  The script just needs to loop through the MRVS contents to extract the user sys_ids from one MRVS variable, and push each to an array - since a list collector's value is a comma-separated list of sys_ids.

function onSubmit() {
	var fetchedAppUserVar = [];
	var mrvs = g_form.getValue('appuser_mrvs');
	if (mrvs.length > 2) { //native UI returns [] for empty MRVS value which causes a parsing error
		var obj = JSON.parse(mrvs);
		for (var i = 0; i < obj.length; i++) {
			fetchedAppUserVar.push(obj[i].appuser_var.toString());
		}
	}
	g_form.setValue('fetched_appuser_var', fetchedAppUserVar.join(','));
}

 

View solution in original post

9 REPLIES 9

Brad Bowman
Kilo Patron
Kilo Patron

Using the MRVS internal name is the right approach.  It's going to be easier to do this on a server-side script, like a workflow Run Script / Approval activity, or Business Rule.  Do you need to see the list collector variable populated on the form before the record is submitted?  You are running this script onChange of which variable? Start with getting the MRVS JSON string without parsing it to confirm the value:

var gr = g_form.getValue('appuser_mrvs');
alert(gr);

If you're getting the expected value, add back in the parse, then check the number of rows

var gr = JSON.parse(g_form.getValue('appuser_mrvs'));
var numerOfRows = gr.length;
alert(numberOfRows);

SNLearnAll
Tera Contributor

Hello Brad, thanks for your guide and script provided.

 

Do you need to see the list collector variable populated on the form before the record is submitted?  No. I intend to have the list collector variable hidden on the catalog item view and read-only on RITM & SCTask.

 

You are running this script onChange of which variable? I ran the onChange script on the the MRVS reference variable "appuser_var" inside the MRVS but that failed. 

 

 

 

If you run this script inside the MRVS you would need to use g_service_catalog.parent.getValue('appuser_mrvs'), to access the MRVS variable on the parent form, but that will retrieve the contents of the MRVS before the current row is added.  This is a lot simpler since you don't need/want to see it on the form.  If you are using a (legacy) workflow, the Approval - user script would look like this:

answer = [];
var mrvs = current.variables.appuser_var;
var rowCount = mrvs.getRowCount();
for (var i=0; i<rowCount; i++) {
	var row = mrvs.getRow(i);
	answer.push(row.appuser_var.toString());
}

Or the Run Script to populate the list collector variable with the users from the MRVS would look like this:

var lc = [];
var mrvs = current.variables.appuser_var;
var rowCount = mrvs.getRowCount();
for (var i=0; i<rowCount; i++) {
	var row = mrvs.getRow(i);
	lc.push(row.appuser_var.toString());
}
current.variables.fetched_appuser_var = lc.join(','); 

 

SNLearnAll
Tera Contributor

Hi Brad - thank you for your continued help on this. I've tried the run script but getting error. I've included a screenshot.

 

SNLearnAll_1-1711663744897.png

 

SNLearnAll_0-1711662984900.png