Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Count items selected in a list variable

Cirrus
Kilo Sage

Please could someone advise how to solve this.

I have a catalog item with a list variable, and I want to be able to count the number of elements selected, but if certain items are added to the list, I want to exclude these from the count. I already have two on change scripts doing similar things:

To count all items:

  value = g_form.getValue('catalogue');
   len = value.split(',').length;
    g_form.setValue('count', len);

And to manage behaviours based on items selected:

 var flg = 'false';
    //check the list collect doesnt contain any free items or is not empty
    var str = newValue.toString();
    var arr = str.split(',');
       for (var i = 0; i < arr.length; i++) {
        if (!((arr[i] == '5fb4d3091bd51d505ab1646fe54bcbea') || (arr[i] == ""))) {
        flg = 'true';
            break;
        }
    }if (flg == 'true........

I tried joining them with my limited scripting ability as follows, but the count still increments despite the free item 5fb4 being selected (unless its the only item). I know its because its set to count all values in the catalog field, but how can I set the count to ignore 5fb4 if its selected.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 var str = newValue.toString();
    var arr = str.split(',');
       for (var i = 0; i < arr.length; i++) {
        if (!(arr[i] == '5fb4d3091bd51d505ab1646fe54bcbea')) {
        
    value = g_form.getValue('catalogue');
   len = value.split(',').length;
    g_form.setValue('count', len);
        }
  }
}

 

 

Thanks

1 ACCEPTED SOLUTION

Cirrus
Kilo Sage

Managed to solve it with the following:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var a = '0';
    var str = newValue.toString();
    var arr = str.split(',');
    for (var i = 0; i < arr.length; i++) {
        
        if (arr[i] == '5fb4d3091bd51d505ab1646fe54bcbea') {
         a = '1';    
        }
    value = g_form.getValue('catalogue');
   len = value.split(',').length;
    g_form.setValue('count', (len-a));
}
}

View solution in original post

1 REPLY 1

Cirrus
Kilo Sage

Managed to solve it with the following:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var a = '0';
    var str = newValue.toString();
    var arr = str.split(',');
    for (var i = 0; i < arr.length; i++) {
        
        if (arr[i] == '5fb4d3091bd51d505ab1646fe54bcbea') {
         a = '1';    
        }
    value = g_form.getValue('catalogue');
   len = value.split(',').length;
    g_form.setValue('count', (len-a));
}
}