JIRA Groovy Script - How To Get A Custom Field's Value Of An Issue

Do you like this?

Summary:
This script shows you how to get custom fields' value of an issue.


Content:

Suppose we have an issue with its key "Issue_Key". Also we create 2 custom fields: Technical Risk and Business Risk as List<Single Option>. Their values are as follows:
Technical Risk

  • Value50
  • Value70
  • Value100
Business Risk
  • Value50
  • Value80
  • Value100
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.config.ResolutionManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;

ComponentManager cm= ComponentManager.getInstance()
IssueManager im= cm.getIssueManager()
MutableIssue issue=im.getIssueObject("Issue_Key")
cfm= cm.getCustomFieldManager()


TECHNICAL_RISK_NAME="Technical Risk"
TECHNICAL_RISK_MAP=["Value50":50,
	"Value70":70,
	"Value100":100]

BUSINESS_RISK_NAME="Business Risk"
BUSINESS_RISK_MAP=["Value50":50,
	"Value80":80,
	"Value100":100]


def int getFieldValue(fieldName, issue, mapping){
	CustomField trf= cfm.getCustomFieldObjectByName(fieldName)
	return mapping[trf.getValue(issue)?.getValue()]
}

return getFieldValue(TECHNICAL_RISK_NAME, issue, TECHNICAL_RISK_MAP) * getFieldValue(BUSINESS_RISK_NAME, issue, BUSINESS_RISK_MAP)

 
comments powered by Disqus