Multi Row Variable set behavior

kirankr2061
Tera Contributor

The below is my onSubmit client script I have written inside the MultiRow Variable set. My problem is after each selection and add I do not want duplicate records that is the user must select unique combination each time here my multirow variable set is has 3 variables all 3 are lis collectors but I tried to write this script but its not working any suggestion how to achieve this or any code changes required ?

function onSubmit() {
    // Get current row values (multi-select list collectors)
    var currentCloud = normalize(g_form.getValue('ten_mrvscloud_account_name'));
    var currentRoles = normalize(g_form.getValue('ten_mrvs_roles'));
    var currentUsers = normalize(g_form.getValue('ten_mrvs_user'));

    // Get previously submitted rows (excluding the current one being added)
    var rows = g_service_catalog.parent.getValue('tenable_add_role');
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];

        var existingCloud = normalize(row.ten_mrvscloud_account_name);
        var existingRoles = normalize(row.ten_mrvs_roles);
        var existingUsers = normalize(row.ten_mrvs_user);

        if (
            currentCloud === existingCloud &&
            currentRoles === existingRoles &&
            currentUsers === existingUsers
        ) {
            g_form.showFieldMsg('cloud_account_name', 'This combination of Cloud Account, Roles, and Users already exists. Please enter a unique combination.', 'error');
            return false;
        }
    }

    return true;
}

function normalize(sysIdString) {
    if (!sysIdString) return '';
    var parts = sysIdString.split(',');
    //parts.sort();
    return parts.join(',');
}
1 ACCEPTED SOLUTION

Chaitanya ILCR
Kilo Patron

Hi @kirankr2061 

try this

function onSubmit() {
    // Get current row values (multi-select list collectors)
    var currentCloud = normalize(g_form.getValue('ten_mrvscloud_account_name'));
    var currentRoles = normalize(g_form.getValue('ten_mrvs_roles'));
    var currentUsers = normalize(g_form.getValue('ten_mrvs_user'));

    // Get previously submitted rows (excluding the current one being added)
    var rows = JSON.parse(g_service_catalog.parent.getValue('tenable_add_role'));
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];

        var existingCloud = normalize(row.ten_mrvscloud_account_name);
        var existingRoles = normalize(row.ten_mrvs_roles);
        var existingUsers = normalize(row.ten_mrvs_user);

        if (
            currentCloud === existingCloud &&
            currentRoles === existingRoles &&
            currentUsers === existingUsers
        ) {
            g_form.addErrorMessage('cloud_account_name', 'This combination of Cloud Account, Roles, and Users already exists. Please enter a unique combination.', 'error');
            return false;
        }
    }

    return true;
}

function normalize(sysIdString) {
    if (!sysIdString) return '';
    var parts = sysIdString.split(',');
    parts.sort();
    return parts.join(',');
}

,

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

If you add some alerts you'll see the 'existing' script variables do not contain the data you expect.  You'll also want to uncomment the sort method so that your normalize function does something.

Chaitanya ILCR
Kilo Patron

Hi @kirankr2061 

try this

function onSubmit() {
    // Get current row values (multi-select list collectors)
    var currentCloud = normalize(g_form.getValue('ten_mrvscloud_account_name'));
    var currentRoles = normalize(g_form.getValue('ten_mrvs_roles'));
    var currentUsers = normalize(g_form.getValue('ten_mrvs_user'));

    // Get previously submitted rows (excluding the current one being added)
    var rows = JSON.parse(g_service_catalog.parent.getValue('tenable_add_role'));
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];

        var existingCloud = normalize(row.ten_mrvscloud_account_name);
        var existingRoles = normalize(row.ten_mrvs_roles);
        var existingUsers = normalize(row.ten_mrvs_user);

        if (
            currentCloud === existingCloud &&
            currentRoles === existingRoles &&
            currentUsers === existingUsers
        ) {
            g_form.addErrorMessage('cloud_account_name', 'This combination of Cloud Account, Roles, and Users already exists. Please enter a unique combination.', 'error');
            return false;
        }
    }

    return true;
}

function normalize(sysIdString) {
    if (!sysIdString) return '';
    var parts = sysIdString.split(',');
    parts.sort();
    return parts.join(',');
}

,

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya