The CreatorCon Call for Content is officially open! Get started here.

Cuong Phan
Kilo Sage

The secret of Multirow Variable Set – ServiceNow MRVS on Change

Reference: Article Link

ServiceNow MRVS Change

In previous part, we have gone through the basic configuration for Multirow Variable Set (MRVS). In this article, I will cover the secret to catch ServiceNow MRVS on Change.

First thing first, what is the requirement?

Situation: We want to show/hide some field whenever one row is inserted in MRVS and hide that field if no rows.

servicenow mrvs on change Show/Hide Fields when insert/update MRVS

HOW TO DO THAT?

Step 1: Create a custom/macro widget to store your logic script (which is a widget)

In your catalog item/ record producer, create a new variable with type of “Custom” (new name on Quebec or Macro in previous releases.

123 4 Create CUSTOM Variable with logic stored in widget

Step 2: Create a custom widget that stores logic on client side part

The reason we don’t use ‘CLIENT SCRIPT’ since ServiceNow Client Script does not recognize any change of MRVS. Remember to include “$scope” api to use the function $watch to recognize any change made from MRVS.

api.controller=function($scope) {
	/* widget controller */
	var c = this;
	var mrvsName = 'multirow_variable_set_title';
	
	$scope.$watch(function() {
		return $scope.page.g_form.getValue(mrvsName);
	}, function(value) {
		// Action when change
		g_form = $scope.page.g_form;
		
		if (value) {
			g_form.setDisplay('json_output',true); // Field name needs to show/hide
		} else {
			g_form.setDisplay('json_output',false);
		}
	});
};

What did “Value” object return?

The value of this kind of function returns the value of MRVS (g_form.getValue(mrvsName)). They are stored as a JSON object. So in order to handle it easily, we can use:

JSON.parse(value)

This way will convert to array object that we can manage to control easier

image 25

To Sum Up,

We have gone through the way to catch change event from a MRVS, but there is still more tricks we can play with

TO BE CONTINUE…

 
Comments
Kieran Anson
Kilo Patron

As an alternative, you can do this in client scripts as follows (no custom widget necessary).

 

Save g_form as a window variable

On the catalog item, add an onLoad client script (that has isolated set to false) with the following:

function onLoad() {
this.my_g_form = g_form;
}

Use an onSubmit script within the MRVS

When a new row is added to the MRVS, any onSubmit client scripts are executed.

function onSubmit() {
   
	if(this.my_g_form){

		this.my_g_form.setDisplay('mrvs_label', true);
	}
}

 

Backend & Service Portal Submit

The above is intended for service portal, the functionality within the backend is actually easier and only requires the MRVS script as the g_form object is available.

function onSubmit() {

	//Run Desktop first
	if(parent.g_form){
		parent.g_form.setDisplay('mrvs_label', true);
	}

	//Run Service Portal (if g_form has been declared by cat_item client script)
	if(this.my_g_form){
		this.my_g_form.setDisplay('mrvs_label', true);
	}
}

 

find_real_file.png

Diogo Figueira
Tera Contributor

Hey Kieran.

Your solution is cool but doesn't cover a very important thing. It will not trigger if you delete a row/Delete all rows.

If you are doing something like the sum of a field that is within the MRVS this will not work if a row is removed. For that, Cuong's solution is better

Sandesh9
Tera Guru

Hello Cuong,

 

I want to get the row object value which is removed from the mrvs. Any way to do that? for instance i only want to populate the multi line variable with the rows that are removed from mrvs?

 

Any help is appreciated.

 

Thanks,

Sandesh

Ruchi Kumari1
Tera Expert

@Sandesh9 Did you get a solution to this ?

Colleen
Tera Expert

Another widget-free alternative.

 

The MRVS triggers the following Angular events (should be self-explanatory):

  • $sp.sc_multi_row_active_row.add
  • $sp.sc_multi_row_active_row.update
  • $sp.sc_multi_row_active_row.cancel

You can set up an event handler in a regular onLoad catalog client script.  For example:

function onLoad() {
	var $rootScope = (0, eval)('this').angular.element('main').injector().get('$rootScope');   

	$rootScope.$on('$sp.sc_multi_row_active_row.update', function(evt, data) {
		alert('MRVS change');
	});
}

 

Unfortunately, I've not been able to identify an event that is triggered when a row is deleted. 

 

 

Colleen
Tera Expert

Update to widget-free alternative, which works when MRVS record is added, updated or deleted.

 

Implement $rootScope.$watch in the catalog onLoad client script.

 

function onLoad() {
	var $rootScope = (0, eval)('this').angular.element('main').injector().get('$rootScope');   

	$rootScope.$watch(
		function() {			
			try {
				var length = (
					g_form.getValue('mrvs')!='' ? 
					JSON.parse(g_form.getValue('mrvs')).length : 
					0
				);
				return length;
			} catch (ex) {
				return 0;
			}

		}, 
		function() { 
// do something with MRVS data
               }
	);
}

 

Version history
Last update:
‎06-04-2021 08:21 PM
Updated by: