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
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]
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;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.