The secret of Multirow Variable Set – ServiceNow MRVS on Change

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…

Leave a Reply

Your email address will not be published. Required fields are marked *