If script in a workflow to check if list collector is empty

pothukuchirsnh
Kilo Explorer

I am trying to use an ifscript to check if a list collector selected values is empty or not. If it's not empty, I will generate tasks based on the selection.

Right now my script looks like this but I see a warning:

1 ACCEPTED SOLUTION

Try this (typo also corrected on line 2)



answer = ifScript();


function ifScript() {


var accessoriesList = current.variables.accessories.toString(); //convert the variable list to string


var arrayList = accessoriesList.split(",");



  if (arrayList.length > 0) {


            return 'yes';


  } else {


          return 'no';


        }


}


View solution in original post

7 REPLIES 7

bhoomikabisht
Kilo Expert

Hi Ravikant,



Please give your script here...


Chuck Tomasi
Tera Patron

In lieu of an if script, here is what I suggest:



answer = ifScript();



function ifScript() {


        var val = current.getValue('watch_list');


        // Check if it is empty


        if (val.nil()) {


                  return "Yes";


        } else {


                  return "No";


        }


}


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ravikant,



Use an if condition in the workflow and assuming that your workflow is on RITM table here is the script you can use:


use your variable name in the variableName highlighted in bold.



answer = ifScript();



function ifScript() {


        var val = current.variables.<variableName>;


        if (JSUtil.nil(val)) {


                  return "Yes"; // is empty


        } else {


                  return "No"; // not empty


        }


}



The transition from "No" you can take to Task generation since list collector is not empty



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader