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

How to restrict or disable the add button in Multi Row Variable Set after the 5th Row?

RISHAV SANSON
Tera Contributor

I have to restrict the number of rows to 5 but i am not be able to restrict or disable the add button. Could you please share some thoughts or suggestions ? 

33 REPLIES 33

Oleg
Mega Sage

I find the question very interesting. Thus I analysed the current code of SeviceNow of Madrid version in both classical GUI and Service Portal. So I can suggest clean solution, which use existing functionality built-in but not documented in ServiceNow.

To solve the problem classical GUI (UI type=Desktop) one can create new onLoad Catalog Client Script on Catalog Item, which use some Multi Row Variable Set. One need to know sys_id of the Variable Set, which has Multi Row type. The corresponding code could be the following

function onLoad() {
	function setMaxRowSize (sysIdMRWS, limit) {
		var tableVariable = typeof tableVariableCache !== "undefined" &&
			tableVariableCache["table_variable_buttons_" + sysIdMRWS] != null ?
				tableVariableCache["table_variable_buttons_" + sysIdMRWS] :
				typeof table_variable !== "undefined" ? table_variable: null;
		if (tableVariable != null) {
			if (typeof tableVariable.setMaxRowSize === "function") {
				tableVariable.setMaxRowSize(String(limit));
			} else {
				tableVariable.maxRowSize = limit;
			}
		}
	}
	setMaxRowSize("feb9ad834fe9f300b8580ab18110c719", 2);
}

find_real_file.png

The string "feb9ad834fe9f300b8580ab18110c719" in above code is sys_id of Multi Row Variable Set (sys_id of the Variable Set, which has Multi Row type). I used 2 as the max row number. By the way, default value of max rows is 50. After inserting of 2 rows the Add button will be disabled by built-in ServiceNow code and the look of Multi Row Variable Set will be like on the picture below:

find_real_file.png

If one need support of the same functionality in Service Portal then one can add one more onLoad Catalog Client Script on the same Catalog Item, but now the Catalog Client Script should have UI type "Mobile / Service Portal". The corresponding code will use the name of the Variable Set:

function onLoad() {
	var field = g_form.getField("uk_billing_invoice_details_new");
	if (field != null) {
		field.max_rows_size = 2;
	}
}

find_real_file.png

The string "uk_billing_invoice_details_new" in above code is the name of Multi Row Variable Set (the name of the Variable Set, which has Multi Row type).

After inserting the max rows the Add button will be disabled by built-in code of ServiceNow and will look like on the picture below

find_real_file.png

So in theory you should be able to do with in one script. With UI type of all for both Desktop version and SP.

function onLoad() {
	//Type appropriate comment here, and begin script below
	try { // try in service portal.
		var field = g_form.getField("uk_billing_invoice_details_new");
		if (field != null) {
			field.max_rows_size = 2;
		}
	}
	catch(e){ //desktop
		function setMaxRowSize (sysIdMRWS, limit) {
			var tableVariable = typeof tableVariableCache !== "undefined" &&
			tableVariableCache["table_variable_buttons_" + sysIdMRWS] != null ?
			tableVariableCache["table_variable_buttons_" + sysIdMRWS] :
			typeof table_variable !== "undefined" ? table_variable: null;
			if (tableVariable != null) {
				if (typeof tableVariable.setMaxRowSize === "function") {
					tableVariable.setMaxRowSize(String(limit));
				} else {
					tableVariable.maxRowSize = limit;
				}
			}
		}
		setMaxRowSize("feb9ad834fe9f300b8580ab18110c719", 2);
	}
}

This works.

Maaz1
Kilo Explorer

Hi Oleg,

 

I am using it in the onChange Client Script, as per my required scenario. Is it possible to reverse this action using onChange Client Script?

Thanks,

Mark

priyadharisini
Kilo Contributor

I would need a similar functionality where the multi row variable set be editable for a particular group when the catalog task is assigned to that group .. For the other members it should be disabled...

 

Possible?