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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,466,911
Community Members
 
Community Events
176
Community Groups

The same value from customfield in loop

Edited

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

1 vote

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"

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

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 ?

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.

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

@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 :)

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

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

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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events