Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

Create Unique records from multi row variable set

Amrutha K V
Tera Contributor

Hi 

We have a Multi-Row Variable Set (MRVS) named "MRP PRODUCTS PLANNING". Inside this variable set, there is a select box variable called "Plant & Storage Location" . 

Users should be allowed to create only one record for each value in the dropdown. If there are only ten  available drop down values, a maximum of ten records should be created in the MRVS. Duplicate selections should not be allowed, and users should not be able to create multiple records with the same Plant & Storage Location value.
How can we implement this functionality?

Thank you

 
6 REPLIES 6

Tanushree Maiti
Tera Patron

Hi @Amrutha K V 

 

Try this Validation using  onSubmit Catalog client script.

 

  • Navigate to Catalog Client Scripts and click New.
  • Name: Give your script a descriptive name (e.g., Prevent Duplicate MRVS Rows).
  • UI Type: Select All or Desktop/Service Portal.
  • Type: Set to onSubmit.
  • Applies to: Select A Catalog Item.
  • Catalog Item: Select your specific catalog item

 

function onSubmit() {

     g_form.clearMessages();

    var mrvsStr = g_form.getValue('mrvs_products_planning'); // Replace with your MRVS internal name

    if (!mrvsStr || mrvsStr == '[]') {

        return true;

    }

    var mrvsData = JSON.parse(mrvsStr);

    if (mrvsData.length > 10) {

        g_form.addErrorMessage("You cannot create more than 10 records in the MRP PRODUCTS PLANNING table.");

        return false;

    }

    var selectedStoragePlants = [];

    var isDuplicate = false;

    for (var i = 0; i < mrvsData.length; i++) {

        var plantValue = mrvsData[i].plant_and_storage_location; // Replace with your variable's internal name

        if (selectedStoragePlants.indexOf(plantValue) > -1) {

            isDuplicate = true;

            break;

        } else {

            selectedStoragePlants.push(plantValue);

        }

    }

    if (isDuplicate) {

        g_form.addErrorMessage("Duplicate Plant & Storage Location values are not allowed. Each plant must be selected only once.");

        return false;

    }

    return true;

}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Vikram Reddy
Tera Guru

Hi @Amrutha K V,

 

Use the built in Unique attribute on the variable itself, that alone blocks duplicate "Plant & Storage Location" rows without any client script.

  1. Open your "MRP PRODUCTS PLANNING" multi-row variable set from the variable set list.
  2. Go into the "Plant & Storage Location" select box variable inside that set.
  3. On the variable form, find the Unique checkbox and enable it.
  4. Save the variable. From that point on, if a user tries to add a row where that value already exists in another row of the same MRVS, the platform throws a validation error and blocks the row from being added.

That handles "no duplicate records." For the "max ten records" part, that is a separate setting on the variable set, not tied to Unique:

  1. Open the "MRP PRODUCTS PLANNING" variable set record.
  2. If you don't see an attributes field on the form, right-click the form header, choose Configure > Form Layout, and add "Variable Set attributes" (set_attributes) to the form.
  3. In that field, enter max_rows=10 (swap 10 for whatever your actual dropdown count is).
  4. Save. The Add row button will now stop working once ten rows exist.

Worth knowing: Unique stops the duplicate from saving, it does not remove that value from the dropdown, so users still see already used values as options and get bounced with an error instead of never seeing them at all. Pruning the list itself would need an onChange or onCellEdit catalog client script reading the current MRVS rows, that's extra polish, not required for your ask.

 

Thank you,
Vikram Karety
Octigo Solutions INC