I need some help in creating a Behaviour for Jira Service Desk. Basically, what I want to do is make a field required if the value of two other fields meet certain criteria.
Environment:
Fields:
Logic:
On creation, if Field1 = X and Field2 contains Y then set Field3 to required, else set Field3 to optional.
Hi Paul,
I think a scripted validator is the solution here. But before I can proceed can you please clarify what is the condition for your multi-select list field that will make Field3 required? A specific set of options selected? Or at least one specific option selected?
The problem I have with a validator is that they don't work with Jira Service Desk from the customer's portal.
Field3 just needs to have any one of the options selected.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That actually depends on how you write your validator.
In any case, if I understood your requirement correctly, then if BOTH Field1 AND Field2 have ANY value, then make Field3 mandatory.
Try this validator, it should work even with your customer portal:
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
def cfMgr = ComponentAccessor.getCustomFieldManager()
def cf1Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Field1"))
def cf2Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Field2"))
def cf3Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Field3"))
if (cf1Value != null && cf2Value != null && cf3Value == null){
throw new InvalidInputException(cfMgr.getCustomFieldObjectByName("Field3").getName(), "Field3 is required!")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I stand corrected. The validator does work in JSD, but it doesn't actually display the error message in the portal. So, it prevents the issue from being corrected, but it doesn't tell the customer why.
Here is the validator that I setup. It works exactly as expected within JSD, it just doesn't display the error message in the portal.
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def custom1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field1'))
def custom2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field2'))
def custom3 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field3'))
if (custom1.value.contains('X') && custom2.value.contains('Y') && (!custom3)) {
throw new InvalidInputException(customFieldManager.getCustomFieldObjectByName('Field3').getName(), 'Required')
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, Ivan. The code works, but it doesn't display the error message on the customer's portal.
Here is the code I used:
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def custom1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field1'))
def custom2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field2'))
def custom3 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field3'))
if (custom1.value.contains('X') && custom2.value.contains('Y') && (!custom3)) {
throw new InvalidInputException(customFieldManager.getCustomFieldObjectByName('Field3').getName(), 'Required')
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's odd. I've just tested this in my instance an error message pops just fine:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm on Windows so I can't say anything for Safari, but I've tested this successfully on the latest Chrome, MS Edge and Firefox 57.0 (64-bit). No problems.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I just upgraded my Chrome to the latest version and still not seeing the message. Just to confirm, the code sample I pasted above is working on your instance?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Not exactly,
I'm using a much simpler code to debug InvalidInputException specifically:
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("VIP")
if (issue.getCustomFieldValue(cf) == null){
throw new InvalidInputException(cf.getName(),"Field is required!")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For posterity, we found that JSD was not returning the field name, but the format "customfield_XXX". Here's the code we eventually got working in both JSD and on the portal:
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def custom1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('fieldA'))
def custom2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('fieldB'))
def custom3 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('fieldC'))
def custom4 = "customfield_" + customFieldManager.getCustomFieldObjectByName('fieldC').getIdAsLong().toString()
if (custom1.value.contains('X') && custom2.value.contains('Y') && custom3 == null) {
throw new InvalidInputException(custom4, 'Error Message')
}
Thanks for your help, @Ivan Tovbin.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello, thanks for your brilliant response!!
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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.