Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Lookup multiple choice

omendersingh
Kilo Explorer

Hi

Anyone know how to set filter for a lookup multiple choice variable?

 

I want to display the values of replacement parts(lookup multiple choice) based on model selected.....

8 REPLIES 8

Hi Trena,



Did you ever get a resolution to your issue ?   I've been looking at something similar and am considering trying to insert a wait/delay until the preceding variables have changed from the default,   but it seems a bit of a clunky solution to me and if you've come up with a more elegant solution i would love to copy it.



Regards



John  


Hi John,


I ended up changing the variable type to be a "Lookup Select Box" instead of "Lookup Multiple Choice".   I believe the functionality of the Default made it impossible for it to work.   I used a GlideAjax call with the parameters being the user's selection on the form and the returned answer is an array of the values.   More complex answer, but that is what I had to do.   I believe there may be another posting of mine that talks about the Glide Ajax Call that was used in this solution.



Thanks,


Trena


Trena -- any change I can get a copy of your GlideAjax script and Script Include? I'm looking to do something similar.   Thanks!


Hi Kim,


Sure, I have an onChange Client Script that executes when a user clicks a checkbox to "Show Printer Choices"   based on pre-defined selections that the user made for the criteria of printers that they need...   Hope this helps!



Client Script:


------------------------------------------------------------------------------------------


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



if (newValue == 'true') {


        g_form.setReadOnly('newprt_NumberUsers', true);
        g_form.setReadOnly('newprt_SheetsPrinted', true);
        g_form.setReadOnly('newprt_Prescriptions', true);
        g_form.setReadOnly('newprt_DoubleSided', true);
        g_form.setReadOnly('newprt_color', true);
        g_form.setReadOnly('newprt_printerType', true);
        g_form.setReadOnly('newprt_IHIS', true);





  if ((g_form.getValue('variables.newprt_color') != 'None') && (g_form.getValue('variables.newprt_printerType') != 'None') && (g_form.getValue('variables.newprt_IHIS') != 'None') && (g_form.getValue('variables.newprt_Prescriptions') != 'None') && (g_form.getValue('variables.newprt_SheetsPrinted') != 'None') && (g_form.getValue('variables.newprt_NumberUsers') != 'None') && (g_form.getValue('variables.newprt_DoubleSided') != 'None')) {
   
    g_form.clearOptions('print_model_select_g');
    g_form.addOption('print_model_select_g', '', '', 1);
   
    var printerColor = g_form.getValue('variables.newprt_color');
    var printerType = g_form.getValue('variables.newprt_printerType');
    var printerIHIS = g_form.getValue('variables.newprt_IHIS');
    var printerPresc = g_form.getValue('variables.newprt_Prescriptions');
    var printerSheets = g_form.getValue('variables.newprt_SheetsPrinted');
    var printerUsers = g_form.getValue('variables.newprt_NumberUsers');
    var printerDoubleS = g_form.getValue('variables.newprt_DoubleSided');
   


    g_form.setDisplay('variables.print_model_select_g', true);


   
    var ga = new GlideAjax('GetLookupValuesAjax'); // Setup the GlideAjax object by giving it the name of the script include to call
    //sysparm_name is a parameter defined by convention.   It is the name of the function that will be called on the Script Include
    ga.addParam('sysparm_name', 'getLookupValues');                                                  
                                              ga.addParam('sysparm_printerColor', printerColor);
                                              ga.addParam('sysparm_printerType', printerType);
                                              ga.addParam('sysparm_printerIHIS', printerIHIS);
                                              ga.addParam('sysparm_printerPresc', printerPresc);
                                              ga.addParam('sysparm_printerSheets', printerSheets);
                                              ga.addParam('sysparm_printerUsers', printerUsers);
                                              ga.addParam('sysparm_printerDoubleS', printerDoubleS);

    ga.getXML(addOptions); // This is the GlideAjax magic.   It will call the server and suspend the script.   When it receives a response, addOptions will be called.   This is referred to as the callback function


                      //alert ('*After GlideAjax Call* addOptions = ' + addOptions);


      } else {
          g_form.setValue('variables.prt_show_choices', false);
          alert('All Printer Questions must be answered to show your options');


  }
}
}


function addOptions(response) {


                // More GlideAjax magic.  
                // It passes an XML document with the answer defined by the script include to the response parameter.  
                // This line pulls the answer out of the XML and converts the JSON string into an object:                  
var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));  


for (var i = 0; i < answer.length; i++) {
                      g_form.addOption('print_model_select_g', answer[i].short_description, answer[i].short_description);
}
}


------------------------------------------------------------------------------------------



Script Include:


------------------------------------------------------------------------------------------


// Note that the name is the same name passed to the GlideAjax object in the Client Script  


var GetLookupValuesAjax = Class.create();  


// Note the extendsObject.   AbstractAjaxProcessor is another Script Include provided by ServiceNow that handles the heavy lifting  


GetLookupValuesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {  
        // Note the name of this function is the same passed to the GlideAjax sysparm_name parameter in the client script  
       
        getLookupValues: function() {  
                  var gr = new GlideRecord('pc_hardware_cat_item');
                  var answer = [];


                  var printerColor = this.getParameter('sysparm_printerColor'); // param will now have the value of 'valueToPassHere'
                  var printerType = this.getParameter('sysparm_printerType'); // param will now have the value of 'valueToPassHere'
                  var printerIHIS = this.getParameter('sysparm_printerIHIS'); // param will now have the value of 'valueToPassHere'
                  var printerPresc = this.getParameter('sysparm_printerPresc'); // param will now have the value of 'valueToPassHere'
                  var printerUsers = this.getParameter('sysparm_printerUsers'); // param will now have the value of 'valueToPassHere'
                  var printerSheets = this.getParameter('sysparm_printerSheets'); // param will now have the value of 'valueToPassHere'
                  var printerDoubleS = this.getParameter('sysparm_printerDoubleS'); // param will now have the value of 'valueToPassHere'


                  var QryString = "category=604e09e26069210055dd239fccf49a07^u_printer_type=" + printerType + "^u_print_color=" + printerColor +   "^u_ihis_print=" + printerIHIS + "^u_prescription_print=" + printerPresc + "^u_user_number=" + printerUsers + "^u_sheet_count=" + printerSheets + "^u_double_sided_printing=" + printerDoubleS;


                  gs.log("**TEST1** - QryString is " + QryString);


                  gr.addEncodedQuery(QryString);
                  gr.query();


                  while (gr.next()) {
                      gs.log("In Query - short_description is " + gr.short_description);
                      answer.push({'short_description': gr.short_description + ''});
                  }
                  gs.log("**TEST2** - returned answer is " + answer);
                  var NewAnswer = new JSON().encode(answer); // Convert answer from object to string and return to the client


                  gs.log("**TEST3** - encoded answer is " + NewAnswer);      
                  return NewAnswer;
        },  
           
        type: "GetLookupValuesAjax"  
 
});  


------------------------------------------------------------------------------------------