ScriptRunner Behaviours trigger on 'Create" button

Jakub Cieplinski December 14, 2021

Hi,

I understand that scriptrunner behaviours trigger either on opening up the edit/create screen or editing the field that script is attached to.

I would like to know if it's possible to trigger the script when the "Create" button is pressed o it reevaluates itself at that time. 

 

Here is some context why I want to know this:

This is a snippet of a example script provided by scriptrunner

// Use this snippet if setting a value in one field always requires the user to enter a value in another field
// Change the 'Other Field Name' to the name of the field that should be required if this one has a value.

def currentField = getFieldById(getFieldChanged()) // field this behaviour script is defined on
def otherField = getFieldByName('Other Field Name')
def errorMessage = 'You must also populate this field!'

// set errors to ensure dependant fields are populated
if (currentField.value && !otherField.value) {
currentField.clearError()
otherField.setError(errorMessage)
} else if (!currentField.value && otherField.value) {
otherField.clearError()
currentField.setError(errorMessage)
} else {
otherField.clearError()
currentField.clearError()
}

 

Lets say we set the "currentField", then script is triggered and tells us that "otherField" is needed. We fill that field as well and click on "Create", however nothing happens because script wasn't triggered. User, after filling out the "otherField" has to go back and edit "currentField" AGAIN to trigger the script, despite the fact its already filled with data that he put there.

This is really not intuitive and user friendly. Can the script be somehow tied to "Create" button so it runs again after clicking it? If not in what other way can this issue be resolved?

1 answer

1 accepted

3 votes
Answer accepted
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

Hi @Jakub Cieplinski ,

based on your description it seems that you should a scripted validator on create transition and not a behaviour.

Behaviour works on create/edit popup screen and allows you to manage something in real time. Validator, instead, is something that works after pressing the create event. 

Here a link to setup your scripted validator https://scriptrunner.adaptavist.com/latest/jira/tutorials/scripted-validators-tutorial.html

Hope this helps,

Fabio

Jakub Cieplinski December 14, 2021

Yes @Fabio Racobaldo _Herzum_ that helps, thank you. 
2 Follow up questions tho:

1. Is there a documentation somwhere for this package com.opensymphony.workflow? I can't seem to find anything

2.  Can I somehow use behaviours api in validators/post functions scripts? I must say its more convenient to write 

def field = getFieldById("customfield_11111") 

rather than

 ComponentAccessor.customFieldManager.getCustomFieldObject(customfield_11111")

I tried including the following in the script but it doesn't seem to work

import com.onresolve.jira.groovy.user.FieldBehaviours
@BaseScript FieldBehaviours fieldBehaviours
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

Hi @Jakub Cieplinski ,

Validator is something that happens after click on create button so behaviour is no more available.

You should retrieve CustomField value through custom field manager and throw an exception if you check should do that :

CustomField myCF = ComponentAccessor.customFieldManager.getCustomFieldObject(customfield_11111");
CustomField mySecondCF = ComponentAccessor.customFieldManager.getCustomFieldObject(customfield_22222");
if(
(issue.getCustomFieldValue(myCF)!=null && issue.getCustomFieldValue(mySecondCF)!=null)
||
(issue.getCustomFieldValue(myCF)==null && issue.getCustomFieldValue(mySecondCF)==null)
){
throw new InvalidInputException("YOUR ERROR MESSAGE HERE");
}

Fabio

Jakub Cieplinski December 14, 2021

Ok, thank you. and do you know of any API reference site of this com.opensymphony.workflow package? I cannot find any and would be nice to know what InvalidInputException overloads are there and any other functions

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 14, 2021

Suggest an answer

Log in or Sign up to answer