How to Make a single variable mandatory in MRVS with UI policy

jonsr20
Tera Expert

Hello everyone,

 

I have an issue where I need to make a variable mandatory on a specific task, that variable is inside an MRVS. I have tried a variety of things, but I cannot make the variable inside the MRVS mandatory and prevent it from closing the task I need it mandatory on if it is not populated. I was using the short description of the task in the UI policy to trigger it but am having trouble accessing the MRVS correctly with a script. Can anyone assist me on how I can make a single variable inside a MRVS mandatory on a specific task?

 

Thanks,

Jon

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Jon,

You need to do this with an onSubmit Catalog Client Script (that applies to the Catalog Item on Catalog Tasks, not the MRVS).  The script will retrieve the contents of the MRVS, then loop through each row.  If any row does not have a certain variable populated it will alert and prevent the form submission.  This example is only for a specific Catalog Task, and only runs when the task is attempted to be Closed Complete.  At the time I wrote this the MRVS value when retrieved from the form contained a sys_id for each variable instead of the variable name.  I don't think that's the case anymore, but I'll include that line in case you are facing something similar.  You can alert on the mrvs script variable to confirm the retrieved contents.

function onSubmit() {
	if (g_form.getValue('state') == 3) { //Closed Complete
		if(g_form.getValue('short_description') == 'Order Network Gear'){
			//MRVS must have a serial number for each row
			var sn = 'true';
			var mrvs = g_form.getValue('network_gear_mrvs');
			mrvs = mrvs.toString().replace(/2f04c2c71b1b1894d24ddc2ddc4bcb94/g,'v_mrvs_serial_number');
			var obj = JSON.parse(mrvs);
			for (var i = 0; i < obj.length; i++) {
				if(obj[i].v_mrvs_serial_number == ''){
					sn = 'false';
					break;
				}
			}
			if(sn == 'false'){
				alert('A Serial Number is required for each row in the Network Gear Assets table before closing this task.');
				return false;
			}
		}
	}
}

 

View solution in original post

7 REPLIES 7

All of the Now Learning courses are worthwhile:

Introduction to Scripting in ServiceNow

Scripting in ServiceNow Fundamentals

Learning JavaScript on the Now Platform

 

For a different format, consider an excellent video series by gurus Chuck Tomasi and Earl Duque

https://www.youtube.com/watch?v=62Nabpb94Jw&list=PL3rNcyAiDYK2_87aRvXEmAyD8M9DARVGK&pp=iAQB 

There are 81 videos, but they are short, digestible, and you can skip through what you already know.

 

Also, W3 Schools has an awesome tutorial, indexed by topic for quick reference. 

https://www.w3schools.com/js/default.asp 

Note that this is not ServiceNow-specific, so some methods / keywords / syntax may differ

 

Scripting best practices

https://developer.servicenow.com/dev.do#!/guides/washingtondc/now-platform/tpb-guide/scripting_techn... 

and a style guide 

https://google.github.io/styleguide/jsguide.html#formatting-braces-all 

are good things to keep in mind.

Thank you, I will check them out for sure, I really appreciate you assisting me. I am extremely Grateful for your time and you sharing your knowledge with me!

 

Thanks again,

Jon

Zach Koch
Giga Sage
Giga Sage

You would go into your Variable Set record, not the Catalog item, then make your UI policy there. To make it task specific you could do like you had mentioned and use the short description of the task (or some other identifying piece of data) to only have it mandatory there.

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!