Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner Behaviours trigger on 'Create" button

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

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

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

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

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

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you