How to get value of a JIRA custom field using the field id using groovy

Nishant Kansal March 13, 2016

Hello,

I am new to groovy and looking for help for the following:

From my JIRA administrator, I got the following id's having database related information.

Database Server Type: ID=10513

Database Server Name: ID=12906

Database Name: ID=12911

Database Username: ID=12907

Database Password: ID=12908

My requirement: I have to develop a groovy script that should fetch the corresponding values based upon the ID's above and next, login into JIRA database using those values. I found groovy has sql class that allows connection to database. For that, I require values from the above ID's.

Please help.

Thanks,

Nishant

5 answers

0 votes
Steve Johnson September 24, 2020

This is the solution that worked for me

 

import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

// Filed id : customfield_16306 --> This is field ID for Impact Score.
def cf = getFieldById("customfield_16306");
//int cFieldValue = (int)(cf.getValue())
def cFieldValue = (int)(cf.getValue() ? cf.getValue() : 0)

if (cFieldValue >= 1 && cFieldValue <= 100 )
{
cf.clearError()
}
else if (cFieldValue < 1 || cFieldValue > 100 )
{
//below Command displays Error and but allows the user to submit the change. If you are enabling below comment setError
//cf.setHelpText("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : "+ cFieldValue)

cf.setError("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : "+ cFieldValue)

}
else
{
cf.setError("Impact Score provided should be between 1 - 100. Please correct Impact Score to an acceptable range. Current score : (Blank) ")
}

0 votes
Ahsan January 20, 2020

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.screen.FieldScreenFactory
import com.atlassian.jira.issue.fields.screen.FieldScreenManager
import com.atlassian.jira.issue.fields.screen.FieldScreenSchemeManager
import com.atlassian.jira.web.action.admin.issuefields.screens.ViewFieldScreens
import com.atlassian.jira.workflow.WorkflowManager

def sb = new StringBuffer()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_id")

sb.append("${cField}<br/>\n")

return sb.toString()

0 votes
aditya mandlekar February 9, 2018
How can we convert the below line of code 

def issue = ComponentAccessor.getIssueManager()
.getIssueByCurrentKey("MFP-10");

to make it more generic in a way where it can work for any Key
0 votes
Aleks Yenin (Polontech)
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.
March 14, 2016

Get CustomField value:

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;

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("MFP-10");
def customField =  ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10301L);//customfield id
if(customField != null) {
	issue.getCustomFieldValue(customField);
}

Note: You must have Issue to get value from, because Customfield does not save data by itself.

Mohit Jayesh Patel July 10, 2017

Hi Aleks,

I tried your code and it worked for me, however what I am trying to do is also automatically fetch the current issue key and use the custom field value in a sql query. I am getting the custom field value if I hardcode my issue key. The sql too isnt functioning. Please point out where I am going wrong.  I get null returned for this.

Following is my code: 

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;
import com.atlassian.jira.issue.IssueManager
import groovy.sql.Sql
import java.sql.Driver

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TR-24");
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11495L);//customfield id

def bugid = issue.getCustomFieldValue(customField);


def driver = Class.forName('com.mysql.jdbc.Driver').newInstance() as Driver

def props = new Properties()
props.setProperty("user", "rxy")
props.setProperty("password", "rxy")


def conn = driver.connect("jdbc:mysql://localhost:3306/bug", props)
def sql = new Sql(conn)

try {
sql.eachRow("select p.realname from bugs.bugs b join bugs.profiles p on b.qa_contact=p.userid where bug_id = ${bugid}") {
log.debug(it)
}
} finally {
sql.close()
conn.close()
}

 

 

 

Tushar August 27, 2017

Dear All,

 

I am looking for the script which will get the external values in the Jira custom field.

Can someone help me on this scripting?

Your help will be much appreciable.

Thanks,

0 votes
Vasiliy Zverev
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.
March 13, 2016

Do you need to do this with external tool (not JIRA) or from JIRA itself?

Groovy is user for internal scripts.

For external tool I wood recommend you to start with JIRA REST API: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials

 

Nishant Kansal March 14, 2016

Thanks Vasiliy for your guidance.

I have to execute the script from JIRA itself as a post function. Further, with the help of different posts, I have developed the below script. All I am trying is to find the field value against the field id:10513.

---------------------------------------------------------------------------------------------------------------------------------------

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

void customFieldValues (String customFieldId) {

// gets a reference to the IssueManager and CustomFieldManager manager classes
final CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

// gets a reference to the desired custom field
final CustomField customField = customFieldManager.getCustomFieldObject("customFieldId")

// retrieves the custom field value object from the issue

final Object customFieldValue = getCustomFieldValue("customField") 
// prints value to console
System.out.println("Custom field value: " + customFieldValue)
}
customFieldValues("10513")

---------------------------------------------------------------------------------------------------------------------------------------

However, it is giving below error:

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (java.lang.String) values: [customField] at Script691.sumCustomFieldValues(Script691.groovy:20) at Script691.run(Script691.groovy:24)

Could you please help me here?

Thanks,

Nishant

Aleks Yenin (Polontech)
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.
March 14, 2016

There is no such a function  getCustomFieldValue in your code. There can be 

