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

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;
			}
		}
	}
}

 

Thank you again Brad you are awesome!!! What would the syntax be if I want to add more than one short description? This is not the proper way to do it is it? I need to add basically or and 2 or 3 short descriptions but other than that it accomplishes exactly what I need thank you so much!!

 

Jon

if(g_form.getValue('short_description') == 'Laptop/Desktop Refresh - Configuration' , 'Iphone/Ipad Refresh - Configuration' ){
			//MRVS must have a serial number for each row

 

if (g_form.getValue('short_description') == 'Laptop/Desktop Refresh - Configuration' || g_form.getValue('short_description') == 'Iphone/Ipad Refresh - Configuration' ) {

You can add as many or || conditions as you need following the same format/syntax.  Let me know if you would like some tried and true resources on JavaScript training.

Yea let me know what you recommend, I have the Udemy course and have been going through the scripting in service now fundamentals. 

 

Thank you Brad,

Jon