Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

update issues returned with JQL

SaidAslan
Contributor
December 12, 2016

Hi all, 

I need to update custom field for issues, returned with jql in script post function. 

I've done the following: 

String jqlSearch = 'project = "SD" AND "Assignee" is EMPTY and IssueParameter in ("Is active")'; 

SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
						def userManager = ComponentAccessor.getUserManager()						
						def user = userManager.getUserByName("admin")

IssueManager issueManager = ComponentAccessor.getIssueManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager()
 
//checkboxes customfield
def issueParameter = customFieldManager.getCustomFieldObjectByName("issueParameter")
 
List<MutableIssue> issues = null;
 
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);
 
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
 
    issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}

This part works fine. The console returns me the list of issue id-s. 

But when I try to update them : 

issues.each() { jqlissue ->
        Issue myIssue = issueManager.getIssueObject(jqlissue.id);
        //def currentissueParameter = myIssue.getCustomFieldValue(issueParameter)
        def issueParameter_Collection = (Collection) myIssue.getCustomFieldValue(issueParameter)
        issueParameter_Collection.removeElement(issueParameter_Collection.getAt(0));
     
}

It returns  2016-12-12 14:57:07,627 WARN [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException

Seems that this is because of getCustomFieldValue() method. 

Could you please help with this? How can I update the field? 

Thanks 

2 answers

1 accepted

0 votes
Answer accepted
SaidAslan
Contributor
December 13, 2016

OK, I've done it: 

def issueParameter = customFieldManager.getCustomFieldObjectByName("IssueParameter")
def issueParameter_Collection = (Collection) issue.getCustomFieldValue(tgtField)
      issueParameter_Collection.removeElement(issueParameter_Collection.getAt(0));
		 issue.setCustomFieldValue(tgtField,issueParameter_Collection)
0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2016

It's not getting useful things from the issues returned.  I'm afraid an NPE is not really going to tell us much - you need to look at which line it occurs on and why.  Have a look at the logs to see the full error

SaidAslan
Contributor
December 12, 2016

Hi Nic, 

I've found here the similar code and tried to test updating summary or description fields: 

def query = jqlQueryParser.parseQuery("project = SD AND Assignee is EMPTY and IssueParameter in (\"Is active\")")
 
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())


results.getIssues().each {documentIssue ->
    MutableIssue issue = issueManager.getIssueObject(documentIssue.id)
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def issueParameter = customFieldManager.getCustomFieldObjects(issue).find {it.name == "issueParameter"}
    log.debug("-----------------------")
    log.debug(issue)
    log.debug("Current Value: " + issue.getCustomFieldValue(issueParameter))
    issue.summary = "TEST"
log.debug("-----------------------")
}

It fails on log.debug("Current Value: " + issue.getCustomFieldValue(tgtField)). 

---

2016-12-12 16:23:03,028 DEBUG [groovy.PostFunction]: -----------------------
2016-12-12 16:23:03,028 DEBUG [groovy.PostFunction]: SD-23602
2016-12-12 16:23:03,028 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-12-12 16:23:03,044 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SD-23608, actionId: 981, file: <inline script>
java.lang.NullPointerException

---


Then, 

log.debug("-----------------------")
    log.debug(issue)
    log.debug("Current Value: " + issue.summary)
    issue.summary = "TEST"
log.debug("-----------------------")
}

Returns no errors, but doesnt update the summary line. 

image2016-12-12 16:38:22.png

How can I get more informative log message? 

SaidAslan
Contributor
December 12, 2016

Moreover, I see that updating value (for example,  issue.setDescription("TEST") ) doesnt affect to the real issue. 

Can you please explain how it can be updated? As I understood, it is because of MutableIssue. 

Thank you

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2016

Ok, that makes sense.  The line it's failing on are probably being given an invalid object, almost certainly a null.

My guess is that this line is returning a null:

issueParameter = customFieldManager.getCustomFieldObjects(issue).find {it.name == "issueParameter"}

Do you really have a field called "issueParameter"?  And I'm not sure customfieldobjects even use .name as the label on the fields.

SaidAslan
Contributor
December 12, 2016

Omg, thank you for your question, cf is IssueParameter. It works! 

But I have another question: How can I save values after changing them in MutableIssue? When I do something like this : 

def issueParameter_Collection = (Collection) issue.getCustomFieldValue(tgtField)      issueParameter_Collection.removeElement(issueParameter_Collection.getAt(0));
    log.debug("New cf Value: " + issue.getCustomFieldValue(tgtField))

It returns me 

image2016-12-13 10:15:2.png

but actually issue is not updated and values are still the same. 

But if I use ChangeHolder, 

String currentFieldValue = issue.getCustomFieldValue(tgtField);
	String newFieldValue = null;
	tgtField.updateValue(null, issue, new ModifiedValue(currentFieldValue, newFieldValue), new DefaultIssueChangeHolder());

It clears me all values of checkbox cf in the issue, when I need to clear / set only one of them. 

Thank you. 


UPD:

 issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Helped me to update the Description, but still cannot change cf value without changeholder. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events