Script Runner > Post Function > Create Issue > Custom Field Value

Samuel Estrada January 14, 2013

Hello,

I am using the Script Runner plugin to create a self-computed value in a field.

This field is an ID then I have to catch later.

I use Groovy to calculate the value of the field.

This ID consists of:

  • Country (this field is read from a external database, I use the plugin "database field")
  • issue.id
  • Year (calculated java.util.Calendar)

Once the value generated not want to be changed (I use it as original ID)

  1. Create a Script Field.

    The problem of auto-computed field is not stored in the DB and the value is auto-calculated whenever you save the issue, so I can not do run "getCustomFieldValue ()".
  2. Post-Function

    In the issue of creating transition.

    • When attached the script before function "make an issue of origin"> I have no value issue.id> I have not the value of the custom field "Country"> I can save my custom field with setCustomFieldValue()
    • After function "make an issue of origin"> I have the issue.id> I have not the value of the custom field "Country"> not allow me to save the value of the custom field with setCustomFieldValue()

The questions are:

  • How would you do it?

  • Why I can not read the Custom Field Value Field Country? Is not saved in this step of transition?
  • How I can save the issue after the function "make an issue of origin"?


The code:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.MutableIssue
import static java.util.Calendar.*
import org.apache.log4j.Category

log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

MutableIssue myIssue = issue

CustomFieldManager cfm = ComponentManager.getCustomFieldManager()
CustomField cfAutoID = cfm.getCustomFieldObject("customfield_001")
CustomField cfCountry = cfm.getCustomFieldObject("customfield_002")

def autoID = myIssue.getCustomFieldValue(cfAutoID)
if(!autoID) {
  def country = myIssue.getCustomFieldValue(cfCountry)[0,1]
  int year = Calendar.getInstance().get(Calendar.YEAR)

  if(country) {
    myIssue.setCustomFieldValue(cfAutoID, country.toUpperCase() + "/" + myIssue.id + "/" + year)
  }
}

in JIRA 5.2.4

Thanks!

1 answer

1 accepted

1 vote
Answer accepted
JamieA
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.
January 14, 2013

The value of a calculated custom field is not in the database but you can still use getCustomFieldValue(). I don't really understand that part of the question.

You could be able to access custom field values before the issue is "stored"... what is the concrete class for myIssue when you script runs?

ie: log.warn myIssue.class.name

If using setCustomFieldValue, add myIssue.store at the end. But this is a post-function?

The question is confusing because you're talking about a scripted field, AND a post-function in the same question. Can we break these out into two questions?

EDIT: If you want to do it in a post-function, AND you need the issue ID, put your post-function LAST.

Then update your custom field using this code:

def changeHolder = new DefaultIssueChangeHolder();
targetField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(targetField), "my new value"), changeHolder);

Where "my new value" is subsitute your calculated value based on the issue ID and other custom field values. Sample that I used for testing: https://gist.github.com/4542341

Samuel Estrada January 14, 2013

sorry if it is a bit confusing.

I mean I've tried two different ways.

  1. I first tried to do it with a Script Field, but the problem I have in this case is that I always re-calculated the field. I just need to calculate it once and does not change the value once saved.
  2. the second time I did it with a "Post-Function" in the transition from the creation of the issue. in this case I fail to get the value of a custom field because I think even not saved any value.
Jacques
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.
January 14, 2013

Hi Jamie,

I'm working together with Samuel on this issue and the issue we encounter is the following:

At the same time an issue is created we are looking to create a unique reference to the issue that doesn't change even if you move it to another project. We're trying to use a script that gets the internal database ID of the issue and combine it with other variables (as shown in original question) and save it in a custom text-field.

We are finding that in the create post-event the script is not able to save the calculated value to a custom field.

The script does the "calculation" but somewhere in the creation process the value is lost.

We have tried to put the script before the Creates the issue originally post function in JIRA, but then obviously the issue doesn't have any issue.id.

We have tried to put the script after this post-function but then the value doesn't get saved in our custom field (probably because the issue was already saved).

Any suggestion on how to work around this catch-22?

thanks!
Jacques.

JamieA
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.
January 14, 2013

Hi there...

For the unique identifier can't you just use a scripted field containing just "issue.id" - the id value of an issue doesn't change.

I'll give you an example of doing it with a CF or a post-function, although people normally pay for this level of support!

JamieA
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.
January 14, 2013

ok... I updated my answer with sample code.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events