• Community
  • Products
  • Jira
  • Questions
  • Script Runner Validation, I need to make sure the value of a select list custom field is not none before an issue is posted.

Script Runner Validation, I need to make sure the value of a select list custom field is not none before an issue is posted.

Script Runner Validation, I need to make sure the value of a select list custom field is not none before an issue is posted, it can be any other value. Here is some of the code I have tried in Script Runner Console. Am I going about this the right way? I'm using the Script Runner Examples.

import com.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Category
 
log = Category.getInstance("com.onresolve.jira.groovy.OneOrOther")
log.debug("In Validator groovy script")

if (cfValues['Defect Source'] == false) {
    invalidInputException = new InvalidInputException("Cannot have Defect Source equal to none!")
}
import org.apache.log4j.Category
import com.opensymphony.workflow.InvalidInputException

log = Category.getInstance("com.onresolve.jira.groovy.example.testValidator")
log.debug("Condition testValidator script running")

if (cfValues['Defect Source'] == false) {
    invalidInputException = new InvalidInputException("Cannot have Defect Source equal to none!")
}
import com.opensymphony.workflow.InvalidInputException

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
String sourceCfId = "Defect Source"
String targetCfId = "Defect Source"
CustomField cfSource = customFieldManager.getCustomFieldObject(sourceCfId)
CustomField cfTarget = customFieldManager.getCustomFieldObject(targetCfId)
if (issue.getCustomFieldValue(cfSource) == false) {
    invalidInputException = new InvalidInputException("Cannot have Defect Source equal to none!")
}

Block 1 error: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: cfValues for class: Script21

Block 2 error: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: cfValues for class: Script10

Block 3 error: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script22.groovy: 3: unable to resolve class CustomFieldManager @ line 3, column 20. CustomFieldManager customFieldManager = componentManager.getCustomFieldManager() ^ Script22.groovy: 6: unable to resolve class CustomField @ line 6, column 13. CustomField cfSource = customFieldManager.getCustomFieldObject(sourceCfId) ^ Script22.groovy: 7: unable to resolve class CustomField @ line 7, column 13. CustomField cfTarget = customFieldManager.getCustomFieldObject(targetCfId) ^ 3 errors

3 answers

1 vote

Jira doesn't have "combo boxes", so you're going to have to investigate how they've been implemented and why they don't return cfValues when asked for data (for block 1 and 2). For the third block, I'm less sure - the customFieldManager should work, which makes me suspect you are on a very different version of Jira from what the script was originally written for...

I'm sorry, I mean Select List, not combo box. Also, when using the built in condition test script I can put in cfValues['Defect Source'] and it works fine.

Thank you, Nic for answering.

Ah, ok, that puts me back on more familiar ground.

Your script doesn't really define cfValues - you need to extract it before you can use the data in it. For example

defcfValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Defect Source"))

I tried the following in Script Runner Console

import org.apache.log4j.Category
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

Issue myIssue = issue
def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def cfValue = myIssue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Defect Source"))

if (cfValue == false) {
    invalidInputException = new InvalidInputException("Cannot have Defect Source equal to none!")
}

I get the following exception: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issue for class: Script36

Of course - issue is not known in Script Console. To be able to test script also in console, I use following snippet (where XYZ-123 is your test issue).

Issue myIssue = binding.variables.get("issue") as MutableIssue ?: ComponentAccessor.issueManager.getIssueObject("XYZ-123")

Thanks this works without any issues and this seems very similar to the Built In Script Condition Tester. How do I make creating and modifying jira issues, bugs, and etc fail when a user does not enter a correct select field entry? I don't understand how to make it happen for all cases. I guess I'm not understanding the purpose of the build in test condition script for script runner.

import org.apache.log4j.Category
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

Issue myIssue = binding.variables.get("issue") as MutableIssue ?: ComponentAccessor.issueManager.getIssueObject("ABC-1")
def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def cfValue = myIssue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Defect Source"))

if (cfValue == false) {
    invalidInputException = new InvalidInputException("Cannot have Defect Source equal to none!")
}

cfValues are custom field values of actual issue in condition/validation/postfunction context (it is bind for you by Script runner), so you cannot test your validator in Script Console.

There is builtin script Condition Tester (should work also for validator - at least cfValues will be populated).

I've used the Condition Tester with the "has custom field value equal to" option and it works fine. I don't understand what it is doing though.

I essentially put cfValues['Defect Source'] in the coondition box and my preview issue key in the other. The boolean output displays correctly for my interests but it doesn't do anything.

Below code may work:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.issue.AbstractIssueEventListener

import com.atlassian.jira.event.issue.IssueEvent

import com.atlassian.jira.issue.Issue

import org.apache.log4j.Logger

import com.atlassian.jira.ComponentManager

import static org.apache.log4j.Level.DEBUG

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.user.util.UserManager

import com.atlassian.jira.security.groups.GroupManager

import com.atlassian.crowd.embedded.api.User

import com.atlassian.mail.Email

import com.atlassian.mail.server.MailServerManager

import com.atlassian.mail.server.SMTPMailServer

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.jira.bc.project.component.ProjectComponent

import com.atlassian.jira.util.ImportUtils

import com.atlassian.jira.user.util.DefaultUserManager

import com.atlassian.jira.project.Project

import org.apache.log4j.Category

import com.atlassian.jira.bc.project.component.ProjectComponent

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.type.EventDispatchOption

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.util.ImportUtils

import com.atlassian.jira.util.ErrorCollection

import com.atlassian.jira.ComponentManager

import com.atlassian.plugin.PluginAccessor

import org.apache.log4j.Level

import org.apache.log4j.Category

class componentupdate extends AbstractIssueEventListener

{

Category log = Category.getInstance(componentupdate.class)

@Override

void issueUpdated (IssueEvent event) {

log.setLevel(org.apache.log4j.Level.DEBUG)

ComponentManager componentManager = ComponentManager.getInstance()

def customFieldManager = componentManager.getCustomFieldManager()

MutableIssue issue = event.issue

String Agroup=issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Assignment Group"))

}

}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jun 14, 2018 in Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

504 views 3 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you