Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Scriptrunner - Values written to a ticket via groovy script don't seem to persist.

Hello,

I have a strange issue with a script I wrote to take some data from somewhere, and add it to the summary and description field of a new issue on creation. (Note, the api URL I'm using in the example below is just for testing - just an easy API to get data back from)

 

I'm guessing my problem is with the "issue.store()" method. Maybe I'm missing a parameter, or maybe I shouldn't even be using this method (it shows as deprecated in IntelliJ IDE). I'm just not sure what the "right" way is to store the new summary and description.

 

What happens is very strange. If I create the new ticket while I'm on the "Open Issues" page for my project, it all appears to work. If I refresh, that new ticket shows up, but it has the junk data that I typed in manually to the summary field. If I click once on that new ticket to view all the details about it, the summary gets updated at that point to have the data that my script is placing in it.

If I refresh - it all happens again - starts off with initial summary data - click on it, then it gets the real data.

 

 

 

Here's the script - it's running based on a Script listener, that triggers on issue creation and issue update:

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

import com.atlassian.jira.ComponentManagerimport com.atlassian.jira.issue.Issue;

import com.atlassian.jira.issue.IssueManager;

import com.atlassian.jira.issue.MutableIssue;

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;


//variables

def heatTicketFieldId = "customfield_10404"

def apiUrl = "https://api.macvendors.com"

 

//set issue variableIssue issue = issue;


//Get heat ticket number from custom field
def issueManager = ComponentAccessor.getIssueManager()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def heatTicketField = customFieldManager.getCustomFieldObject(heatTicketFieldId)

def heatTicketNumber = issue.getCustomFieldValue(heatTicketField)

 


//if heat ticket field has a value, look it up, if not, return nothing.

if(heatTicketNumber == null){

return

}

else{

def completeUrl = apiUrl + "/" + heatTicketNumber

def connection = completeUrl.toURL().openConnection()

connection.setRequestMethod("GET")

connection.doOutput = false

connection.connect()

def ticketContent = connection.content.text


def newSummary = "HEAT Ticket:" + heatTicketNumber

def newDescription = "Ticket content: " + ticketContent

issue.setDescription(newDescription)

issue.setSummary(newSummary)

issue.store()


UserMessageUtil.success("Created JIRA ticket " + issue.key + " from HEAT ticket " + heatTicketNumber)}

 

 

 

1 answer

Hey Dan,

in all of our problems, where an issue would not show updated values we used the IssueManager function

  • updateIssue

    Issue updateIssue(ApplicationUser user,
                      MutableIssue issue,
                      EventDispatchOption eventDispatchOption,
                      boolean sendMail)
    This method will store the provided issue to the JIRA datastore.

    The issue will be saved and re-indexed unless EventDispatchOption.DO_NOT_DISPATCH is specified. This method performs no permission checks.

    This method should be used if you want to exert more control over what happens when JIRA updates an issue. This method will allow you to specify if an event is dispatched and if so which event is dispatched, see EventDispatchOption. This method also allows you to specify if email notifications should be send to notify users of the update.

    Parameters:
    user - who is performing the operation
    issue - the issue to update
    eventDispatchOption - specifies if an event should be sent and if so which should be sent.
    sendMail - if true mail notifications will be sent, otherwise mail notifications will be suppressed.
    Returns:
    the updated issue.
    Throws:
    IssueFieldsCharacterLimitExceededException - when issue fields exceed the jira character limit
    Since:
    v4.0

(Function Reference from https://docs.atlassian.com/software/jira/docs/api/7.8.0/com/atlassian/jira/issue/IssueManager.html)

Thanks for the info. I got it working in a similar way, but I used IssueService instead.

 

 

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
//Setting Issue Input Parameters
issueInputParameters.setDescription(newDescription)
issueInputParameters.setSummary(newSummary)

IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(currentUser, issue.getId(), issueInputParameters);

if(updateValidationResult.isValid()) {
IssueResult updateResult = issueService.update(currentUser, updateValidationResult);

}else{

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

78 views 1 6
Read article

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