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

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

3 answers

0 vote
Vasiliy Zverev Community Champion Mar 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

 

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

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)

 

 

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

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)

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

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

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 Community Champion Mar 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]);
}

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 Community Champion Mar 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()
Vasiliy Zverev Community Champion Mar 17, 2016

And you should to see custom field name

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 Community Champion Mar 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()

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

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.

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()
}

 

 

 

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,

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Service Desk

Wy are we still using email for Service Desk workflows?

...attest to the experience of an urgent approval that gets lost in the boss’s inbox and requires that special “Please Approve” email or text message. In an age where we have distributed teams...

110 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you