Service Catalog - Client Script To Add Options To A Drop Down Field

billv6
Kilo Expert

Hello,

 

This is my first post here and I would like a help with a Client Script in the Service Catalog.

 

I have a Catalog Item named FILENET, inside the FILENET variable set, I have two variables (filenet_lob_1 and filenet_Role_1), these two variables should contain some of the Maintain Item Choices and the second variable (filenet_Role_1) MUST be dependent of what was chosen in the first variable (filenet_lob_1).

 

So, I've made the script below and it is not working, not sure if I'm doing it wrong or the problem above can not be solved using Client Script:

 

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

   

    if(newValue == oldValue){

          return;

    }

   

    //build a new list of dependent options

    var gp = new GlideRecord('sys_choice');

    gp.addQuery('Commercial', newValue);

    gp.addQuery('Field Underwriter/ Service Operations', 'filenet_Role_1');

    gp.addQuery('Field Underwriter/ Service Operations w/ Restricted Access', 'filenet_Role_1');

    gp.query();

    while(gp.next()){

          if (gp == "Commercial") {

                g_form.addOption('filenet_Role_1', gp.value, gp.label);

                g_form.addOption('filenet_Role_1', gp.value, gp.label);

          }

          else {

                g_form.clearOptions('filenet_Role_1');

          }

    }

}

 

I appreciate any ideas or some help! 🙂

 

Thanks!

7 REPLIES 7

randrews
Tera Guru

i think you are making this a little more complex than it has to be...


why are you trying to look up the values on the sys_choice table??



the values should be set in the form itself.. so you can use either   a g_form.getValue('variablename');


or if they are reference fields g_form.getReference('variablename');



to add your choices...



one note.. and this is a possible issue.. if your selections are ALL listed in the initial drop down choice list they won't appear on the catalog tasks that you generate....



so first build the drop down to include all possible selections


then at the top of your scrip put in a   g_form.clearOptions('variablename'); to clear the list


then the script to build a new list.


billv6
Kilo Expert

Thanks randrews! Good point, I was making the things more complex than it has to be, you are absolutely right! 🙂



I've got this partially works with your tips, now I'm working on this other script and my issue is to make this works every time the option in the first drop down field changes:



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


   


    g_form.clearOptions('filenet_Role_1');


   


    var lob1 = g_form.getValue('filenet_lob_1');


   


    if (lob1 == "Commercial") {


          g_form.addOption('filenet_Role_1', 'Field Underwriter/ Service Operations', 'Field Underwriter/ Service Operations');


          g_form.addOption('filenet_Role_1', 'Field Underwriter/ Service Operations w/ Restricted Access', 'Field Underwriter/ Service Operations w/ Restricted Access');


    }


   


    if (lob1 == "Personal") {


          g_form.addOption('filenet_Role_1', 'Field Underwriter/ Service Operations', 'Field Underwriter/ Service Operations');


          g_form.addOption('filenet_Role_1', 'Field Underwriter/ Service Operations w/ Restricted Access', 'Field Underwriter/ Service Operations w/ Restricted Access');


    }


   


    if (lob1 == "Claims") {


          g_form.addOption('filenet_Role_1', 'Adjuster', 'Adjuster');


          g_form.addOption('filenet_Role_1', 'Manager', 'Manager');


          g_form.addOption('filenet_Role_1', 'Adjuster/Operations (no inbox) *Approval Required', 'Adjuster/Operations (no inbox) *Approval Required');


          g_form.addOption('filenet_Role_1', 'Technical', 'Technical');


          g_form.addOption('filenet_Role_1', 'QA', 'QA');


          g_form.addOption('filenet_Role_1', 'Risk Services', 'Risk Services');


    }


   


    if (lob1 == "Vendor") {


          g_form.addOption('filenet_Role_1', 'Indexing for all LOBs (no inbox)', 'Indexing for all LOBs (no inbox)');


          g_form.addOption('filenet_Role_1', 'Medical Review', 'Medical Review');


          g_form.addOption('filenet_Role_1', 'Claims Support-TMG', 'Claims Support-TMG');


          g_form.addOption('filenet_Role_1', 'Claims Support Manager-TMG', 'Claims Support Manager-TMG');


          g_form.addOption('filenet_Role_1', 'Commercial Lines Support-TMG', 'Commercial Lines Support-TMG');


          g_form.addOption('filenet_Role_1', 'Personal Lines Support-TMG', 'Personal Lines Support-TMG');


    }


   


    if (lob1 == "Other") {


          g_form.addOption('filenet_Role_1', 'IT Support', 'IT Support');


          g_form.addOption('filenet_Role_1', 'Auditor - Nothing By Default', 'Auditor - Nothing By Default');


    }


   


    else {


          g_form.clearOptions('filenet_Role_1');


    }


}


randrews
Tera Guru

one other thing you may consider doing is adding a data table that lists all the roles


then you can just make the role a reference field using the lob as a reference qualifier... either way works and i have done both.



if it is an onchange based on the lob variable it should reset the role everytime you change LOB.


Hi Andrews,



I am also trying to populate values in drop down(Lookup Select Box) from a custom table which is having few users and these users will be populated as per the level and catalog item that is being requested.



In the "Reference qual" field, I am calling a script include which will be responsible for retrieving users from the table and populating in the drop down. However everything is working fine except filtering of records for drop down(all records from table are populated) or you can say retrieved information is not getting populated in drop down.




Below is the code in script include:



getApprovers:function(catId){


  gs.addInfoMessage(catId);


  g_form.clearOptions('u_a_approver'); // this line of code is not working, processing is done further only if this line is removed.


  gs.addInfoMessage('building query');



  var gp = new GlideRecord('u_approvers');



  gp.addQuery('u_catalog_id','Guest Wireless (Visitors)');


  gp.addQuery('u_approver_type','A');


  gp.addQuery('u_active',1);


  gp.query();


  gs.addInfoMessage('query executed');



  while(gp.next()){


    gs.addInfoMessage(gp.u_user+'|'+gp.u_user_name); // displaying correction information


    g_form.addOption('u_a_approver',gp.u_user,gp.u_user_name); // again this line of code is not working. it seems I am not using g_form object properly


  }



}



It would be helpful if you could help me out.




Thanks


Tushar