Count number of options selected in List collector in Service portal

jaisonjoy
Giga Contributor

Hello dear experts,

I want to display a short description field if there are more than 3 options selected in the right bucket of the list collector, but my code isn't working in the service portal. Its probably because the gel function doesn't work for portal. The below is the code that works in the native view and doesn't in the Service Portal:

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

     

  if(isLoading)

  return;

      var varName = 'swItem_costcenter';

      var rightBucket = gel(varName + '_select_1');

      var selectedOptions = rightBucket.options;

  var len = selectedOptions.length;

      // Determine if more than 1 cost center is selected, if so cost distribution should be determined      

      if( len > 1){

              g_form.setDisplay('swItem_cost_distribution', true);

              g_form.setMandatory('swItem_cost_distribution', true);

      }else{

              g_form.setDisplay('swItem_cost_distribution', false);

              g_form.setMandatory('swItem_cost_distribution', false);

              g_form.setValue('swItem_cost_distribution', '');

      }

}

1 ACCEPTED SOLUTION

Thanks everyone. I was able to do this with the use of of the Split function. Values in the right bucket in the List Collector are separated by commas. I was able to make use of that fact.



Here's the code:




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



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


  g_form.setDisplay('swItem_cost_distribution', false);


              g_form.setMandatory('swItem_cost_distribution', false);


              g_form.setValue('swItem_cost_distribution', '');          


  return;


    }


         


  value = g_form.getValue('swItem_costcenter');


  len = value.split(',').length;


// Determine if more than 1 cost center is selected, if so cost distribution should be determined    


      if( len > 1){


              g_form.setDisplay('swItem_cost_distribution', true);


              g_form.setMandatory('swItem_cost_distribution', true);


      }else{


              g_form.setDisplay('swItem_cost_distribution', false);


              g_form.setMandatory('swItem_cost_distribution', false);


              g_form.setValue('swItem_cost_distribution', '');


      }


}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



Thanks again everyone!


View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Jaison,



You may find the below thread helpful.


Re: What is the alternative for gel() on service portal


reginabautista
Kilo Sage

In addition, once you've sorted out/converted gel, you would need to set the Catalog Client script's UI type to 'Both' (Helsinki) or 'All' (Istanbul) for your scripts to work in service portal.


Regina - Thanks for that. Yes I've already set the UI Type of the Client script in the Istanbul instance as 'All'.