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

Rex Cooper March 4, 2016

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

2 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 10, 2016

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

Rex Cooper March 10, 2016

Thanks Kristian,

The code works.

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 10, 2016

Glad I was able to help.

Rex Cooper March 21, 2016

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

Teja April 5, 2016

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
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 5, 2016

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

Rex Cooper June 6, 2016

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

 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 6, 2016

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

Rex Cooper June 7, 2016

Thanks Kristian, it works.

Rex

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 21, 2016

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

Rex Cooper March 21, 2016

Kristian,

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

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 21, 2016

Hi Rex,

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

Thanks

Kristian

Rex Cooper March 22, 2016

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

 

 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 22, 2016

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

Rex Cooper March 22, 2016

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

 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 22, 2016

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

Sue Webber March 23, 2016

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

Rex Cooper March 23, 2016

Sorry Kristian,

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

Rex

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
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.
March 4, 2016

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

Rex Cooper March 7, 2016

Thanks,

We will consider this add-on.

Suggest an answer

Log in or Sign up to answer