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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,555,974
Community Members
 
Community Events
184
Community Groups

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

Jia Jie
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Feb 10, 2021

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

0 votes
Jia Jie
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Feb 06, 2021

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!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events