Set variable ReadOnly when Multi Row Variable Set not empty

SamuelChang
Tera Contributor

Hi all,

 

I need some help or guidance on how to set a variable Read-Only when the Multi Row Variable Sets (MRVS) not empty i.e. once it populated at least one item.


Basically, once I added Report Name & Email the field outside of MRVS should be set to Read-Only

SamuelChang_0-1722362720233.png

 

I've tried onLoad client script but it doesn't work quite right

function onLoad() {
	var mrvs = JSON.parse(g_form.getValue('report_name_and_email'));
	if(mrvs.length > 0) {
		g_form.setReadOnly('add_or_remove', true);
	}
}

Is there something else that I missed?

 

Thank you in advance!

17 REPLIES 17

Doesn't looks like there is a simple way to do this. Can you explain more why you have this requirement. I don't think you have to worry about to many changing it multiple times. 

 

Edit: Another thought is that you could make the variable outside the MSRV part of the MSRV. That was each line they can choose if they are adding or removing the contact from a report. This will much better user experience.

My requirement is they can submit multiple report's name with email associate with the report, and they can either add or remove the report. My original design could cause some confusion because they might think first I select 'Add' then put 3 reports in MRVS, then later select 'Remove' with 2 other reports. The assignment group will interpret as Remove them all, so I think if the MRVS isn't empty then set the selection field to be Read Only omit them to change.

 

If this isn't achievable then I might addErrorMessage whenever they change the variable that states you can only do 1 action per request

Saloni Suthar
Mega Sage
Mega Sage

Hi @SamuelChang,

You can pull the values of the MRVS in a client script, but you cannot listen for the onChange event from outside of the MRVS. I recently implemented a similar solution and I referred to https://www.servicenow.com/community/developer-forum/impossible-onchange-client-script-on-mrvs-multi... for the solution. Please go through the solution from the community article and let me know if you need a hand with script.


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

Hi Saloni,

 

Thank you for your suggestion, but I'm unable to implement this in my situation if you can walk me through or provide a script that would greatly appreciate!

Sure thing!

  1. Create new variable, type = multi line text
  2. Create a new variable and type is Custom.
  3. Click on Type Specifications, click on the widget search icon.
  4. Create new widget.
  5. In the client controller, paste below script and make sure to replace the variable name with your custom variable.
 

 

 

api.controller=function($scope) {
/* widget controller */
var c = this;
var mrvsName = 'accessory'; // replace with your MRVS internal name

$scope.$watch(function() {
return $scope.page.g_form.getValue(mrvsName);
}, function(value) {
g_form = $scope.page.g_form;
g_form.setValue('hidden_variable', value); //replace with your multi line text variable name
});
};​

 


6. Save the widget and ensure the custom variable is using the widget.
7. Create new Catalog Client Script, type = onChange, variable name = your multi line text variable.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue != ' '{
g_form.setReadOnly('read_only_variable', true) //replace with you variable name
}
else {
g_form.setReadOnly('read_only_variable', false) //replace with you variable name
}​

 

 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni