Forums

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

How to get current issue object in a class

Pavels Avens May 31, 2018

Currently, to get a custom field object or value, I use such approach:

MutableIssue issue = issue

Long someCustomFieldId = 10206L

def object(Long customFieldId) {
ComponentAccessor.customFieldManager.getCustomFieldObject(customFieldId)
}

def value(Long customFieldId) {
issue.getCustomFieldValue(object(customFieldId))
}

return "Field object: ${object(someCustomFieldId)}, field value: ${value(someCustomFieldId)}"

But instead of calling object(someCustomFieldId) and value(someCustomFieldId), I want to use someCustomFieldId.object and someCustomFieldId.value

For implementing, I have created a class

class Field {
Long customFieldId
final object
final value

Field(customFieldId) {
this.customFieldId = customFieldId
}

def getObject() {
ComponentAccessor.getCustomFieldManager().getCustomFieldObject(customFieldId)
}

def getValue() {
issue.getCustomFieldValue(getObject())
}
}

def someCustomField = new Field(11002L)

 

Calling someCustomField.object returns a custom field object, but calling someCustomField.value does not return any value.

An error occurred:

 

Message:
groovy.lang.MissingPropertyException: No such property: issue for class: Field
Possible solutions: value

 

How to get current issue object in a custom class? 

1 answer

0 votes
Nic Brough -Adaptavist-
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.
May 31, 2018

This usually depends on where you are trying to get a handle on the issue.  What is the context for the code you are running?

Pavels Avens May 31, 2018

I need to operate with an issue fields values and/or objects.

Previously I defined all necessary custom field ID in Long format and used something like this:

def customFieldManager = ComponentAccessor.customFieldManager


Long businessEmail = 12345L

def businessEmailObject = customFieldManager.getCustomFieldObject(businessEmail)

def businessEmailValue = issue.getCustomFieldValue(businessEmailObject)


if (businessEmailValue) {


  ...

}

else {
  issue.setCustomFieldValue(businessEmailObject, someValue)

}

 

It is ok, if I use 2-3 issue fields in my code. But in case of 10-20 fields it is not ok.

Therefore I want to use some simple and short approach for custom fields objects and values operations.

Nic Brough -Adaptavist-
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.
May 31, 2018

I am sorry, I was not clear.  What is the context the code will run in?  What does a user do that makes it run?

Pavels Avens May 31, 2018

The groovy code is used in a post-function. 

Nic Brough -Adaptavist-
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.
June 1, 2018

Ok, then

cf = customFieldManager.getCustomFieldBy<something-type>(<something-name>)

issue.getCustomFieldValue ( cf )

is the right approach.  You'll get an object back which is of the type of the stored data (string, option, date/time, number etc).  The <something-type> would be "by name" or "by ID", and you need to feed it the name or id of the field you want to get.

Suggest an answer

Log in or Sign up to answer