Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
Level
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

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
Community showcase
Published in Jira Service Management

ThinkTilt is joining the Atlassian Family!

This morning, Atlassian announced the acquisition of ThinkTilt , the maker of ProForma, a no-code/low code form builder with 700+ customers worldwide. ThinkTilt helps IT empower any team in their or...

354 views 19 20
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