When and how initaliser function is executed in behaviours

Hi all,

I have some dobuts about initaliser function in behaviours. I've added a simple initialiser function with a log writer, something like that:

1 log.debug " --- behaviour is executed --- "

So them, I access to the view/create/edit view in an isse and I see that this log is executed two or three times in "catalina.out":

2017-09-19 14:20:18,142 http-nio-8050-exec-13 DEBUG admin 860x134977x1 15xt1nl, /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validators.json [c.o.j.groovy.user.FieldBehaviours] --- behaviour is executed ---
2017-09-19 14:20:18,167 http-nio-8050-exec-12 DEBUG admin 860x134978x1 15xt1nl, /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validators.json [c.o.j.groovy.user.FieldBehaviours] --- behaviour is executed ---
2017-09-19 14:20:18,190 http-nio-8050-exec-4 DEBUG admin 860x134979x1 15xt1nl, /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validators.json [c.o.j.groovy.user.FieldBehaviours] --- behaviour is executed ---

So my first question is, how many times is executed the initialiser function?

And, it is possible to restrict the initialiser function to execute only in the create or edit screen?

Thanks a lot!



3 answers

Hi Miguel,

Before answering your question I am adding related information. JIRA suggests "JIRA.Events.NEW_CONTENT_ADDED" for client side editing and html changes. This event triggers multiple time based on different things such pageLoad, pagePanelRefresh etc. According to Atlassian document the event triggers "whenever new HTML is inserted into the document". Please check the document for more information : https://developer.atlassian.com/jiradev/jira-platform/guides/fields/guide-inline-edit-for-jira-plugins . It seems to me for the same page the number of calls of the event varies as well. Behaviours uses "JIRA.Events.NEW_CONTENT_ADDED" to initialise itself. As a result the behaviour get called whenever the event triggers based on the "Reason" column value in the document .  

Here is your answers for the questions:

1. how many times is executed the initialiser function?

 Depends on the how many time the event triggers. I can say we have tried to placed check to minimise from our side as much as possible.


2. Is it possible to restrict the initialiser function to execute only in the create or edit screen?

Probably not. Because we have only one place to bind behaviours with JIRA and there are multiple "Reason" when the event triggers.

I hope it answer your question. Please feel free to correct me if you notice any gap or misunderstanding.



Hi Ash,

Continuing with this open question, If I understood you correctly, it's not possible to distinguish when "behaviour" is executed on a particular screen, isn't it?

But, is possible to prevent to execute "behaviour script" when a transition is done? I mean, I want to execute script only when the create or edit screen is shown.

Thanks a lot!




I have installed a new JIRA instance 7.5.0 version with Script Runner 5.1.6 version. I have added an Initialiser script of Behaviours and I 'm having the same problem.

The script is a single log writer, and the log line is printed multiple times, so it seems that the initialiser is executed multiple times: 4 times when I view an issue, another 4 times when I edit it, 2 to 4 times when visiting the create issue screen, and 4 to 10 times when updating the issue.



0 votes

Initializers run once when the form is loaded, so that's probably why you see it in the logs 3 times. Once for when the view screen is loaded, once for when the create screen is loaded, and once when the edit screen is loaded.

You can restrict a behaviour to a particular screen by doing something like this:

log.debug(getActionName()) // this will tell you the screen name

if (getActionName() == "Create Isssue") {
def fieldToSet = getFieldByName("TextFieldA")

Hi @Joshua Yamdogo [Adaptavist],

Thank you very much for your quickly response ;)

However I still have some doubts. Probably is my fault because I didn't describe the problem correctly.

I have update my simple script to this:

1 log.debug " --- behaviour is executed in : ${getActionName()} --- "

And when I click on "Create Issue" button I see this in the log:

2017-09-20 08:10:57,257 http-nio-8050-exec-5 DEBUG admin 490x140742x1 4hhad8, /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validatorsByPid.json [c.o.j.groovy.user.FieldBehaviours] --- behaviour is executed in : Crear --- 
2017-09-20 08:10:57,646 http-nio-8050-exec-20 DEBUG admin 490x140753x1 4hhad8, /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validatorsByPid.json [c.o.j.groovy.user.FieldBehaviours] --- behaviour is executed in : Crear ---

As you can see, this log is shown two times only in this screen (you can view that the time shown is very close 08:10:57,257 and 08:10:57,646).

And you can check that the action name is "Crear" (this JIRA instance is in Spanish). But when I click to show or edit an issue the action name is null. This action name, is the workflow name, not the screen name (create, edit or show), insn't it? It is there any method to get this action name  (create, edit or show)?

So now I have this two questions, related when the original one, why is shown two times the log in the same action? And how to distinguish if the issue is created, edited or shown?

Thanks in advance!



P.S.: Is getting the action name to null probably related to this (https://productsupport.adaptavist.com/browse/SRJIRA-85)?

Hi Miguel,

Thanks for the information. Apologies for misunderstanding your original question.

getActionName() works for the Create Issue screen, but doesn't work for the Edit screen. According to https://productsupport.adaptavist.com/browse/SRJIRA-85, the Edit screen returning null is not a bug but was rather done by design. I don't think getFieldScreen().name won't work either for the Edit screen.

Behaviours do not work on the Issue View screen. Behaviours only function on the Create Issue, Update/Edit Issue, Assign Issue, and Workflow Transition screens. If you are on a later version of ScriptRunner, attempting to inline edit a field on the View Screen that has a behaviour attached will simply open the Edit Issue screen.

So as far as I know, there isn't really a way to check in a behaviour if the issue is on the Edit screen. 

As for why the logging statements are appearing multiple times, that is a really good question. I might suggest that you open up a new case on our support portal so that we can investigate the issue further.

Thanks for using ScriptRunner! :)


Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

441 views 8 26
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