New to the JIRA Behaviours Plugin and need assistance

I am attempting to use the JIRA Behaviours Plugin and could use assistance.

Problem Statement:

I am attempting to write a script that will compare the values of custom number fields. Each month I must gather inputs from managers on what project their headcount is working on. I need to validate their entries against what is rolled up by the Finance dept. I created a ticket on the Atlassian Support page and received a reply from Jaime Echlin with the following script:

FormField f = getFieldByName("U.S. Headcount")
def is8 = f.getValue() == 8
if (is8) {
  f.clearError()
}
else {
  f.setError("You must enter 8")
}

The problems with this code was the following:

1) The error message appears before an entry is even made. Ideally I would prefer the error to appear if the entry is wrong.

2) Once the correct entry is entered nothing occurs. Correct entry should allow user to create issue and or proceed through a workflow

3) Ideally instead of having to hard code the value to validate I would prefer for a check to be issued against the comparison field. Example Psuedocode:

Manager Field ID should equal Finance Field ID

If manager field ID value does not equal finance field ID value

stop issue from being created and/or proceeding through workflow and throw error message

If manager field ID value does equal Finance Field ID value

allow issue to be created and/or process through workflow

4) There a total of 6-8 manager fields that need to be compared to 6-8 finance fields.

In attempting to write this script have pursued the Sample.Groovy file in the plug-in itself to know avail. Any assistand would be highly appreciated. Thanks.

*Atlassian documenatation states to review the Sample.Groovy file to see how certain examples are created. I did not locate a Sample.Groovy file. Did find a ScriptSample.Groovy file? Is that where the examples are?

3 answers

1 accepted

Question answered above.

1 vote

So you've created a behaviour, you've added a field, and then a server-side validator. You can just paste the script in to the big text box rather than modifying Sample or ScriptSample. This is the easiest way to get started.

1) Change the code to

def is8 = !f.getValue || f.getValue() == 8

But experiment, and do a tutorial on groovy.

2) Have you added this script to the correct field? Add some logging to your script.

3) Repeat the code of getting a formfield then the value.

4) I covered that first. The samples are just samples. You can add to them if you want but you should probably create your own class. You need to create the file in the right place. You *will* need to read the docs. Both files are in the binary jar.

Also, please don't cross-post - I'm going to delete your wiki comment and the bug, otherwise I need to copy and paste my answer around, or it looks like I'm not answering questions.

Hello Jaime,

Thanks for the reply.

I'm still troubleshooting for items #1 and #2:

1) The error message appears before an entry is even made. Ideally I would prefer the error to appear if the entry is wrong.

2) Once the correct entry is entered nothing occurs. Correct entry should allow user to create issue and or proceed through a workflow

The bigger issue is #2. I could just code the error message to be blank on the initial load up of the screen to take care of issue #1 but the bigger problem is that when the correct value is entered the issue is not created/edited.

Looking at the comments by other users at: https://studio.plugins.atlassian.com/wiki/display/JBHV/JIRA+Behaviours+Plugin

I've found other's code that use:

formField.setValid(false) or formField.setValid(true)

others have initialized the number in the field with "Long"

I also tried this from a post on Feb 24, 2012 where I fully emulated the the JIRA admin set up and tired the code here and was still unable to create/edit the issue:

FormField numField= getFieldByName("number field")
Long numFieldval=numField.getFormValue() as Long
if(numFieldval>999){
numField.setError("Please input less then 1000.") // note: setError will add the red formatting for you.
}else{
numField.clearError()
}
the overall functionalaity of the plugin appears to work. I have created this code to test and it works fine:

FormField formField = getFieldByName("Test")

FormField descField = getFieldByName("Test 2")

FormField descField1 = getFieldByName("Taylorsville CW")

descField1.setFormValue("1")

formField.setFormValue("1")

descField.setFormValue("1")

but when I try to code a script using if else statements to do true validation the issue wont create/edit even when the correct value is entered from your initial post?

FormField f = getFieldByName("U.S. Headcount")
def is8 = f.getValue() == 8
if (is8) {
  f.clearError()
}
else {
  f.setError("You must enter 8")

}

Finally figured out:

FormField jmbgField = getFieldByName("Test")

Long jmbgLong=jmbgField.getFormValue() as Long

if (jmbgLong == 8 )

{

jmbgField.clearError()

jmbgField.setValid(true)

jmbgField.setHelpText("")

}

else {

jmbgField.setError("Test")

jmbgField.setValid(false)

jmbgField.setHelpText("<em><font color=\"red\">Test</font></em><br/>"

+ "")

}

Wanted to also say that Jaime is right in the sense that as a new developer I was not very experienced Groovy and was subsequently lost. Found some Groovy tutorial which helped and also the comments from other users at the JIRA behavioral link as helped: https://studio.plugins.atlassian.com/wiki/display/JBHV/JIRA+Behaviours+Plugin. In general this is an amazing plugin primarily because it is so customizable.

Hi Mate

I am having a similar problem and its mainly based on 1) - "1) The error message appears before an entry is even made. Ideally I would prefer the error to appear if the entry is wrong."

What did you do to get around it? My case is I want to check whether emty or not and rather than sending javascript alerts which I think are quite ugly and intrusive, to add a server side error.

However Im not finding that's so easy, anyhelp?

Yohan

You just do as I did, which is only run the validation if the field has a value. Let's say the value is assigned to a variable called value then:

if (value) {

// do validation

}

Hi Jamie

The validation I wanted to do was to see whether there is a vlue or now -i.e.- empty field. Found a way to do it via Field Configurations.

How can I using the server scrips get the 'Due Date' of the issue and set a custom field (again a date field) to be 7 days after the due date?

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 Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

219 views 2 11
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