count the amount of selected checkboxes options

Hi,

i want to count how many checkbox options are selected! I've already seen something like this:

def count = 0;
def test1 = cfValues['Checkbox Customfield']?.getValue() == 'Option1';
def test2 = cfValues['Checkbox Customfield']?.getValue() == 'Option2';
	if(test){
		count++;
	}
	if(test2){
		count++;
	}

 However, this code does not work! Does anyone has another idea how it could work? Maybe in a less complicated way?

 

Best regards,

Louisa

 

 

2 answers

1 accepted

This widget could not be displayed.

 So with the following code it works:

def numberselected = issue.getCustomFieldValue(ComponentManager.getInstance().getCustomFieldManager().getCustomFieldObjectByName("Checkbox Customfield")).size();

 Thanks Nic for your help smile

 

Best regards,

Louisa

This widget could not be displayed.

I'd go with

def numberselected = cfValues['Checkbox Customfield']?.getValue().size()

The getValue should be returning an array of all the currently selected options, so all you should need to do is count them, and you can do that with the size function.

Thank you for fast response! Unfortunately, it still does not work. Do I have to define cfValues? Or do I have to import anything to be able to use cfValues?

Ah, I was assuming you'd only given a snippet of script and had already dealt with fetching cfValues in a bit you hadn't posted. From scratch, you should be able to use this in a scripted field: def numberselected = getCustomFieldValue("Checkbox Customfield")?.getValue().size() If you're running this somewhere else, it may need to change.

I'll use it in a scripted postfunction! I tried: def numberselected = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Checkbox Customfield"))?.getValue().size(); But it still does not work!

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

290 views 5 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you