Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
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

Issue when converting DocumentIssueImpl in MutableIssue on Scriptrunner

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.

 

 

1 answer

1 accepted

0 votes
Answer accepted

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)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

274 views 8 7
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