calculated custom field to show result

Raju Adluru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 18, 2014

I want calculate a score based on selection of different fields.

Here is the scenario

I have few drop down fields and radio button fields ( custom fields), each selection has a value to it.

based on selection, it should calculate value and shown in "Score" - custom field.

e.g.

1, radio button field: for option "yes" - value - 4

for option "no" - value - 0

2. drop down field : for option 1 - value - 5

for option 2 - value 5

for option 3 - value 4

for option 4 - value 4

3. drop down field : for option 1 - value 5

for option 2 - value 4

..

.

.

likewise 5 dropdown fields with multiple options and values for those.

after selecting all above fields, my score field should show ( addition of values of selected options)

Can somebody help me with code or guide me the correct plugin for this?

Thanks and appreciate your help.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2014

Hi Raj,

here code for your scripted field :

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;

Issue issue  = issue;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField radio = customFieldManager.getCustomFieldObjectByName("Radio Field Name");
CustomField customField1 = customFieldManager.getCustomFieldObjectByName("Drop down Field Name 1");
CustomField customField2 = customFieldManager.getCustomFieldObjectByName("Drop down Field Name 2");
Integer result = new Integer(0);
if(issue.getCustomFieldValue(radio)!=null){
	if("yes".equalsIgnoreCase(issue.getCustomFieldValue(radio).toString())){
		result = result + 4;
	}
}
if(issue.getCustomFieldValue(customField1)!=null){
	if("option name 1".equalsIgnoreCase(issue.getCustomFieldValue(customField1).toString())
			|| "option name 2".equalsIgnoreCase(issue.getCustomFieldValue(customField1).toString()) ){
		result = result + 5;
	} else {
		if("option name 3".equalsIgnoreCase(issue.getCustomFieldValue(radio).toString())
				|| "option name 4".equalsIgnoreCase(issue.getCustomFieldValue(radio).toString()) ){
			result = result + 4;
		}
	}
}
if(issue.getCustomFieldValue(customField2)!=null){
	if("option name 1".equalsIgnoreCase(issue.getCustomFieldValue(customField2).toString())){
		result = result + 5;
	} else {
		if("option name 2".equalsIgnoreCase(issue.getCustomFieldValue(customField2).toString())){
			result = result + 4;
		}
	}
}
return result;

Hope this helps.

Please change option name and custom field name at your convenience. Use "Script Runner" plugin that allows you to define a scripted field.

Regards,

Fabio

Raju Adluru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2014

Fabio

thank you so much, it is working great, i need to add some more options and test it out, appreciate your help.

Raju Adluru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2014

Hi Fabio

when i have the "$" in the name of custom field or option values, i am getting error.

for option values i am putting backward slash "over \$500k" is this ok?

how to avoid this error? appreciate your help.

- javax.script.ScriptException: java.lang.NullPointerException

A stacktrace has been logged.

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 22, 2014

Hi Raj,

try to use "\u0024" instead of "$". Let me know if it fixs your issue.

Regrds,

Fabio

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 22, 2014

\$ should be fine... any reason to use the unicode char over \$ ? The latter is clearer to me.

You can use single quotes if you don't want interpolation - either of these should work:

"\$5" or '$5'

Raju Adluru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 22, 2014

Hi Fabio

It did not help, i have removed $ sign from custom field name and options, still i am getting same error, did reindex also, still no use.

when i remove code for that field, remaining code is working great.

thanks for your help.

Raju Adluru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 22, 2014

@Jamie

\$ is working fine, as suggested in warning message!

My issue is : one custom field is giving NullPointer exception, previously it had $ in the name and options, i removed $ in all, still error is coming!!

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 22, 2014

Hello Raj,

please could you post the whole script code? I could try to figure out the problem.

Regrds,

Fabio

Raju Adluru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 22, 2014

Thanks for all your help, reindexing did not help, somehow old value with '$' residing somewhere, so i had to put back $ with \$, now it is working fine, thanks for all your help. appreciate it.

TAGS
AUG Leaders

Atlassian Community Events