You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.