Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Variable Set with Client Script (onLoad)

EliaquimS
Tera Contributor

Good morning! How do I get a value from within the multirow variable and send it to a variable that is not in the multirow?

EliaquimS_0-1734696801569.png

 

11 REPLIES 11

Brad Bowman
Kilo Patron
Kilo Patron

Do you want to do this after the MRVS is populated, like onLoad of the RITM/Catalog Task, or as each row of the MRVS is populated/edited, or wait until onSubmit of the record to ensure it is accurate with any row edits or deletions?

As each row of the MRVS is populated/edited. 

Since you want to do this with each MRVS row addition/edit, you can use an onSubmit Catalog Client Script like this that applies to the MRVS, not the Catalog Item:

function onSubmit() {
	if (this) { //Service Portal method
		this.cat_g_form.setValue('v_manager', 'Bob');
	} else { //native UI method
		parent.g_form.setValue('v_manager', 'Bob');
	}

where 'v_manager' is the name of the Catalog Item variable.  Note that this will replace the value with each row.  If you want to append the value, use something like:

		parent.g_form.setValue('v_manager', parent.g_form.getValue('v_manager') + ' Bob');

For the Service Portal method to work, you also need this onLoad Catalog Client Script that applies to the Catalog Item:

function onLoad() {
	if (this) {//we only need to do this for Service Portal
		//We need to make the g_form object for the parent item available from the MRVS window
		this.cat_g_form = g_form;
	}
}

 

 

Community Alums
Not applicable

Try this 

function onLoad() {
    // Get the multi-row variable (replace 'multirow_variable_name' with the actual name of the multi-row variable)
    var multiRowVariable = g_form.getValue('multirow_variable_name'); 

    // Assuming the multi-row variable is a list or an array, you can access specific values:
    // Example: Retrieve the first row's value in a specific field (e.g., field 'item_name')
    var firstRowItemName = multiRowVariable[0].item_name; // Replace 'item_name' with the actual field name in the multi-row

    // Now, assign this value to another variable that is not in the multi-row (replace 'single_variable_name' with the actual variable name)
    g_form.setValue('single_variable_name', firstRowItemName);
}