Call an array in the Catalog Client Script

xhensilahad
Tera Expert

Good afternoon,

I am trying to pass an array from a Script Inlcude to a Catalog Client Script.

I have tested my Script Include at it is returning the correct values, as a coma separated list.

I want to call this list in my Catalog Client Script, and hide all these variables.

 

Please can you help me with my Catalog Client Script.

 

Script Include:

(Client Callable is true)

var VariableTypeChecker = Class.create();
VariableTypeChecker.prototype = {
    getVariableTypes: function(ritmSysId) {
    var results = [];
        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmSysId)) {
            var variables = ritm.variables.getElements();

            for (var i = 0; i < variables.length; i++) {
                var question = variables[i].getQuestion(); // Access variable metadata
                if (question) {
                    var name = question.getName(); 
                    var type = question.getType(); 
                    var value = variables[i].getValue();
                    // Check for checkbox type (type 7) and value 'false'
                    if (type === '7' && value === 'false') {
                        results.push(name); /
                    }
                }
            }
        }
        return results.toString();
    },
    type: 'VariableTypeChecker' 
};
 
Catalog Client Script
 
function onLoad() {
   var ritmSysId = g_form.getUniqueValue();
  
    var ga = new GlideAjax('VariableTypeChecker');
    ga.addParam('sysparm_name', 'getVariableTypes');
    ga.addParam('sysparm_ritmSysId', ritmSysId);
    ga.getXMLAnswer(hideVariables);
 
    function hideVariables(response) {
        var answer = response;
 
        var variableNames = JSON.parse(answer);
       
        for (var i = 0; i < variableNames.length; i++) {
        var variableName = variableNames[i];
        g_form.setDisplay(variableName, false);
             }
         }
    }
 
 
Thank you
6 REPLIES 6

anshul_goyal
Kilo Sage

Hello @xhensilahad,

Try below code:

Script Include:

 

 

 getVariableTypes: function() {
     var results = [];
     var ritmSysId = this.getParameter('sysparm_ritmSysId');
     var ritm = new GlideRecord('sc_req_item');
     if (ritm.get(ritmSysId)) {
         var variables = ritm.variables.getElements();

         for (var i = 0; i < variables.length; i++) {
             var question = variables[i].getQuestion(); // Access variable metadata
             if (question) {
                 var name = question.getName();
                 var type = question.getType();
                 var value = variables[i].getValue();
                 // Check for checkbox type (type 7) and value 'false'
                 if (type === '7' && value === 'false') {
                     results.push(name);
                     /
                 }
             }
         }
     }
     return results.toString();
 },

 


Client Script:

function onLoad() {
   var ritmSysId = g_form.getUniqueValue();
  
    var ga = new GlideAjax('VariableTypeChecker');
    ga.addParam('sysparm_name', 'getVariableTypes');
    ga.addParam('sysparm_ritmSysId', ritmSysId);
    ga.getXMLAnswer(hideVariables);
 
    function hideVariables(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
 
        var variableNames = JSON.parse(answer);
       
        for (var i = 0; i < variableNames.length; i++) {
        var variableName = variableNames[i];
        g_form.setDisplay(variableName, false);
             }
         }
    }

 

I made a small change in your script include and client script. If you're calling GlideAjax and passing the RITM sys_id, you need to use this.getParameter('sysparm_ritmSysId') instead of using an argument-based function.



Please mark my solution as Helpful and Accepted, if it works for you in any way!

Thanks

Thank you Anshul,

Can you please kindly review the Catalog Client script, as it is still not hiding the Unchecked Checkboxes from the RITM.

Thank you

I have added an alert in the Catalog Client Script, but nothing is being returned:

 
function hideVariables(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);

@xhensilahad, There is a mistake in your code var ritmSysId = g_form.getUniqueValue();
This line returns the catalog item's sys_id, not the sys_id of the requested item record.

Could you clarify the requirement? Do you want to hide those variables on the catalog item itself, or after the requested item has been created?

Thanks