update issue field with issue key id

Hemant S February 20, 2017

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
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.
February 20, 2017

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?

Hemant S February 21, 2017

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

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

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.
February 21, 2017

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

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        , issue
        , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build())
Hemant S February 22, 2017

works as exepected with the 

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        , issue
        , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build())

 

 

Thanks

0 votes
Stefan Arnold
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.
February 20, 2017

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")
log.setLevel(org.apache.log4j.Level.DEBUG)
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")
liefList.find{
        if (it == lieferant){
                log.debug(it + " gefunden in " + String.valueOf(issue));
                issueInputParameters.addCustomFieldValue(customFreigabe.id, optionFalse.getOptionId().toString())
                return true;
        }
        log.debug("Lieferant: " + lieferant + " nicht in Liste")
        issueInputParameters.addCustomFieldValue(customFreigabe.id, optionTrue.getOptionId().toString())
        return false;
}
log.debug(issueInputParameters.getCustomFieldValue(customFreigabe.getId()).toString())
UpdateValidationResult update = issueService.validateUpdate(user, issue.id, issueInputParameters)
log.debug(user.toString())
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.

Hemant S February 21, 2017

Ok. I fixed it with ... 

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
        ,issue
        , 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(GenericTextCFType.java:51)
	at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:138)
	at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFieldImpl.java:731)
	at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:448)
	at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:434)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655)
	at com.atlassian.jira.issue.IssueManager$updateIssue.call(Unknown Source)
	at Script240.run(Script240.groovy:23)
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.
February 21, 2017

Somewhere you are using something

"$variable"

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

Hemant S February 22, 2017

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

Suggest an answer

Log in or Sign up to answer