update issues returned with JQL

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

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 vote

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

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? 

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

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.

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

179 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you