Hello everyone,
The idea is the following:
I import automatically a list of issues by email for a recruitment Jira board.
On those issues, the summary is something like: "New application: Network Engineer from First_Name Last_Name"
I want to run a script on very first transition (issue created) which transforms this summary:
- removes first 17 chars ("New application:")
- saves job title and populates a custom field with it
- new summary value should be "First_Name Last_Name"
I tried a lot to get the summary value as a string to be able to work on it but whatever I do, I cannot get that value at all.
My code now looks something like this:
// Define a JQL query to search for the issues on which you want to set the impediment flag
def query = "some_query"
// Search for the issues we want to update
def searchReq = get("/rest/api/2/search")
.queryString("jql", query)
.asObject(Map)
// Verify the search completed successfully
assert searchReq.status == 200
// Save the search results as a Map
Map searchResult = searchReq.body
// Iterate through the search results and set the summary and custom field value for each issue returned
searchResult.issues.each { Map issue ->
String sum = (issue.fields.summary as String)
if (sum.toLowerCase().contains('new application:')) {
// Trim and split the summary
sum = sum.substring(17)
String new_summary = sum.split('from ');
//Update the summary and the custom field
def result = put('/rest/api/2/issue/' + issue.key)
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields:[
issue.fields['10102'] : new_summary[0],
summary: new_summary[1]
]
]),
.asString();
// Log out the issues updated or which failed to update
if (result.status == 204) {
logger.info("The ${issue.key} issue was flagged as an Impediment. ")
} else {
logger.warn("Failed to set the summary on the ${issue.key} issue. ${result.status}: ${result.body}")
}
}
} // end of loop
return "Script Completed - Check the Logs tab for information on which issues were updated."
I get an error saying that the summary variable is not defined here:
String sum = (issue.fields.summary as String)
Can someone please help me?
Which position in the list of postfunctions on your transition is this script placed?
My guess is that you have it set before the "Update change history for an issue and store the issue in the database".
What I suspect is happening is that your script updates the issues in the background, but then the postfunction stores an older version of the issue object (without your fields populated) and overwrites those values. If you look in the history, you might see evidence of that.
Your script would be perfect for a script listener because it is compeltely stand-alone.
In a post function, it's better to use the "issueInputParameters" object to set the custom field values. Then, these will be picked up by the built-in functions to store and index the issue.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import java.util.Date.*
def customFieldManager = ComponentAccessor.customFieldManager
def outageStart = customFieldManager.getCustomFieldObject('customfield_12631')
def outageEnd = customFieldManager.getCustomFieldObject('customfield_12632')
def outageDuration = customFieldManager.getCustomFieldObject('customfield_13032')
if(issue.getCustomFieldValue(outageStart) && issue.getCustomFieldValue(outageEnd)) {
def dateValue = issue.getCustomFieldValue(outageStart) as Date
def dateValue2 = issue.getCustomFieldValue(outageEnd) as Date
issueInputParameters.addCustomFieldValue(outageDuration.id, ((dateValue2.getTime()-dateValue.getTime())/1000/60))
Or, keep your code exactly as is, but move it after the store and re-index post function tasks. This will cause additional db and indexing transaction and is generally less efficient, but will work in a pinch.
It is the second to last, so behind "Update change history for an issue and store the issue in the database".
I am going to try out your script, I am just getting some errors with the last line that I am trying to fix.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you leave it second to last, you will need a script more like what you had before and throw some debug messages to figure out why it wasn't working.
But if you try my version, you will need to move the function up.
And you can just add a toString() on that last line since the addCustomFieldvalue expects a string value
issueInputParameters.addCustomFieldValue(outageDuration.id, ((dateValue2.getTime()-dateValue.getTime())/1000/60).toString())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.