issue.getCustomFieldValue(customFieldObject);

Also do not use double qoutes here

customFieldManager.getCustomFieldObject("customFieldId")

because you it will be transformed to String. Use it without scopes:

customFieldManager.getCustomFieldObject(customFieldId)

 

 

Nishant Kansal March 14, 2016

Thanks Aleks for all the tips. I tried your your provided script as below with currentkey and customfieldid per my JIRA set-up. However, it is giving "Null" response. Any help on this, please?

--------------------------------------------------------------------------------------------------------------------------

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;
 
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("MFP-10");
def customField =  ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10301L);//customfield id
if(customField != null) {
        issue.getCustomFieldValue(customField);
}

---------------------------------------------------------------------------------------------------------------------------

Thanks,

Nishant

Aleks Yenin (Polontech)
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.
March 14, 2016

My code is just an example.

Here 

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("MFP-10");

input your own Issue Key instead of my "MFP-10"

and here 

def customField =  ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10301L);

must be uor own customfield id instead of my 10301L (e.g. 10513L for Database Server Type Custom Field)

Nishant Kansal March 14, 2016

Yes, that I understood and did the same. In my case, the Issue Key value was KWD-1 and customfield id was 10001. It is still returning null...sad

Aleks Yenin (Polontech)
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.
March 14, 2016

Please check again if there is value for customfield 10001 in issue KWD-1. THiscode was checked in sciptrunner and it works fine.

Nishant Kansal March 16, 2016

Hi Aleks,

Thanks a lot, your script worked.

Further, I have extended it as below to store the custom field values into an array for further processing. However, it is giving null values.

I have double checked and each of the field id have valid value against it.

Please help.

----------------------------------------------------------------------

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;

def fieldid = new int[5] //array storing the customfield id
def fieldvalue= new String[5] //array to store field values corresponding to each custom field for a given ticket
fieldid[0] = 10513
fieldid[1] = 12906
fieldid[2] = 12911
fieldid[3] = 12907
fieldid[4] = 12908

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TIK-3039");

for (i = 0; i <5; i++) {
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);//customfield id
if(customField != null) {
final Object customFieldValue = issue.getCustomFieldValue(customField);
fieldvalue[i] = "customFieldValue";
}}
for (i = 0; i <5; i++){
System.out.println("Custom field value: " + fieldvalue[i]);
}

-------------------------------------------------------------------------

Vasiliy Zverev
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.
March 16, 2016

Custom field id is long type, but not int. 

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;
def fieldid = new long[5] //array storing the customfield id
def fieldvalue= new String[5] //array to store field values corresponding to each custom field for a given ticket
fieldid[0] = 10513L
fieldid[1] = 12906L
fieldid[2] = 12911L
fieldid[3] = 12907L
fieldid[4] = 12908L
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TIK-3039");
for (i = 0; i &lt;5; i++) {
    def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);//customfield id
    if(customField != null) {
        final Object customFieldValue = issue.getCustomFieldValue(customField);
        fieldvalue[i] = "customFieldValue";
    }
}
for (i = 0; i &lt;5; i++){
    System.out.println("Custom field value: " + fieldvalue[i]);
}
Nishant Kansal March 17, 2016

Thanks Vasiliy for pointing it out. However, script is still returning null.

I tried putting println at different points to debug the script. It seems "customField" is getting null value all the time within FOR loop. with my little knowledge, I am feeling that I am still not using arrays and then FOR loop correctly. May be in (fieldid[i]), value of i is not getting substituted.

Please help.

Vasiliy Zverev
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.
March 17, 2016

To test a script I would recommend to use Script Console and use retun statement. 

At first lets try to get a custom field object like this:

import com.atlassian.jira.component.ComponentAccessor

return ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10513L).getName()
Like Govind likes this
Vasiliy Zverev
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.
March 17, 2016

And you should to see custom field name

Nishant Kansal March 17, 2016

Thanks Vasiliy for your continued support in helping me resolving it.

I tested the same and found that-

  1. "def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);//customfield id" is returning correct value and getting properly assigned to customField. So, index "i" value is coming ok from FOR loop.
  2. "issue.getCustomFieldValue(customField);" is also calculating correct value.

So the main statements are working fine. Problem is the assignation statement, "fieldvalue[i] = "customFieldValue";". Here, null is getting stored. I tried different ways to assign, however, "null" is the result.

Vasiliy Zverev
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.
March 17, 2016

Sorry, it is hard for me to see this code, I just modified it slightly.

Idea - you created an String array, by try to store an Object value. Here is my version:

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;
def fieldid = new long[5] //array storing the customfield id
def fieldvalue = new String[5] //array to store field values corresponding to each custom field for a given ticket
fieldid[0] = 10513L
fieldid[1] = 12906L
fieldid[2] = 12911L
fieldid[3] = 12907L
fieldid[4] = 12908L
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TIK-3039");
for (i = 0; i &lt;5; i++) {
    def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(fieldid[i]);//customfield id
    if(customField != null) {
        fieldvalue[i] = (String) issue.getCustomFieldValue(customField);
    }
}

return fieldvalue.toString()
Like Alexander likes this
Nishant Kansal March 18, 2016

It worked...many-many thanks Vasiliy:) 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events