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

BALAJI40
Mega Sage

Use the length command to calculate number of options selected.



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


   


  if(isLoading)


  return;



  var len = g_form.getValue('<variable name of list collector>').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', '');


      }


}



This onChange script will run when ever you are selected the option from left to right.


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!