So I need to create a Groovy script where if value X is selected from a checkbox field than a error message comes up saying value Y must be filled in. So essentially if value X is selected then value Y must be filled in, so I thought it would throw a input exception to notify the user however this has not worked and it just says that my condition has failed every time. Could someone give me some advice on how I can fix this script I will attach it bellow. I assume it is something very obvious I missing I think the logic makes sense.
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
if (cfValues['Roll-off user from']*.value.contains("D4D"))
{
throw new InvalidInputException("If you have selected D4D, please enter a D4D PID!!")
}
return true
At the moment the condition just fails and the transition is blocked. Just to reiterate again if value X is chosen from the checkbox field than a error message should come up alerting the user they need to fill in value Y.
Any help would be appreciated,
Thanks in advance :)
Hi Jia,
Thank you for your response,
Perhaps I did not explain it properly the first time,
So here is the checkbox, so if they select the option D4D then then an error message should be thrown to say D4D PID has to be filled in.
Hi there again,
I just tried your second solution and tweaked it slightly and it actually worked!!!
I cannot thank you enough :) believe it or not I was pulling my hair out because of this and could not understand why it would not work.
Kind regards,
IA
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Imran,
Thank you for the detailed explanation. Glad that it helps! =)
Kind regards,
Jia Jie
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi There,
I have no clue what has happened but the script was working and now its gone all wonky...
So here it is bellow:
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
def text = cfValues['D4D PID']
def checkbox = cfValues['Roll-off user from']*.value.contains("D4D")
//If "D4D" is checked but "D4D PID" is not filled in to the text field
if (checkbox && (text == null)) {
throw new InvalidInputException("If you have selected D4D, please enter a D4D PID!!")
}else{
return true
}
The issue is the user has not selected D4D then it stills says "Please enter a D4D PID!" even though they have not selected that. Not sure where I went wrong :/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Does the issue comes when not checking any values in the Checkbox field?
If yes, this is because we cannot invoke method contains() on null object.
cfValues['Checkbox']*.value.contains("D4D")
Therefore, the code above will give errors when Checkbox contains null value.
You can try somethings like below:
def checkboxValue = cfValues['Checkbox']*.value
def num = cfValues['D4D PID']
if(checkboxValue){ //To check if Checkbox field contains any values
if (cfValues['Checkbox']*.value.contains("D4D") && (!num)) {
throw new InvalidInputException("If you have selected D4D, please enter a D4D PID!!!!!")
}else{
return true
}
}else{
return true
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah so the issue comes when nothing has been checked in the checkbox field I will try the script that you have provided and see if that works.
Thank you again for your help :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Imran,
I'm not sure what do you mean by value Y must be filled in.
Your script is throwing an error message when "D4D" is checked in the Checkbox field. Therefore, you'll always block by the validation whenever "D4D" is checked. You should also put in condition to validate if "D4D PID" is not filled in or checked.
You can try the script below and see:
def checkbox = cfValues['Checkbox']*.value.contains("D4D")
def checkbox2 = cfValues['Checkbox']*.value.contains("D4D PID")
//If "D4D" is checked but "D4D PID" isn't checked
if (checkbox && !checkbox2) {
throw new InvalidInputException("If you have selected D4D, please enter a D4D PID!!")
}else{
return true
}
OR
def text = cfValues['Single Text']
//If "D4D" is checked but "D4D PID" is not filled in to the text field
if (checkbox && !(text == 'D4D PID')) {
throw new InvalidInputException("If you have selected D4D, please enter a D4D PID!!")
}else{
return true
}
Hope it helps!
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.