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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

get value from selected customfield

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

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!

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

Like Olivera Tancheva likes this

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

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?)

Its the "create issue" transition.

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 Jan 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.

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")

}

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

Tarun Sapra Community Leader Jan 16, 2019
if(cFieldValue) {

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

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

  UserMessageUtil.error('Please select project.')

}
}

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

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 

Not sure what should I import to resolve this error

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

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

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

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 ?

It says "Project Picker (single project)"

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

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.

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?

It would be something like this?

ProjectImpl project = (ProjectImpl) cFieldValue

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?

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 Jan 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

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

71 views 1 6
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you