Forums

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

get value from selected customfield

Olivera Tancheva
January 16, 2019

I am trying to compare a value from a multiselect customfield. i did the following code:

import com.atlassian.jira.component.ComponentAccessor;
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.user.util.UserManager;
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.scriptrunner.runner.util.UserMessageUtil

def issueManager = ComponentAccessor.getIssueManager();
def userManager = ComponentAccessor.getUserManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cField = customFieldManager.getCustomFieldObject("customfield_12032");
def cFieldValue = issue.getCustomFieldValue(cField);

if (cFieldValue.toString() == "Business Trips")
{
    UserMessageUtil.error('Please select project.')
}

 

But the code only works when I put cFieldValue.toString() == "None". I can't seem to figure out why it doesn't work for the other options.

4 answers

Suggest an answer

Log in or Sign up to answer
1 vote
PD Sheehan
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 Champions.
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 Champion
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 ?

PD Sheehan
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 Champions.
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 Champion
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