Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need a groovy script for calculate two single select fields

Ravi
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.
January 27, 2021

Hi,

 

   Can you please provide me a groovy script for sum of two fields that display results in third field. I have tried using JCMF but it is not working as expected.

1. Cost1 - Selectlist single choice (1-5)

2. Cost2 - select list singlechoice (1-5)

3. Total - Scripted field Cost1+cost2

 

  Any help would be much appreciated.

 

Thanks,

Ravi

3 answers

1 accepted

2 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 27, 2021

Hi @Ravi there is similar thread: https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Calculate-the-value-of-two-single-select-custom-fields-that-have/qaq-p/1115641.

You just need to adjust custom fields names and to change multiplying to addition in the script of @Milan Chheda [INFOSYSTA] 

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2021

Try to use this code:

def costValueStr = issue.getCustomFieldValue(cost)?.getValue()
def selfValueStr = issue.getCustomFieldValue(selffinance)?.getValue()
if(!costValueStr || !selfValueStr){
log.error("Invalid values [costValue:{}, selfValue:{}]", costValueStr, selfValueStr)
}
def costValue = Double.parseDouble(costValueStr)
def selfValue = Double.parseDouble(selfValueStr)

return costValue + selfValue;
0 votes
Lih Yao Tiong
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.
January 29, 2021

Hi @Ravi , not sure if this helps.

But here's my sample Listener code on calculating 2 custom field's value:

import com.atlassian.jira.component.ComponentAccessor
def issueManager = ComponentAccessor.issueManager
def v1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("value1")
def v2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("value2")
// your issue key heredef issue = issueManager.getIssueByCurrentKey("TESTING-1")
// convert the value to int
def i = (issue.getCustomFieldValue(v1) ?: 0)
def j = (issue.getCustomFieldValue(v2) ?: 0)
return (i ?: 0) + (j ?: 0)
Ravi
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.
January 29, 2021

Hi @Lih Yao Tiong 

 

 Thank you but I get the following error, when I use the script in scripted field.

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.customfields.option.LazyLoadedOption.plus() is applicable for argument types: (com.atlassian.jira.issue.customfields.option.LazyLoadedOption) values: [5] Possible solutions: use([Ljava.lang.Object;), is(java.lang.Object), split(groovy.lang.Closure), wait(), dump(), grep() at Script108.run(Script108.groovy:9)

 

Thanks,

Ravi

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2021

@Ravi  you cannot use value of the field directly as it is "Option" and its value is of "String" type. I posted you a part of the script in my first thread which should fix your script.

Like Steffen Schuchardt likes this
0 votes
Ravi
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.
January 28, 2021

Here is my code 

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;

def customfieldManager = ComponentAccessor.getCustomFieldManager()

def cost = customfieldManager.getCustomFieldObjectByName("Cost");
def selffinance = customfieldManager.getCustomFieldObjectByName("SelfFinance");

def costValue = issue.getCustomFieldValue(cost) as Double
def selfValue = issue.getCustomFieldValue(selffinance) as Double

if(costValue > 0 && selfValue > 0) {
return costValue + selfValue;
} else {
return null;
}

I get the following error.

 

2021-01-28 13:58:16,390 ERROR [customfield.GroovyCustomField]: ************************************************************************************* 2021-01-28 13:58:16,391 ERROR [customfield.GroovyCustomField]: Script field failed on issue: GMOPMR-523, field: Total org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '2' with class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption' to class 'java.lang.Double' at Script88.run(Script88.groovy:14)

 

   Does it rely on the custom field type?

 

Thanks,

Ravi

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events