Preventing Duplicates on MRVS based on two values

Code_Tiefling
Tera Contributor

Hello Everyone, 

I have a request to check, alert and prevent duplicates when working with a Multirow Variable Set.
The problem is that I am requested to do it at the moment of the Multirow Variable Set OnSubmit [So basically working with a Client Script that Applies to the variable Set]

I was able to do it on a Client Script that applies to the Catalog item with the following script:

 

function onSubmit() {
    // prep mrvs
    var mrvsField = g_form.getValue('variable_set_name');
    
    var mrvsArray = JSON.parse(mrvsField);

    // Create an array to store the combined field values
    var combinedValues = [];
    var duplicateIndices = [];

    // Iterate through each object in the MRVS array
    for (var i = 0; i < mrvsArray.length; i++) {
        
        var combinedFieldValue = mrvsArray[i].fieldValue1+ '-' + mrvsArray[i].fieldValue2; 

       
        if (combinedValues.indexOf(combinedFieldValue) > -1) {
            duplicateIndices.push(i); // Store the index of the duplicate value
        } else {
            combinedValues.push(combinedFieldValue); // Add the combined key to the array if it's not a duplicate
        }
    }

    // Remove duplicate values from the MRVS array
    for (var j = duplicateIndices.length - 1; j >= 0; j--) {
        mrvsArray.splice(duplicateIndices[j], 1);
    }

    // Convert the updated MRVS array back to JSON and set it to the field
    g_form.setValue('variable_set_name', JSON.stringify(mrvsArray));

    if (duplicateIndices.length > 0) {
        g_form.addErrorMessage('Duplicates Found and cleared, please check the list again');
        return false; // Prevent form submission if duplicates were found
    }else {return true;}

   
}

 

 

But Again the Script only works when it applies to the Catalog Item's Submit button, I am requested to do it on the Variable Set Submit, so it runs anytime they are adding a new value to the list and check for duplicates. 

Code_Tiefling_0-1720758642069.png

As shown in the image, I need it to run OnSubmit for the Multirow variable set, other OnSubmit scripts run fine each time I click on add.  



Thank you for your time and any feedback is appreciated


3 REPLIES 3

Vrushali  Kolte
Mega Sage

Hello @Code_Tiefling ,

 

In order to make a unique entry in MRVS column you can check the unique checkbox available on MRVS variable form.

This check will prevent any duplication.

 

VrushaliKolte_0-1720765412538.png

 

VrushaliKolte_1-1720765420057.png

 

If my answer solves your issue, please mark it as Accepted ✔️and Helpful 👍based on the impact.

 

 

Thank you for your answer. I tried what you suggested and it didnt quite work. It works perfectly for individual fields. But I need to check if a match of Field1 + Field2 exist in another row. Here is a screenshot of what I am required, as shown below, we should be able to submit the same user ID as many times as we want, AS LONG as de Role (column: rol_solicitado) is not the same for the same user twice. 

Code_Tiefling_2-1720803257690.png

 

 

So in this Scenario, the first and second row are acceptable, but the third one should have been prevented because it has the same User ID and Role. I was unable to achieve it with the "Unique" property because it would not allow me to add the same rol to different users, or different users with the same role

Thank you  for your time and help, I hope we can figure this out. Thanks again!

Brad Bowman
Kilo Patron
Kilo Patron

For an onSubmit Catalog Client Script that applies to the MRVS, you would just need to slightly change the line that retrieves the existing MRVS value, then iterate through the MRVS to ensure that both variables are not the same as the ones attempting to be added:

function onSubmit() {
    // prep mrvs
    var mrvsField = g_service_catalog.parent.getValue('variable_set_name');
    if (mrvsField.length > 2) { //native UI returns [] for empty MRVS value which causes a parsing error    
        var mrvsArray = JSON.parse(mrvsField);
         // Iterate through each object in the MRVS array
        for (var i=0; i<mrvsArray.length; i++) {
     	    if (mrvsArray[i].user_id == g_form.getValue('user_id') { //replace with your variable name
                if (mrvsArray[i].rol_solicitado == g_form.getValue('rol_solicitado') {
	                alert('User ID and Role already exists in this table.');
                    return false;
                }
            }
        }
    }
}