Preventing Duplicates on MRVS based on two values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2024 09:28 PM - edited 07-11-2024 09:31 PM
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2024 11:25 PM
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.
If my answer solves your issue, please mark it as Accepted ✔️and Helpful 👍based on the impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2024 09:52 AM - edited 07-12-2024 09:54 AM
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.
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2024 10:19 AM
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;
}
}
}
}
}