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

The two code examples I provided run on the server - in a workflow Approval - user activity or a Run Script activity as noted.  Running an onChange Catalog Client Script that applies to the MRVS will give you incomplete results.  You could do this as an onSubmit Catalog Client Script that applies to the Catalog Item, starting with your Trial 1 code, then looping through it to extract the user sys_ids from each row, but since you're doing something that doesn't need to be seen on the request form, it's best to do this with a script that runs on the server, after the request is submitted.

Hi Brad,

 

I've tried two different onSubmit scripts but still no luck. See the scripts below and please let me know if I'm missing anything. PS: Our catalog items run on a single flow that's why I've not tried the two workflow-based scripts you graciously provided.

 

Script 1

function onSubmit() {
 var mrvsJSON = g_form.getValue('appuser_mrvs');
 var mrvsArray = JSON.parse(mrvsJSON);
 var fetchedAppUserVar = [];
  for (var i = 0; i < mrvsArray.length; i++) {
    var mrvsRow = mrvsArray[i];
var userRef = mrvsRow.appuser_var;
var entry = {
value: userRef, 
display_value: userRef.getDisplayValue()
   };
     fetchedAppUserVar.push(entry);
  }
     g_form.setValue('fetched_appuser_var', JSON.stringify(fetchedAppUserVar));
     return true;
}
 
Tried Script 2
function onSubmit() {
    var g_form = g_form.get();
    var mrvs = g_form.getControl('appuser_mrvs').getValue();
    for (var i = 0; i < mrvs.length; i++) {
        var appuser_ref = mrvs[i].appuser_var;
        g_form.addOption('fetched_appuser_var', appuser_ref, appuser_ref);
    }
    return true;
}

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(','));
}

 

Hello Brad, Thank you for your invaluable assistance with the script. Your expertise, guidance, and patience have been instrumental in solving the challenges I faced. Thanks to your support, I was able to overcome obstacles and improve the quality of my project.

Great to hear.  You are very welcome!