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

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.

3 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Olivera Tancheva January 17, 2019

It doesn't work without the casting part. So I did something like this:

def cFieldValue = issue.getCustomFieldValue(cField);

if(cFieldValue) {

ProjectImpl project = (ProjectImpl)cFieldValue

if(project && project.getName() == "Business Trips"){

 throw new InvalidInputException("Please select a corresponding Project")

}
}

and it works now! But the scirpt sometimes executes and sometimes doesn't!

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.
January 17, 2019

What is the trigger for the script?  How is it being called?

Like Olivera Tancheva likes this
Olivera Tancheva January 17, 2019

It's a custom scripted validator! When the issue is created from a popup window the script doesn't execute.

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.
January 17, 2019

Which "popup window" is this, and where is the validator placed in the workflow exactly?  (Which transition, and where does that transition start and end?)

Olivera Tancheva January 17, 2019

Its the "create issue" transition.

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.
January 20, 2019

Ok, the script will always execute when it's on a validator, so you need to go through some debugging - put messages into the code to see what the variables it is working with are.

Like Tarun Sapra likes this
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 21, 2019

Hello @Olivera Tancheva

Have you  imported all the classes like "InvalidInputException"

Can you paste the screenshot of the complete validator here. And as Nic said, also add some logging.

Olivera Tancheva January 21, 2019

Here is the whole validator:

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.atlassian.jira.project.ProjectImpl
import com.opensymphony.workflow.InvalidInputException


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

ProjectImpl project = (ProjectImpl)cFieldValue

if(project && project.getName() == "Business Trips"){

throw new InvalidInputException("Please select a corresponding Project")

}

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019
Olivera Tancheva January 16, 2019

Can you give me a hint with the code? As a following of my code?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019
if(cFieldValue) {

List<Option> options = (List<Option>)
cFieldValue

if(options && options.first().getValue() == "Business Trip"){

  UserMessageUtil.error('Please select project.')

}
}

Olivera Tancheva January 16, 2019

It gives me an error "Unable to resolve class Option"?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019

Hello @Olivera Tancheva

You have to import

import com.atlassian.jira.issue.customfields.option.Option;

I would suggest you to take some basic programming classes as that would help you in implementing real life scenario using SR plugins and getting basic understanding of Java and Groovy 

Olivera Tancheva January 16, 2019

Not sure what should I import to resolve this error

Olivera Tancheva January 16, 2019

Thanks for your advice. but it still gives an error

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Project: BT' with class 'com.atlassian.jira.project.ProjectImpl' to class 'java.util.List
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.
January 16, 2019

I think your message crossed with Tarun's?  His import line is what I would have typed.

Olivera Tancheva January 16, 2019

yes. I imported the specified Option field, it jsut gives an error that the casting to list cannot happen.

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.
January 16, 2019

Oh, hang on, you said "multi-select" field and I went with it.  But it looks like it is not a multi-select, but actually a project field!

Could you confirm the exact custom field type in the list of custom fields under administration -> issues -> customfields ?

Olivera Tancheva January 16, 2019

It says "Project Picker (single project)"

Olivera Tancheva January 16, 2019

How would this affect the code? The casting to list does not apply anymore. How to retrieve the project value from the customfield?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019

Hello @Olivera Tancheva

Here's the API definition of the object's class

https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/project/ProjectImpl.html

Just use the method getName on the "cFieldValue" object after casting it to ProjectImpl.

Olivera Tancheva January 16, 2019

As I am still weak at my coding abilities, I have the idea in my head, but could you help me with the casting part to ProjectImpl?

Olivera Tancheva January 16, 2019

It would be something like this?

ProjectImpl project = (ProjectImpl) cFieldValue

Olivera Tancheva January 16, 2019

I managed to work the code, but instead of an error message i want the creation of the issue to be disabled with an error message. Do you have any guide for this?

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.
January 16, 2019

Actually, not quite - the .getCustomFieldValue will return a Project object directly (at least my slightly old code didn't need to cast it).

try issue.getCustomFieldValue(cf).getName() directly - my code is old, but I don't think it's changed!

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019

@Nic Brough -Adaptavist- is correct, you don't need to explicitly cast the object. Though I personally always prefer to cast to the right object in order to have good readability of the code and since groovy is dynamic language thus not casting objects leads to complains from the IDE.

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.
January 16, 2019

Fields contain different types of data, depending on what they hold, so you need to take account of that when using "getCustomFieldValue(field)".  Number fields give you a number, as do estimates.  Date and date/time fields give you a date stamp object.  Select lists give you "option" objects back, and so-on.

You say it's a multi-select field.  These contain options, but because they can contain many, they will return a list or array type thing containing 0, 1 or many options.

You will need to iterate over the list you get back and look for "Business Trips" in the set of elements.

TAGS
AUG Leaders

Atlassian Community Events