Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,369,959
Community Members
 
Community Events
168
Community Groups

Use Scriptrunner cloud to update summary plus a custom field for a group of issues

Edited

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?

1 answer

1 accepted

1 vote
Answer accepted

I managed to fix it today, seems I was missing the fields definition:

def fields = issue.fields as Map

 

The updated code now looks like this and it works fine:

//Define the JQL query to be run
def query = "project = xx AND key = 'xxxxx' ORDER BY created DESC"

//Execute the query and save the issues as a Map
Map<String, Object> searchResult = get('/rest/api/2/search')
.queryString('jql', query)
.queryString('fields', 'summary')
.asObject(Map)
.body

def issues = (List<Map<String, Object>>) searchResult.issues

//Start the iteration through the issues
issues.each { Map issue ->

//get each issue fields and key
def fields = issue.fields as Map
def issueKey = issue.key

// Get the Custom field to get the option value from
def customField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Job Role'
} as Map

//Get the issue Summary value as string
def sum = fields.summary.toString()

//If condition for issues coming automatically from LinkedIn which were not processed
if (sum.contains("New application: ")) {

//Process the summary information, extract the Job Role and new summary value as First_Name Last_Name
sum = sum.substring(17)
def new_summary = sum.split(' from ');

//Execute the put operation to update the fields
def result = put("/rest/api/2/issue/${issueKey}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
summary: "${new_summary[1]}",
(customField.id):[value: "${new_summary[0]}"] as Map

]
])
.asString()
}

}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events