Iterate Through Variable Set To Check for Empty Value

lpruit2
Kilo Sage

Greetings all,

 

Is there an easy way to iterate through the list of Variables within a Variable Set and check if any are empty? Say I have a Variable Set with 10 Variables. I'm looking for a way to iterate through all the Variables within the Variable Set, check if they are empty, and if so, possibly display a field message alert. I also only want to capture the Variables within the Variable Set and NOT any of the other Variables outside of the Variable Set (i.e incident.variables.variable_set rather than incident.variables).

 

Is there an easier way rather than using if (g_form.getValue('variable1') == '' || g_form.getValue('variable1') == null) || .....?

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @lpruit2 ,

 

Typically you would make the variables mandatory so that the system takes care of checking if they are empty and showing alerts when they are.

 

But if you need to check this through a script you can do something like this:

 

var vars = [
	'NAME OF VARIABLE 1',
	'NAME OF VARIABLE 2',
	'NAME OF VARIABLE 3',
	'etc'
];

var emptyVars = vars.filter(v => !g_form.getValue(v));

emptyVars.forEach(v => g_form.showErrorBox(v, 'Variable is empty'));

 

Regards,

Robert

View solution in original post

4 REPLIES 4

Robert H
Mega Sage

Hello @lpruit2 ,

 

Typically you would make the variables mandatory so that the system takes care of checking if they are empty and showing alerts when they are.

 

But if you need to check this through a script you can do something like this:

 

var vars = [
	'NAME OF VARIABLE 1',
	'NAME OF VARIABLE 2',
	'NAME OF VARIABLE 3',
	'etc'
];

var emptyVars = vars.filter(v => !g_form.getValue(v));

emptyVars.forEach(v => g_form.showErrorBox(v, 'Variable is empty'));

 

Regards,

Robert

Thanks for the reply, Robert. Further backstory is that some of the Variables within the Variable set are String and some are Select Box. The Select Box's Options are being populated through another script that is pulling data from the table. This population overrides the --None-- option so hopefully this solution will be able to check. 

Hello @lpruit2 ,

 

The solution identifies all variables where the value is an empty string, which is also the case for select boxes that show "-- None --".

 

Regards,

Robert

lpruit2
Kilo Sage

Thank you for the solution, Robert. This worked. I'll need to think some more on how I'm filtering the array but it's a great solution.