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

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

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

works as exepected with the 

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

 

 

Thanks

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.

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)

Somewhere you are using something

"$variable"

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

Moving from Hipchat to Stride? Here’s what you’ll love

Heya, Hipchat friends! We’re so happy you’re checking out   Stride. Whether you know it or not, you have been instrumental in making Stride come to life. Every feature, design, and functionality...

56 views 2 6
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