Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How can I throw a error message if user has selected an option on a checkbox Edited

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 :)

2 answers

Hi Imran, 

I'm not sure what do you mean by value Y must be filled in. 

  • Are you trying to block the transition when "D4D" is checked in the Checkbox field but "D4D PID" isn't checked?
  • Or block the transition when "D4D" is checked but "D4D PID" value is not entered into a text field?

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!

Hi Jia, 

 

Thank you for your response, 

 

Perhaps I did not explain it properly the first time,

 

Screenshot 2021-02-06 232140.png

 

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. 

 

Screenshot 2021-02-06 232327.png

So if they select D4D but leave D4D PID empty than an error message should be generated basically. 

So yes if they have selected D4D but not entered in a D4D PID number then the transition should be blocked

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

Hi Imran, 

Thank you for the detailed explanation. Glad that it helps! =)

Kind regards, 

Jia Jie

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 :/

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
}

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 :)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

Announcing Team Calendars in Confluence Data Center

Hi Community! We're thrilled to share that Team Calendars for Confluence is now a built-in feature for Confluence Data Center releases 7.11 and beyond.  A long time favorite,  Team Cale...

125 views 0 3
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you