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

Updating Fields dynamically in Jira Service Desk Portal with Scriptrunner

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

1 vote
Answer accepted

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! 

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

Hi @Mark Markov

Thanks, but do you have any alternate suggestions

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")
}

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

 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

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.

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

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.

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

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

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

51 views 0 8
Read article

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