how to use simple scripted validator to validate the issue type and a value in a custom field

My company is evaluating either to use Script Runner or not. One of our requirement is to only allow a certain issue created when Task, New Feature, or Improvement issue types is selected and a value (Capital) of one of the our custom field. Also, if the user selects other values of the custom field would allow to create the issue regardless of the issue type.

Scenario:

  • If the user selects a bug issue type and selects the custom field with a value of capital, an error would occur and say that the capital value can only be used for "Task, New Feature, and Improvement issue types"
  • The rule we would like to have is that only "Task, New Feature, and Improvement" issue types is available when selecting capital in the custom field
  • I don't want to hide the custom field depending on the issue type

If there are other options to do this, let me know.

Thanks 

3 answers

1 accepted

Hi Rex,

I can confirm that a Simple Scripted Validator provided by the Script Runner plugin could be placed on the create transition of the issue in order to solve your requirements outlined below.

The way the validator would work would be to check to see if the Issue type was Bug and if so it would check if the Capital value had not been selected in the drop down list and only allow the issue to be created if this was the case. If the Capital value was selected when the issue type was of Bug then it would show an error to the user as shown in the screenshots below otherwise it would allow the issue to be created.

I have attached the code below along with some screenshots of how this works and should be configured below.

Code:

if (issue.issueTypeObject.name == 'Bug' && cfValues['DemoSelectList']?.value == 'Capital'){
    return false
}else {
    return true
}

Config:

image2016-3-10 17:17:16.png

Result:

image2016-3-10 17:17:25.png

Thanks

Kristian

Thanks Kristian,

The code works.

Kristian,

Can this functionality be done in the update screen? I understand it will not be a validator. Can it be a behaviours or other functionality in Script Runner?

 

Thanks,

Rex

Hi Kristian, 

How should I set Customfield to some value in that script? 

Eg.

if (issue.issueTypeObject.name == 'Bug' && cfValues['DemoSelectList']?.value == 'Capital')
{ // Need to set Cf "Approving Group" to _JiraTeam 
}
Am I able set like this in Custom script post-function under Script Post function? 
Thanks in advance

Hi Teju,

This code is for a validator and you cannot set values in a validator.

You would need to use a post function to set the field using the Script Post function option you mentioned above.

You can find more information on post functions here.

If you need further assistance can you please raise a separate question.

Thanks

Kristian

Kristian,

image2016-6-6 15:36:38.png

I am researching/testing/upgrading to possible JIRA Software version 7.1.7. The screenshot above is showing. I tried to do as it suggested but I'm not able to get it right. As of right now, it still works.

Can you help me out on fixing this issue/warning error?

Thanks,

Rex

 

Hi Rex,

I have changed my validator above to be as per the code below and it works with JIRA 7.

if (issue.getIssueType().name == 'Bug' && cfValues['DemoSelectList']?.value == 'Capital'){
    return false
}else {
    return true
}

It looks as if you are not calling  the getIssueType() which is the correct method to use for JIRA 7. Also you are checking 2 issue types in the screenshot below and should look to have a seperate simple scripted validator for each issue type.

I hope this helps.

Kristian

Thanks Kristian, it works.

Rex

Conditioned validator from Update on Transition for JIRA uses regular expression based condition checking that can usually accommodate requirements like these.

Thanks,

We will consider this add-on.

Hi Rex,

Yes you would need to use Behaviours to implement similar functionality on the edit issue screen.

An example of how you could achieve this is shown below.

Code:

// Required Imports
import com.atlassian.jira.component.ComponentAccessor

// Get a pointer to my select list field
def selectList = getFieldByName("DemoSelectList")

// Get the Value of the Demo Select List Field
def selectListVal = selectList.getValue()

// If the Value is Capital throw and error
if(selectListVal == "Capital"){
    selectList.setError("The Capital Value can only be used for the Task, New Feature and Improvement Issue Types")
}

Mapping required to only fire for Bug Issue Type:

image2016-3-21 16:28:51.png

Config:

image2016-3-21 16:29:21.png

Result:

image2016-3-21 16:30:49.png

I hope this helps

Thanks

Kristian

Kristian,

It's working in the create screen, how do you add it to the edit screen.

Hi Rex,

Behaviours work on the create and edit screens by default but do not work when a field is inline edited.

Thanks

Kristian

Kristian,

The behaviour is able to work without the validator in the create screen.

Here is a scenario:

  1. Created an issue with task

image2016-3-22 7:29:39.png

2. Edit the issue first time with the issue type of bug:

image2016-3-22 7:31:16.png

3. Issue lets it changed (behaviour didn't work)

image2016-3-22 7:32:7.png

4. Edit the issue the second time: (it has the error, but I am not able to update to another issue type or even charge type)

image2016-3-22 7:33:32.png

image2016-3-22 7:35:13.png

 

What do you think?

 

Thanks,

Rex

 

 

Hi Rex,

You may want to look at checking the issue type using code similar to below and move this to the behaviour to be on the issue type field.

// Required Imports
import com.atlassian.jira.component.ComponentAccessor

// Get a pointer to my fields
def issueType = getFieldById("issuetype")
def selectList = getFieldByName("DemoSelectList")

// Get the Value of the Demo Select List Field
def selectListVal = selectList.getValue()

// If the issue type is bug and the Value is Capital throw and error
if(issueType.getValue() == "1") {
    if( selectListVal == "Capital")
        selectList.setError("The Capital Value can only be used for the Task, New Feature and Improvement Issue Types")
    }else {
    selectList.clearError() // clear the error 
}

 

 

Thanks

Kristian

Kristian,

Unfortunately, it's still not working.

Scenario:

  1. Bug issue type is created
    1. issue type: bug, charge type: indirect  
  2. In the edit screen,
    1. Charge type: Capital is selected and error displays and not able to update the screen - expected outcome
    2. Still in the edit screen:
      1. issue type: bug, charge type: indirect -  can't update and have error still
  3. Still in the edit screen:
    1. issue type: Improvement, charge type: indirect - can't update and have error still
  4.  Still in the edit screen:
    1. issue type: improvement, charge type: capital - able to update and no error
      1. Edit screen:
        1. issue type: bug, charge type: capital - able to update and no error
          1. go back to edit screen - can't update and have error

 

Scenario 2:

  1. create screen
    1. issue type: bug, charge type: capital - not able to create issue and with error - expected
      1. change charge type: indirect - still not able to create issue and with error still 

 

It's almost there, but it needs just a little more changes. Thanks for helping me.

Rex

 

Hi Rex,

Unfortunately when a field is changed it needs the fields need to be clicked in again for the change to be recognised and fired.

Unfortunately this is a limitation of the behaviours plugin and is something you will need to put up with if you follow the behaviours approach.

Thanks

Kristian

Thanks Kristian,

I will go back to simple scripted validator for now. I am thinking of adding more JavaScript code, but it will take more work and I'm not a JavaScript master. Let me know if you can think of other ways to use script runner to my companies requirement.

Rex

Sorry Kristian,

It was actually me in the last comment. My coworker login and I forgot to log her out.

Rex

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

26,286 views 2 7
Join discussion

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