It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

update issue field with issue key id

We need to save the issue id in a custom field before we move the ticket over. So i added a script to save the issue id when a ticket gets created. Here is my script.


import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def custom_ticket_id = issue.getKey()
def cticketCf = customFieldManager.getCustomFieldObjectByName("Custom Ticket ID")
issue.setCustomFieldValue(cticketCf, "${custom_ticket_id}")

For some reason this does not populate the custom field. The script seems to run without errors. Is there something wrong with my script ?

2 answers

1 accepted

1 vote
Answer accepted

Your coe seems to be correct.

But when postfucntion is called? Is it aflter create issue one? Do you have "Save changes into database" postfucntion after it?

Postfunction is called after the create issue. The last post function in the list. 

How do i add "Save changes into database" postfunction ?

Edit workflow - select transition - open postfunction section - press "Add post function" button - select "Stores updates to an issue".

I also would reccomend to use this code to store changes since it is compatible with JIRA 7:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

issue.setCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Custom Ticket ID"), issue.getKey())

        , issue
        , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build())

works as exepected with the 

        , issue
        , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build())




Not sure i its really necessary but i think you have to use issueService to update the issue.

Iam using it in my scripts and everythink is working fine.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.issue.IssueInputParameters
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
IssueService issueService = ComponentAccessor.getIssueService()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
def optionsManager = ComponentAccessor.getOptionsManager()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
CustomField customLieferant = customFieldManager.getCustomFieldObject(10046L)
CustomField customFreigabe = customFieldManager.getCustomFieldObject(10028L)
//def issue = issueService.getIssue(user, "WAR-17")?.issue
String lieferant = (String) issue.getCustomFieldValue(customLieferant)
def cfConfig = customFreigabe.getRelevantConfig(issue)
def optionTrue = ComponentAccessor.optionsManager.getOptions(cfConfig).getOptionForValue("True", null)
def optionFalse = ComponentAccessor.optionsManager.getOptions(cfConfig).getOptionForValue("False", null)

def changeHolder = new DefaultIssueChangeHolder();

def liefList = ['Test1', 'Test2']
log.debug("checkDistributor -> " + String.valueOf(issue) + " : Starte Lieferantenvergleich")
        if (it == lieferant){
                log.debug(it + " gefunden in " + String.valueOf(issue));
                issueInputParameters.addCustomFieldValue(, optionFalse.getOptionId().toString())
                return true;
        log.debug("Lieferant: " + lieferant + " nicht in Liste")
        issueInputParameters.addCustomFieldValue(, optionTrue.getOptionId().toString())
        return false;
UpdateValidationResult update = issueService.validateUpdate(user,, issueInputParameters)
if (update.isValid()){
IssueResult updateResult = issueService.update(user, update)
    if (updateResult.isValid()){
           log.debug("Updating " + issue.getKey() + " succesful")
    } else {
                log.debug("Error in issueService.update of " + issue.getKey())
} else {
    log.debug("Error in issueService.validateUpdate of " + issue.getKey() + ": " + update.getErrorCollection())

In my case i change radiobutton field but concept should be the same. Hope this helps.

Ok. I fixed it with ... 

        , EventDispatchOption.ISSUE_UPDATED
        , false



But now i have an issue with the following error ..


2017-02-21 15:06:16,744 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: CM-419, actionId: 1, file: <inline script>
java.lang.ClassCastException: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String
	at com.atlassian.jira.issue.customfields.impl.GenericTextCFType.getDbValueFromObject(
	at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(
	at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(
	at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(
	at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(
	at com.atlassian.jira.issue.IssueManager$ Source)

Somewhere you are using something


(ie a GString). Just add a .toString() after the string.

Ah. I was converting it outside. using .toString with the declaration fixed it. thanks

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

1,945 views 8 3
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you