Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Multi row variable set dupicate restriction

pk2184046
Tera Expert

Hi there, 

I have a multi row variable set having two variables. here is don't want users to select the same option twice below is the code I was trying but it is not working and throwing javascript error in you console.

 

function onSubmit() {
    var mrvs =parent.g_form.getValue("additional_responsibilities_and_security_roles"); //name of the multi row variable set

       var arr = JSON.parse(mrvs);

   
    var res = g_form.getValue("responsibility_add");  // name of the variable1
    var sec = g_form.getValue("security_role_add");  //name of the variable2
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].responsibility_add == res && arr[i].security_role_add==sec ) {
            return false;
        }
    }
    return true;
}
 
 
 
Additionally tried in place of parent.getvalue just getvalues same result 
2 ACCEPTED SOLUTIONS

Robert H
Mega Sage

Hello @pk2184046 ,

 

Please use this onSubmit script on your Variable Set:

function onSubmit() {

    var mrvs = g_service_catalog.parent.getValue("additional_responsibilities_and_security_roles");
    if (!mrvs) return true;
    var arr = JSON.parse(mrvs);

    var res = g_form.getValue("responsibility_add");
    var sec = g_form.getValue("security_role_add");
    var duplicate = arr.some(row =>
        row.responsibility_add === res &&
        row.security_role_add === sec);

    if (duplicate) {
		g_form.addErrorMessage('Row with same values already present.');
		return false;
	}

}

 

Regards,

Robert

View solution in original post

Hello @pk2184046 ,

 

Glad to hear that.

The issue was that, in order to access the parent form, you need to use

g_service_catalog.parent

instead of

parent.g_form

 

There are many old scripts floating around with the latter code, but that doesn't work in Service Portal / Employee Center.

 

Regards,

Robert

View solution in original post

12 REPLIES 12

Hello @pk2184046 ,

 

I made a modification to the code in my original response. Use the updated code (line #2 was added "if (!mrvs) return true;").

 

Regards,

Robert

Excellent this worked. What is the mistake I'm doing exactly?

Hello @pk2184046 ,

 

Glad to hear that.

The issue was that, in order to access the parent form, you need to use

g_service_catalog.parent

instead of

parent.g_form

 

There are many old scripts floating around with the latter code, but that doesn't work in Service Portal / Employee Center.

 

Regards,

Robert

Ankur Bawiskar
Tera Patron
Tera Patron

@pk2184046 

you can use Unique=true checkbox on those variables present within MRVS set.

AnkurBawiskar_1-1745836354325.png

 

AnkurBawiskar_0-1745836183705.png

if you want to use script then check this link and it has solution

How to stop duplicates entering in MRVS based on MRVS Variables(Applies Catalog item/ServicePortal) 

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

Hi Ankur,

 

Unique option I cannot use here since user can select var1 multiple times as variable 2 has different values

Please see the attached screenshot

pk2184046_0-1745836971703.png