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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's a custom scripted validator! When the issue is created from a popup window the script doesn't execute.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Olivera Tancheva
As @Nic Brough -Adaptavist- has already shared, the multi-select list will return a list of options. Thus, you might need to traverse the options in the list.
Here's a working sample
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you give me a hint with the code? As a following of my code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
if(cFieldValue) {
List<Option> options = (List<Option>)cFieldValue
if(options && options.first().getValue() == "Business Trip"){
UserMessageUtil.error('Please select project.')
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think your message crossed with Tarun's? His import line is what I would have typed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
yes. I imported the specified Option field, it jsut gives an error that the casting to list cannot happen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How would this affect the code? The casting to list does not apply anymore. How to retrieve the project value from the customfield?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It would be something like this?
ProjectImpl project = (ProjectImpl) cFieldValue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.