Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

The same value from customfield in loop

Dawid Skowron December 1, 2021

Hi,

Regardless of "thisissue" value changing I get the same customfield CF2 value every time. This is the value for the first issue found.
What am I doing wrong?


import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.IssueImpl
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.issue.MutableIssue

def jql = "key in ( MP-820, MP-874, MP-871)"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def searchService = ComponentAccessor.getOSGiComponentInstanceOfType(SearchService)
def queryParser = ComponentAccessor.getOSGiComponentInstanceOfType(JqlQueryParser)
def query = queryParser.parseQuery(jql)
SearchResults search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
def products = search.results
log.error(products)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManafer = ComponentAccessor.getIssueManager()
def CF = customFieldManager.getCustomFieldObject('customfield_13004')
def CF2 = customFieldManager.getCustomFieldObject('customfield_11908')
def i =0;
def ProductStatus = "Probably error"

Issue thisissue
def status
products.any {
//def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)

thisissue = it

ProductStatus = "Probably error"

// def test =""
// log.error(thisissue.status)

status = ""
log.error(thisissue)
status = thisissue.getCustomFieldValue(CF2)
log.error(status)


if(!status.isEmpty())
{
if(status.status.name.contains("Canceled") || status.status.name.contains("Done"))
{
//
ProductStatus = "Yes"
}
else
{
ProductStatus = "No"
}
}
else
{
ProductStatus = "No IT Products"
}
i++
log.error(i.toString())
// issueIndexManager.reIndex(it)
//CF.updateValue(null, thisissue, new ModifiedValue(thisissue.getCustomFieldValue(CF), ProductStatus), new DefaultIssueChangeHolder())

}

LOGS:

2021-12-02 09:13:24,919 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=MP-874], DocumentIssueImpl[issueKey=MP-871], DocumentIssueImpl[issueKey=MP-820]]

2021-12-02 09:13:24,923 ERROR [runner.ScriptBindingsManager]: MP-874

****************************Wrong:**********************************
2021-12-02 09:13:24,947 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]] 
**********************************************************************

2021-12-02 09:13:24,947 ERROR [runner.ScriptBindingsManager]: 1

2021-12-02 09:13:24,949 ERROR [runner.ScriptBindingsManager]: MP-871

****************************Wrong:**********************************
2021-12-02 09:13:24,966 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]]
**********************************************************************

2021-12-02 09:13:24,966 ERROR [runner.ScriptBindingsManager]: 2

2021-12-02 09:13:24,968 ERROR [runner.ScriptBindingsManager]: MP-820

****************************Wrong:**********************************
2021-12-02 09:13:25,025 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]]
**********************************************************************

2021-12-02 09:13:25,026 ERROR [runner.ScriptBindingsManager]: 3

 

4 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 1, 2021

After accessing an issue from a search results, I prefer to grab a new copy of the issue. The issue object in the results is not complete and not suitable for all typical operations:

Try:

thisissue = issueManager.getIssueObject(it.id)

But then, I'm not clear how you expect to get a "status.status.name" from a custom field...

Custom fields contain either String text of Option object.

You can get the issue's status with "thisissue.status.name"

Or you can get the selected option's label with "thisissue.getCustomFieldValue(CF2).value"

Dawid Skowron December 1, 2021

But the problem is neither "it" nor "thisissue" with him. The problem is with
status = thisissue.getCustomFieldValue(CF2)
Which for every subsequent issue returns the same value anyway
As if the .getCustomFieldValue(CF2) function doesn't respond to changing the issue

1 vote
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 1, 2021

Hi @Dawid Skowron , it is weird... I didn't find any documentation to "any" function which would say that it stops when the first entry causes true result or anything like that. But the true is that I always use "each" function when traversing a collection of the objects.

Can you try to log the value of thisissue, please?

thisissue = it
log.error(thisissue)

or simply use each instead of  any ?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 1, 2021

I think this is the most relevant documentation that covers the any method: https://groovy-lang.org/groovy-dev-kit.html#_manipulating_lists

But you are correct, this stops the loop as soon a "truthy" element terminates the closure. each or collect may be more appropriate in this case.

Dawid Skowron December 1, 2021

thisissue returns me the correct issue value in the loop problem is with thisissue.getCustomFieldValue(CF2).value

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 2, 2021

@Dawid Skowron can you try to reindex the MP project? If you updated the value in custom field by other script and didn't use proper method, it can be the cause of the problem... and please, use threads on the community correctly. Now we have 2 real answers here, but another 2 "answers" are yours :)

0 votes
Dawid Skowron December 1, 2021

log.error(thisissue.toString())
log.error(status)

*********************************LOGS*********************************************
2021-12-02 08:35:45,389 ERROR [runner.ScriptBindingsManager]: DocumentIssueImpl[issueKey=MP-874]

2021-12-02 08:35:45,391 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]]

2021-12-02 08:35:45,391 ERROR [runner.ScriptBindingsManager]: 1

**********************************************************************************

2021-12-02 08:35:45,416 ERROR [runner.ScriptBindingsManager]: DocumentIssueImpl[issueKey=MP-871]

2021-12-02 08:35:45,417 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]]

2021-12-02 08:35:45,418 ERROR [runner.ScriptBindingsManager]: 2

**********************************************************************************

2021-12-02 08:35:45,441 ERROR [runner.ScriptBindingsManager]: DocumentIssueImpl[issueKey=MP-820]

2021-12-02 08:35:45,442 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]]

2021-12-02 08:35:45,442 ERROR [runner.ScriptBindingsManager]: 3

**********************************************************************************

0 votes
Dawid Skowron December 1, 2021

status = thisissue.getCustomFieldValue(CF2)
log.error(thisissue.toString())
log.error(status)
i++
log.error(i.toString())

2021-12-02 08:35:45,389 ERROR [runner.ScriptBindingsManager]: DocumentIssueImpl[issueKey=MP-874] 2021-12-02 08:35:45,391 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]] 2021-12-02 08:35:45,391 ERROR [runner.ScriptBindingsManager]: 1 2021-12-02 08:35:45,416 ERROR [runner.ScriptBindingsManager]: DocumentIssueImpl[issueKey=MP-871] 2021-12-02 08:35:45,417 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]] 2021-12-02 08:35:45,418 ERROR [runner.ScriptBindingsManager]: 2 2021-12-02 08:35:45,441 ERROR [runner.ScriptBindingsManager]: DocumentIssueImpl[issueKey=MP-820] 2021-12-02 08:35:45,442 ERROR [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=ECT-6214]] 2021-12-02 08:35:45,442 ERROR [runner.ScriptBindingsManager]: 3

TAGS
AUG Leaders

Atlassian Community Events