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,552,516
Community Members
 
Community Events
184
Community Groups

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!

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 17, 2019

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 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?)

Its the "create issue" transition.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 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.
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")

}

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

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.
Jan 16, 2019 • edited

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
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 16, 2019

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 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 ?

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?

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.
Jan 16, 2019 • edited

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?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 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.
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
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events