Hiding a multi row variable set variable reference option

Amina El hadad
Tera Contributor

Hello everyone,

I have a catalog item that includes a Multi-Row Variable Set (MRVS) named "new_tables", which contains a variable called "table". This variable is a reference to the sys_db_object table.

I want to ensure that users cannot select the same table more than once.

For example, if a user selects ".NET Application" in the first row, it should no longer be available for selection when adding a new row.

Any thoughts?

Thank you.

3 ACCEPTED SOLUTIONS

Hi @Amina El hadad ,

try this

try to alert newTables and see what you are getting

function onSubmit() {

    var newTables = g_service_catalog.parent.getValue('new_tables');
    newTables = newTables ? newTables : '[]';
    var systemTabs = JSON.parse(newTables);
    for (var i = 0; i < systemTabs.length; i++) {
        if (systemTabs[i]['table'] == g_form.getValue('table')) {
            g_form.addErrorMessage('table is already added');
            return false;
        }
    }
}

 

if you still getting the error 

 

Could you please share the screenshots of the error and the configuration that you have done and the MVRS and it's scripts?

 

 

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

Regards,
Chaitanya

View solution in original post

Medi C
Giga Sage

Hi @Amina El hadad,

 

I hope you are doing well! I was able to solve your issue on my PDI. Please follow below instructions to get a working solution:

 

  1. Go to your Multi-Row Variable Set "new_tables"
  2. Create OnSubmit Catalog Client Script with the following script: (Please ensure variable names are correct)
function onSubmit() {
    var multiRow = JSON.parse(parent.g_form.getValue("new_tables")); 

    var obj = {
        'table': g_form.getValue('table')
    };

    if (multiRow) {
        var result = multiRow.filter(function(item) {
            return (item.table == obj.table);
        });
        if (result.length > 0) {
            g_form.addErrorMessage('You have entered the same twice');
            return false;
        }
    }
}

 

Results:

As you can see below screenshot, I was unable to add ".NET Application" twice. the Error message would be displayed when you click on Add.

 

MediC_0-1741972637496.png

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

Hi @Amina El hadad,

 

Yes! that is definitely possible. Please follow below instructions to achieve your requirements:

 

1- Disable/Deactivate Existing Catalog Client Script(s) you created earlier in the previous solution(s)

 

2- Create OnLoad Catalog Client Script on your Variable Set

 

function onLoad() {
    var catItemVarName = 'community_test'; //PLEASE REPLACE THIS WITH YOUR VARIABLE SET TECHNICAL NAME
    var catItemVarValue = g_service_catalog.parent.getValue(catItemVarName);
    var ga = new GlideAjax('RefQualifierClient'); // PLEASE USE THE SCRIPT INCLUDE WHICH IS CREATED IN NEXT STEP
    ga.addParam('sysparm_name', 'setSessionData'); //function in script include
    ga.addParam('sysparm_cat_item_var_name', catItemVarName);
    ga.addParam('sysparm_cat_item_var_value', catItemVarValue);
    ga.getXMLAnswer(getResponse);
}

function getResponse(response) {
    //do nothing 
}

 

 

3- Create a Script Include (Client Callable / Glide AJAX enabled) - In my example, I named it "RefQualifierClient"

 

4- Add the following function to your script include:

 

    setSessionData: function() {
        var sysId = [];
        var catItemVarName = this.getParameter('sysparm_cat_item_var_name');
        var catItemVarValue = this.getParameter('sysparm_cat_item_var_value');
        var variableSetData = JSON.parse(catItemVarValue);
        for (var i = 0; i < variableSetData.length; i++) {
            sysId.push(variableSetData[i].table); //REPLACE "table" WITH TECHNICAL NAME OF THE REFERENCE FIELD TO sys_db_object ON YOUR VARIABLE SET
        }
        var session = gs.getSession().putClientData(catItemVarName, sysId);
        return;
    }

 

MediC_0-1742004398617.png

 

5 - Go to your reference variable to sys_db_object on your Variable Set. And set an advanced Reference qualifier as follow:

 

javascript&colon; "sys_idNOT IN" + session.getClientData("community_test"); // REPLACE community_test WITH YOUR VARIABLE SET TECHNICAL NAME

 

 

MediC_1-1742004499801.png

 

(Please follow the comments on the give script where you need to update some input with your variable / script include names)

 

I tested it on my PDI and it is working as expected. Selected tables are no longer displayed when adding a new row.

 

MediC_2-1742004846897.png

 

I hope it helps!

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

11 REPLIES 11

Amina El hadad
Tera Contributor

I tried this onChange client script, but it didn't work.

 

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {

      return;

   }

   var existingTables = [];

   //var systemTabs = g_form.getValue('new_tables');  // Multi-row variable set

   var systemTabs = JSON.parse(g_form.getValue('new_tables'));

   if (systemTabs) {

       try {

           systemTabs.forEach(function(row) {

               if (row['table']) {

                   existingTables.push(row['table']);

               }

           });

       } catch (e) {

           console.error("Error parsing JSON:", e);

           return; // Exit if JSON parsing fails

       }

   }

   // Remove already selected values

   removeUsedOptions('table', existingTables);  

}

 

function removeUsedOptions(fieldName, usedValues) {

   var selectElement = document.getElementById('sys_select.IO:' + fieldName);

   if (selectElement) {

       for (var i = selectElement.options.length - 1; i >= 0; i--) {

           if (usedValues.includes(selectElement.options[i].value)) {

               selectElement.remove(i);

           }

       }

   } else {

       console.warn("Select element not found for " + fieldName);

   }

}

 

Chaitanya ILCR
Kilo Patron

Hi @Amina El hadad ,

try this on submit client script 

what you are missing is "g_service_catalog.parent.getValue('new_tables')" this line

 

Instead of removing the row you can add an error message and don't let user submit the row by adding below on submit client script.

 

or you can g_service_catalog.parent.getValue('new_tables') this lien in the on change client script and customize it as per your need

 

function onSubmit() {

    var systemTabs = JSON.parse(g_service_catalog.parent.getValue('new_tables'));
    for (var i = 0; i < systemTabs.length; i++) {
        if (systemTabs[i]['table'] == g_form.getValue('table')) {
            g_form.addErrorMessage('table is already added');
            return false;
        }
    }
}

 

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

Regards,
Chaitanya

 

Hi @Chaitanya ILCR ,

Thank you for your comment.

I tried adding this line: "g_service_catalog.parent.getValue('new_tables')" but it still doesn't work.

Also, the script you provided isn't working. There seems to be an issue with JSON.parse because when I try to retrieve the object, I get an "Unexpected end of JSON input" error.

Hi @Amina El hadad ,

did you add the script to the MVRS? or to the catalog item?

is this the internal name of the MVRS "new_tables"  or is it different ?

please add the script to the MVRS

 

 

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

Regards,
Chaitanya