How to prevent Scriptrunner Behaivours when "Create" button clicked on service desk screen.
Asked by Bar?? Bayramo?lu:

I have a behaivour that changes the value of field_2 when field_1 is changed. But I want to allow the user to change the value of field_2. When the user clicks on the "Create" button, the behaivour is also triggered and the value of the field returns to default. How can I prevent this.  

I use "formField.setFormValue()" function.


Answers:
author: Fabio Racobaldo _Herzum_
body: Hi [@Bar?? Bayramo?lu](/t5/user/viewprofilepage/user-id/5441282) ,

Initializer code is something that happens on form load (first time), Field specific code is something that happen on field change.

Please let us know your behaviour configuration.

Fabio

comments: author: Bar?? Bayramo?lu
body: I have a category field (select list). I want to clear every field on the form when category field is changed. I can do this with using setFormValue() but when I click to "Create" button, setFormValue()s clear all fields again and my form is going empty. So I want to prevent this situation.

\*When the "Create" button is clicked, scriptrunner thinks that my category field has been updated and runs the script.


author: Fabio Racobaldo _Herzum_
body: Please could you attach your script code?


author: Bar?? Bayramo?lu
body: import com.atlassian.jira.component.ComponentAccessor  
import com.onresolve.jira.groovy.user.FieldBehaviours  
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

<br />

<br />

def cfManager = ComponentAccessor.customFieldManager  
def optionsManager = ComponentAccessor.optionsManager  
def changeHistoryManager = ComponentAccessor.changeHistoryManager

final singleSelectName = 'Asset Category'  
final selectName = 'Asset Type (Dynamic)'

def singleSelectField = cfManager.getCustomFieldObjectsByName(selectName)\[0\]  
def formSingleSelect = getFieldByName(singleSelectName)  
def singleSelectValue = formSingleSelect?.value  
def formSelect = getFieldByName(selectName)

if (!formSingleSelect \&\& !formSelect) {  
return  
}

def config = ComponentAccessor.fieldConfigSchemeManager.getRelevantConfig(issueContext, singleSelectField)  
def options = optionsManager.getOptions(config)  
def endUserAssets = \[  
"Notebook",  
"PC/Workstation",  
"Monitor",  
"Printer/Scanner",  
"Video Conference Devices",  
"Projectors",  
"Communication Devices",  
"Peripheral"  
\]  
def industrialDevices = \[  
"Mobile Computers",  
"Industrial Printers",  
"Barcode Scanners",  
"Industrial Tablets",  
"Industrial PC",  
"Industrial Accessories"  
\]  
def networkDevices = \[  
"Switch",  
"Router",  
"Wireless Access Point",  
"Wireless LAN Controller",  
"Camera and Digital Recording Systems",  
"Security Appliances",  
"Hub",  
"Modem",  
"IDS-IPS",  
"Voice Gateway",  
"VPN Gatewat",  
"WAN Optimization and Acceleerator",  
"Bridge",  
"Access Server",  
"Transreciever/Converter",  
"Other"  
\]  
def hosts = \[  
"Storage Devices",  
"Backup Devices",  
"Server"  
\]

if (!options) {  
return  
}

switch (singleSelectValue) {  
case 'End User Assets':  
formSelect.setFieldOptions(options.findAll { it.value in endUserAssets }.collectEntries {  
\[(it.optionId): it.value\]  
})  
formSelect.setFormValue(options.getOptionForValue(endUserAssets\[0\], null).getOptionId())  
break;  
case 'Industrial Devices':  
formSelect.setFieldOptions(options.findAll { it.value in industrialDevices }.collectEntries {  
\[(it.optionId): it.value\]  
})  
formSelect.setFormValue(options.getOptionForValue(industrialDevices\[0\], null).getOptionId())  
case 'Network Devices':  
formSelect.setFieldOptions(options.findAll { it.value in networkDevices }.collectEntries {  
\[(it.optionId): it.value\]  
})  
formSelect.setFormValue(options.getOptionForValue(networkDevices\[0\], null).getOptionId())  
break;  
case 'Hosts':  
formSelect.setFieldOptions(options.findAll { it.value in hosts }.collectEntries {  
\[(it.optionId): it.value\]  
})  
formSelect.setFormValue(options.getOptionForValue(hosts\[0\], null).getOptionId())  
break;  
default:  
formSelect.setFieldOptions(options);  
}

*** ** * ** ***

I dynamically change the options of formSelect but the initial value doesn't change. So I use setFormValue() to change the initial value for every change. But when I click the create button it changes to initial value again.  

Ex.  
Asset Category -\> End User Assets  
Asset Type -\> Monitor  

Click "Create", then issue saved as  
Asset Category -\> End User Assets  
Asset Type -\> Notebook


author: Fabio Racobaldo _Herzum_
body: Please could you try to put setFormValue method after setFieldOptions ?


author: Bar?? Bayramo?lu
body: First of all, sorry for the late reply, I tried your suggestion:

formSelect.setFieldOptions(options.findAll { it.value in endUserAssets }.collectEntries {  
\[(it.optionId): it.value\]  
}).setFormValue(options.getOptionForValue(endUserAssets\[0\], null).getOptionId())

But the problem remains.

I solved the problem somewhat indirectly, but I'm attaching screenshots of the actual problem. If you know anything about it, I'd be grateful if you could help

![Screenshot_1.png](https://community.atlassian.com/t5/image/serverpage/image-id/311773i568975E8B74BA08A/image-size/large?v=v2&px=999 "Screenshot_1.png")

As you can see, I select Monitor in customer portal.

But when I click the "create" button.

Asset type changes to "Notebook" as follows.

![Screenshot_2.png](https://community.atlassian.com/t5/image/serverpage/image-id/311775i518974CE24386867/image-size/large?v=v2&px=999 "Screenshot_2.png")


author: Fabio Racobaldo _Herzum_
body: [@Bar?? Bayramo?lu](/t5/user/viewprofilepage/user-id/5441282) please check if there is some postfuncion on create transition (on workflow level) that change that value.


author: Bar?? Bayramo?lu
body: [@Fabio Racobaldo _Herzum_](/t5/user/viewprofilepage/user-id/781528) No there is no postfunction on create transition that changes the "Asset Category" or "Asset Type"

I think the "create" button somehow triggers the behaviour script.


Tags: data-center

jira-service-management

jira-service-management-data-center