Validation on Multi Row variable set (same role cannot be allowed).

Shidhi
Tera Contributor

Hi @Ankur Bawiskar 

 

Under a multi-row variable set (environment_selection) two select box fields i.e., select_environment , select_role is present.

 

A requester could request different roles for the same environment this can't be allowed.

For ex. I could select both the user and the viewer roles for ARCS UAT. Please check below.

 

Shidhi_0-1716404334567.png

 

However, only one role per environment is allowed to be selected.

If multiple roles are selected for the same environment, an alert must come saying that the selected environment is already added and clear the chosen environment field value.

 

I'm not sure how to check two variables. Please help me with this. 

 

Please help me with this.

 

Thank you!

 

 

 

 

 

1 ACCEPTED SOLUTION

@Shidhi 

what type of client script it is?

onSubmit on catalog item right?

try using alert instead of g_form

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@Shidhi 

you need to use onSubmit catalog client script, get the mrvs json, parse and check

what script did you start with?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Shidhi 

normal javascript you can apply to identify duplicate key value pairs within array of json objects

check this link

https://www.servicenow.com/community/developer-forum/json-array-remove-duplicates-based-on-key-and-v... 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar,

 

Below is the code that is not working. I'm not sure how to check if there are multiple roles with the same environment (no idea how to check those string comparisons). Need your help in this. Or you can give me another best way of doing this.

 

function onSubmit() {
   
var env = g_form.getValue('environment_selection');// variable set name
var count = 0;
 
 
var parser = JSON.parse(env);
 
 
for(var i=0;i<parser.length;i++){
 
var check = parseInt(parser[i].select_environment); //variable name 
var check1 = parseInt(parser[i].select_role); //variable name
count ++;
}
//Here if we can put a logic to check multiple roles are selected for the same environment and if yes we will increment the counter.
 
if(count>0){
 
g_form.addErrorMessage('Multiple roles for same environment is not allowed');
g_form.clearValue('select_environment');
 
return false;
 
}
 
}
   

Can you please help me with the logic part.

 

Thank you!

@Shidhi 

try this

basically environment value should be unique as per your case

function onSubmit() {

	var arrayWithDuplicates = JSON.parse(g_form.getValue('environment_selection'));// variable set name

	var uniqueArray = removeDuplicates(arrayWithDuplicates, "select_environment");    // here instead of "type" give your key name which you want to be unique

	var oldArrayLength = arrayWithDuplicates.length;
	var newArrayLength = uniqueArray.length;

	if(newArrayLength < oldArrayLength){
		g_form.addErrorMessage('Multiple roles for same environment is not allowed');
		return false;
	}

}

function removeDuplicates(originalArray, prop) {
	var newArray = [];
	var lookupObject  = {};

	for(var i in originalArray) {
		lookupObject[originalArray[i][prop]] = originalArray[i];
	}

	for(i in lookupObject) {
		newArray.push(lookupObject[i]);
	}
	return newArray;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader