ifScript - Count Checkbox to answer True of False

Mark Lanning
Tera Guru

Currently working on a Requirement - Where I have 8 Checkboxes, depending on the number of Checkboxes that get selected, I need the Workflow to take two different paths.

If 1 Checkbox is selected, path A

If 2 or more are selected, path B

 

Everything I am finding is OnSubmit, or OnChange Script, nothing with IfScripts and I am wondering if its possible.

 

answer = ifScript();
var ch1 = g_form.getValue('variable name ');
var ch2 = g_form.getValue('variable name ');
var ch3 = g_form.getValue('variable name ');
var ch4 = g_form.getValue('variable name ');
var ch5 = g_form.getValue('variable name ');
var ch6 = g_form.getValue('variable name ');
var ch7 = g_form.getValue('variable name ');
var ch8 = g_form.getValue('variable name ');
var cnt = 1;


function ifScript() {
    if (ch1 == 'true') {
        cnt++;
    }
    if (ch2 == 'true') {
        cnt++;
    }
    if (ch3 == 'true') {
        cnt++;
    }
    if (ch4 == 'true') {
        cnt++;
    }
    if (ch5 == 'true') {
        cnt++;
    }
    if (ch6 == 'true') {
        cnt++;
    }
    if (ch7 == 'true') {
        cnt++;
    }
    if (ch8 == 'true') {
        cnt++;
    }
    if (cnt >= count) {
        answer = true; {


            return 'false';
        }
    }
}
2 ACCEPTED SOLUTIONS

Peter Bodelier
Giga Sage

Hi @Mark Lanning 

 

You're not far off, but I would structure it a bit differently.


I presume you are running this in an if action within a workflow? If so, you do not have g_form available.

 

Try something like this:

 

answer = ifScript();

function ifScript() {
	
	var fieldNames = ['field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8'];
	var count = 0;
	
	for (var i = 0; i < fieldNames.length; i++){
		
		if (current.variables[fieldNames[i]] == true){
			count++;
		}
	}
	return count > 1; //false if 1, true if more than 1
	
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

Hi Mark,

 

I assumed you were talking about a catalog item, but I realize you didn't state that.

 

If it's for a regular record, you can use this script:

 

 

  answer = ifScript();

function ifScript() {
	
	var fieldNames = ['field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8'];
	var count = 0;
	
	for (var i = 0; i < fieldNames.length; i++){
		
		if (current[fieldNames[i]] == true){
			count++;
		}
	}
	return count > 1; //false if 1, true if more than 1
	
}

 

 

If it's indeed for a requested item with variables, you can use this script (I used true/false instead of yes/no before)

 

  answer = ifScript();

function ifScript() {
	
	var fieldNames = ['field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8'];
	var count = 0;
	
	for (var i = 0; i < fieldNames.length; i++){
		
		if (current.variables[fieldNames[i]] == 'Yes'){
			count++;
		}
	}
	return count > 1; //false if 1, true if more than 1
	
}

 

 

 

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

3 REPLIES 3

Peter Bodelier
Giga Sage

Hi @Mark Lanning 

 

You're not far off, but I would structure it a bit differently.


I presume you are running this in an if action within a workflow? If so, you do not have g_form available.

 

Try something like this:

 

answer = ifScript();

function ifScript() {
	
	var fieldNames = ['field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8'];
	var count = 0;
	
	for (var i = 0; i < fieldNames.length; i++){
		
		if (current.variables[fieldNames[i]] == true){
			count++;
		}
	}
	return count > 1; //false if 1, true if more than 1
	
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Thank you for your help Peter,

After testing this more, it will not return true or false.

Hi Mark,

 

I assumed you were talking about a catalog item, but I realize you didn't state that.

 

If it's for a regular record, you can use this script:

 

 

  answer = ifScript();

function ifScript() {
	
	var fieldNames = ['field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8'];
	var count = 0;
	
	for (var i = 0; i < fieldNames.length; i++){
		
		if (current[fieldNames[i]] == true){
			count++;
		}
	}
	return count > 1; //false if 1, true if more than 1
	
}

 

 

If it's indeed for a requested item with variables, you can use this script (I used true/false instead of yes/no before)

 

  answer = ifScript();

function ifScript() {
	
	var fieldNames = ['field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8'];
	var count = 0;
	
	for (var i = 0; i < fieldNames.length; i++){
		
		if (current.variables[fieldNames[i]] == 'Yes'){
			count++;
		}
	}
	return count > 1; //false if 1, true if more than 1
	
}

 

 

 

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.