I ahve created a catalog in side catalog i have one variable to add application if i already add

AaravOjha
Tera Contributor

I have created a catalog in side catalog i have one variable to add application if i already add the application with a name it should not allow to application with same name

13 REPLIES 13

@AaravOjha 

create onSubmit catalog client script which applies to your MRVS variable set

script like this, taken from this link and 1st line is updated

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

function onSubmit() {
    var multiRow = JSON.parse(g_service_catalog.parent.g_form.getValue("your_variable_set_here")); //This is the multirow that has already been submitted

    var obj = {
        'test1': g_form.getValue('test1'), //replace test1 with your variable1
        'test2': g_form.getValue('test2'), //replace test1 with your variable2
        'test3': g_form.getValue('test3'), //replace test1 with your variable3
        'test4': g_form.getValue('test4'), //replace test1 with your variable4
    };
	
if(multiRow) { //No need to run this if we don't have any values yet. 
var result = multiRow.filter(function(item) {
//Replace below (test1 etc) with your appropriate variable names!
  return (item.test1 == obj.test1 && item.test2 == obj.test2 && item.test3 == obj.test3 && item.test4 == obj.test4);
});
	if(result.length > 0) {
		g_form.addErrorMessage('You have entered the same twice');
		return false;
	}
}
}

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

I have only One variable which name is application name i need to prevent while adding the name it should not get added twice

 

 

Hi @AaravOjha 

 

You can either mark Unique checkbox as true for Application Name variable. OR

 

You can have on submit client script to validate if MRVS has any duplicate value, here is sample script for you:

 

var mrvs = g_form.getValue('your_mrvs_variable_name');
    var parsed = JSON.parse(mrvs);
    var values = [];
    
    for (var i = 0; i < parsed.length; i++) {
        var val = parsed[i].application;
        if (values.indexOf(val) !== -1) {
            alert('Duplicate values are not allowed in the MRVS field.');
            return false;
        }
        values.push(val);
    }
    return true;

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

This is even not allow to submit getting this error

 

Error MessageonSubmit script error: SyntaxError: Unexpected token 'm', "mm" is not valid JSON:
function () { [native code] }

@AaravOjha 

It worked for me with Unique checkbox on Select Box variable

It worked for me for Single Iine text variable as well

mrvs unique variable native and portal.gif

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