Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Updating Fields dynamically in Jira Service Desk Portal with Scriptrunner

rakshith June 28, 2018

I have tried many of the examples for updating fields, but none of them seem to work.

I want to update any field based on another selected, in the portal form front end. Is this possible ? As the links below seem to mention how to do it, but it doesnt work practically

https://scriptrunner.adaptavist.com/latest/jira/behaviours-servicedesk.html

https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/select-list-other.html

 

---------

def desc = getFieldById("description")
def regressionIssueField = getFieldById(getFieldChanged())
if (regressionIssueField == "priority") {
desc.setFormValue("defaultValue")
}

---------------

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.priority.Priority

@BaseScript FieldBehaviours fieldBehaviours

if ((getFieldById(getFieldChanged()).value as Priority)?.name == "High") {
getFieldById("description")
.setLabel("Why do you need this and why so important?")
.setDescription("Please explain why this is Highest priority including details of outage etc.")
}
else {
getFieldById("description")
.setLabel("Why do you need this?")
.setDescription("Tell us why you want this.")

}

 

2 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Mark Markov
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.
June 28, 2018

Hello @rakshith

And welcome to the community.

I check both scripts. First one have an error (i think), second one works perfectly.

So, first thing to check is that your behaviour correctly mapped to service desk like this

Снимок экрана 2018-06-28 в 20.11.40.png

 

And second thing is that in your first script in your condition compared value and formfield object, it must be like this:

def desc = getFieldById("description")
def regressionIssueField = getFieldById(getFieldChanged())
if (regressionIssueField.value == "priority") {
desc.setFormValue("defaultValue")
}

 

Hope it helps! 

rakshith June 29, 2018

Hi Mark, Thanks for reply

I had set the mapping already.

Also regressionIssueField.value is the value, i.e. High, Medium, Low not actual priority

Do you have any other suggestions.

 

Is ScriptRunner actually able to dynamically update front end UI fields, or is ther any other tool

Like Michael Smith likes this
rakshith July 1, 2018

Hi @Mark Markov

Thanks, but do you have any alternate suggestions

Mark Markov
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.
July 2, 2018

Hi @rakshith

Sorry for a long reply. As i understand, you need to set description based on regressionIssueField value, am i right?

Yes, Scriptrunner able to update fields values dynamically.

Try this

def desc = getFieldById("description")
def regressionIssueField = getFieldById(getFieldChanged())
if (regressionIssueField.value == "High") {
desc.setFormValue("defaultValue for High")
}
if (regressionIssueField.value == "Low") {
desc.setFormValue("defaultValue for Low")
}
rakshith July 2, 2018

Hi @Mark Markov

Thanks but I had already tried this earlier, 

I have pasted code in Initialiser section under Behaviours. Attached Screenshot.

Behaviours_DescriptionChange.png

rakshith July 2, 2018

 Hi @Mark Markov
I can pre-populate the description or any other field infact, during page load.

def desc = getFieldById("description")
def defaultValue = "Test value"
desc.setFormValue(defaultValue)

But I am not able to dynamically change values once the page is already loaded. Is there any other event firing method available other than getFieldChanged(). As this doesnt seem to work. Im using Jira v7.5.0

Mark Markov
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.
July 3, 2018

Oh, that will not work. Initialiser function runs only once when form loads. It uses for setting form defaults only. If you want to change dynamically based on some field value, you need add that field in the section bellow initialiser (in your case it will be regressionIssueField), and place you code to that field.

rakshith July 3, 2018

Hey @Mark Markov

can you tell me where I need to add the script, the only other option I see is "Add server-side script" and that doesnt work.

Where is this location " you need add that field in the section bellow initialiser (in your case it will be regressionIssueField), and place you code to that field."

 

Can you post a screengrab or other detail of how I can use this

Mark Markov
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.
July 3, 2018

Ofcourse :

1. add your field

2. Add serverside script

image.png

Script that you attach to field will be run every time field value changes.

rakshith July 3, 2018

Thanks @Mark Markov but I had added in Server side script too, it didnt work.

Also I feel usually its client side script that we need to add, like javascript or other which renders front end changes.

 

The ScriptRunner documentation is not clear on this, can you pls advice if you know anything further

Mark Markov
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.
July 3, 2018

Actually behaviours javascript-based and renders frontend changes. But partly.

When form is loaded, behaviour catch this changes, runs code you wrote(on server side), make some magic, and returns final javascrpit thats performs on client side after form loaded.

Thats why "Add server-side script" misleading you. It's sctill client-side and javascript based

ref

https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html#_limitations

rakshith July 4, 2018

Hi @Mark Markov

Thank you for all your help, I was able to achieve what i was looking for.

Appreciate it :)

0 votes
Answer accepted
rakshith July 3, 2018

Hi @Mark Markov

I used different code to get field changed, and that seemed to work in the server side script

if ((getFieldById(getFieldChanged()).value as Priority)?.name == "High") {

 

ill keep you updated if it all works

TAGS
AUG Leaders

Atlassian Community Events