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

Allow Bulk Update Existing Issues using CSV for users

Hi Guys,

Need your help, how to enable Bulk update existing issue using csv for users or suggest any other method like REST API or Scriprunner method which allow users to update existing issues every issue have different value. 

Thanks

 

1 answer

1 accepted

1 vote
Answer accepted

Hi @Vikrant Yadav,

For your requirement, you can try the sample working code below:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager

def field1 = customFieldManager.getCustomFieldObjectsByName('Field 1').first()
def field2 = customFieldManager.getCustomFieldObjectsByName('Field 2').first()

final searchQuery = "project = MOCK order by key asc"

/**
* Path to the CSV file
*/
def file = new File("/home/ram/Downloads/content.csv")

def list = [] as ArrayList<Map>

/**
* Extract the value from the CSV file and add it to a List of Maps
*/
file.eachLine {
def column = it.split(',')
def mapInput = [:] as Map
mapInput.put('Key', column[0])
mapInput.put('Field1', column[1])
mapInput.put('Field2', column[2])
list.add(mapInput)
}

def query = jqlQueryParser.parseQuery(searchQuery)
def searchResults = searchProvider.search(SearchQuery.create(query, loggedInUser), PagerFilter.unlimitedFilter)

searchResults.results.each { documentIssue ->
def key = documentIssue.document.fields.find { it.name() == "key" }.stringValue()
def issue = issueManager.getIssueObject(key)

/**
* Iterate the List of maps and pass the value into the Custom Fields.
*/
list.each {data ->
if(data.get('Key') == issue.key) {
field1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field1), data.get('Field1')), new DefaultIssueChangeHolder())
field2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field2), data.get('Field2')), new DefaultIssueChangeHolder())
}
}
}

Please note, the sample working code above is not 100% exact to your environment. Hence, you will need to modify it accordingly.

What the example code above does is that it reads through the csv file and stores it into a List of Maps.

Next, it checks for the issues in the Project via JQL and passes the values from the List into the Issues of the project according to the issue's key. 

Note: I have included the issue keys in the left column in the sample CSV file I used to filter the values from the List by the Issue key and pass it to the issues accordingly.

Below is a print screen of the configuration:-

console_config.png

 

Below is a print screen of the csv values I have tested with:-

sample_csv_values.png

Below is a print screen of the tickets with updated field values:-

image1.png

image2.png

image3.pngimage4.png

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

Hi @Ram Kumar Aravindakshan _Adaptavist_  Thanks a lot as always for sharing the solution. But only JIRA admin have permission to access Script Console.

I want to allow users to bulk update existing issue. CSV file and path user will provide but only JIRA admin can run on script console. 

Can we do one thing using script fragment put a button on issue screen like bulk update user only need to save file at path and click that button but i think that only works for fields which are added in script for any modifcation or minor change like adding they need to contact us for adding that field in script. 

 

Thanks.

Vikrant Yadav

Hi @Vikrant Yadav

Usually, you should have Global Permission when you want to do a bulk update in Jira.

Regarding the Script Fragment, you could try it, but you would also need to have a REST Endpoint setup that makes the bulk update.

There is an existing REST Endpoint example to make the bulk update in the Adaptavist library

Once you have the REST Endpoint setup, you can follow the steps provided in the Adaptavist documentation to set up a web item. 

Also, for the example provided in my previous comment, there is a more straightforward approach to trigger the bulk update using the console, i.e. without JQL. Below is the simplified code:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def project = projectManager.getProjectByCurrentKey('MOCK')

def issues = issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.id))

def field1 = customFieldManager.getCustomFieldObjectsByName('Field 1').first()
def field2 = customFieldManager.getCustomFieldObjectsByName('Field 2').first()

/**
* Path to the CSV file
*/
def file = new File("/home/ram/Downloads/content.csv")

def list = [] as ArrayList<Map>

/**
* Extract the value from the CSV file and add it to a List of Maps
*/
file.eachLine {
def column = it.split(',')
def mapInput = [:] as Map
mapInput.put('Key', column[0])
mapInput.put('Field1', column[1])
mapInput.put('Field2', column[2])
list.add(mapInput)
}

issues.sort().each { issue ->
/**
* Iterate the List of maps and pass the value into the Custom Fields.
*/
list.each {data ->
if(data.get('Key') == issue.key) {
field1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field1), data.get('Field1')), new DefaultIssueChangeHolder())
field2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field2), data.get('Field2')), new DefaultIssueChangeHolder())
}
}
}

 

I hope this helps to answer your question. :) 

 

Thank you and Kind Regards,

Ram

Like Vikrant Yadav likes this

Thanks a lot @Ram Kumar Aravindakshan _Adaptavist_  . First solution works for me using Script Console. 
Alternate solution i also provided to the users that use POSTMAN if possible for updating multiple issues in bulk. They only need to select data file (.csv) and run the Edit issue API. This will update all issues which are in csv file. This don't need any support from jira admin side.  

 

Thanks Again! Have a Great week ahead. 

Suggest an answer

Log in or Sign up to answer
TAGS

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