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

JackieZhang
Kilo Sage

I have req to stop the duplicates entering in MRVS.

MRVS Internal name is temp_workers_for_create_accounts

JackieZhang_0-1769066256558.png

I write onsubmit client script under the variable set. but not working and  g_form.getValue('temp_workers_for_create_accounts') is always empty. 

function onSubmit() {
    debugger;
    var mrvsJson = g_form.getValue('temp_workers_for_create_accounts');
    var name =  g_form.getValue('name');
    var mrvsRows = [];
    if (mrvsJson && mrvsJson.trim() !== '' && mrvsJson !== '[]') {
        mrvsRows = JSON.parse(mrvsJson);
    }
    if (mrvsRows.length === 0) return true;
    for (var i = 0; i < mrvsRows.length; i++) {
        var currentSysId = (mrvsRows[i]['name'] || '').trim();
        if (currentSysId== name) {
            g_form.addErrorMessage('Duplicate Temp Worker detected. This Temp Worker has already been added in the MRVS.');
            return false;
        }
    }
    return true;
}
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron

Since this Catalog Client Script applies to the variable set, not the Catalog Item, you need to use 

var mrvsJson = g_service_catalog.parent.getValue('temp_workers_for_create_accounts');

to get the existing values in your unfortunately-long-internal-name MRVS.  It looks like you are using the MRVS 'name' variable to determine if a row for this worker already exists in the MRVS.  I would do this onChange of this variable for a better user experience and immediate feedback/prevention of the new row before other data is needlessly entered.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    mrvsJson = g_service_catalog.parent.getValue('temp_workers_for_create_accounts');
    if (mrvsJson.length > 2) { //native UI empty MRVS = '[]'
        var obj = JSON.parse(mrvsJson);
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].name == newValue) {
                g_form.clearValue('name');
                alert('Duplicate Temp Worker detected. This Temp Worker has already been added in the MRVS.);
            }
        }
    }
}

 

View solution in original post

2 REPLIES 2

Adrian Ubeda
Mega Sage

Hello @JackieZhang , 

Try this:

function onSubmit() {

    var result = [];
    var items = [];

    var getData = g_form.getValue('enter your mrvs name here'); //MultiRow Variable Set Name

    getData = getData.toString().replace(/sys_id_of_the_variable/g, 'varibale_name'); //'variable_name' - Variable in MRVS.

    var mrvData = JSON.parse(getData);

    for (var i = 0; i < mrvData.length; i++) {
        items.push(JSON.stringify(mrvData[i].varibale_name)); // varibale_name -->Variable in MRVS.
    }

    findDuplicateEntries(items);

    if (result.length > 0) {
        g_form.addErrorMessage("Please do not add the same item multiple times.");
        return false;
    }


    function findDuplicateEntries(items) {
        var sortElements = items.slice().sort();
        for (var i = 0; i < sortElements.length - 1; i++) {
            if (sortElements[i + 1] === sortElements[i]) {
                result.push(sortElements[i]);
            }
        }
    }

}

 I've found something similar in this link: 

https://www.servicenow.com/community/itsm-articles/multi-row-variable-set-mrvs-catalog-client-script...

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

Brad Bowman
Kilo Patron

Since this Catalog Client Script applies to the variable set, not the Catalog Item, you need to use 

var mrvsJson = g_service_catalog.parent.getValue('temp_workers_for_create_accounts');

to get the existing values in your unfortunately-long-internal-name MRVS.  It looks like you are using the MRVS 'name' variable to determine if a row for this worker already exists in the MRVS.  I would do this onChange of this variable for a better user experience and immediate feedback/prevention of the new row before other data is needlessly entered.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    mrvsJson = g_service_catalog.parent.getValue('temp_workers_for_create_accounts');
    if (mrvsJson.length > 2) { //native UI empty MRVS = '[]'
        var obj = JSON.parse(mrvsJson);
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].name == newValue) {
                g_form.clearValue('name');
                alert('Duplicate Temp Worker detected. This Temp Worker has already been added in the MRVS.);
            }
        }
    }
}