You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.
View groupJoin the community to find out what other Atlassian users are discussing, debating and creating.
Hello,
I try to bulk update the description and a custom field of a list of issues that I find with a JQL search.
here my code :
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.label.LabelManager;
import com.atlassian.jira.issue.fields.NavigableField
import com.atlassian.jira.issue.fields.FieldManager;
import com.atlassian.jira.issue.RendererManager;
def sourceFieldId1 = 11202;
def sourceField1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(sourceFieldId1)
String jql = 'key = TTD-1';
boolean doSearch = true;
//user to run the searches as
def username = "XXXXX";
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserUtil userUtil = ComponentAccessor.getUserUtil()
IssueManager issueManager = ComponentAccessor.getIssueManager()
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def user = userUtil.getUserObject(username)
//Creation of the issue list
ArrayList<Issue> issues = new ArrayList<>();
if(doSearch) {
SearchService.ParseResult parseResult = searchService.parseQuery(currentUser, jql)
if (parseResult.isValid()) {
def searchResult = searchService.search(currentUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues.addAll(searchResult.getResults());
}
}
else {
//Nothing for now
}
long start = System.currentTimeMillis();
//action
for(Issue issue : issues){
MutableIssue mi = (MutableIssue) issue;
def cFieldValue1 = sourceField1.getValue(issue)
def cFieldValue2 = issue.getDescription()
String label1 = cFieldValue1
String label2 = cFieldValue2
String label1C
String label2C
label1C = label1.replaceAll("<a>","_")
label2C = label2.replaceAll("<a>","_")
sourceField1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sourceField1), label1C), new DefaultIssueChangeHolder())
mi.setDescription("example")
}
def end = System.currentTimeMillis();
long genTime = end-start;
String outString = "Set Values for "+issues.size()+" issues.";
outString+=" in "+ genTime +"ms";
return outString;
I know it can be optimized but I first have a problem to solve...
If I don't use mutableIssue and that I try to update my issue with this method :
issue.description = "example"
I have the following error message :
"groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: description for class: com.atlassian.jira.issue.DocumentIssueImpl"
So I used the mutable issue as you can see in my code, but now I have this error message :
"org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'DocumentIssueImpl[issueKey=TTD-1]' with class 'com.atlassian.jira.issue.DocumentIssueImpl' to class 'com.atlassian.jira.issue.MutableIssue' at Script499.run(Script499.groovy:64)"
Do you have an Idea ?
Thanks in advance.
Best regards,
David.
You could try IssueManager to get the MutableIssue:
IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue mutableIssue = issueManager.getIssueObject(documentIssueImpl.id)
documentIssueImpl in your code would be "issue" from for (Issue issue : issues), so .getIssueObject(issue.id); I'd definitely change that because searchresults give DocumentIssueImpl class, at least to me on first look this looks a little confusing what class we're really looking at :)
Hello,
Thanks for your reply, it works :)
For information I also added lines to force reindex the ticket.
issueManager.updateIssue(currentUser, mi, EventDispatchOption.DO_NOT_DISPATCH,
false
)
Boolean wasIndexing = ImportUtils.isIndexIssues()
IssueIndexingService indexing = (IssueIndexingService) ComponentAccessor.getComponent(IssueIndexingService.
class
)
indexing.reIndex(mi)
ImportUtils.setIndexIssues(wasIndexing)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.