Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to prevent Scriptrunner Behaivours when "Create" button clicked on service desk screen.

Barış Bayramoğlu March 10, 2024

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.

1 answer

0 votes
Fabio Racobaldo _Herzum_
Community Champion
March 11, 2024

Hi @Barış Bayramoğlu ,

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

Barış Bayramoğlu March 11, 2024

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.

Fabio Racobaldo _Herzum_
Community Champion
March 11, 2024

Please could you attach your script code?

Barış Bayramoğlu March 11, 2024
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

 

@BaseScript FieldBehaviours fieldBehaviours



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
Fabio Racobaldo _Herzum_
Community Champion
March 12, 2024

Please could you try to put setFormValue method after setFieldOptions ?

Barış Bayramoğlu March 13, 2024

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

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

Fabio Racobaldo _Herzum_
Community Champion
March 13, 2024

@Barış Bayramoğlu please check if there is some postfuncion on create transition (on workflow level) that change that value.

Barış Bayramoğlu March 13, 2024

@Fabio Racobaldo _Herzum_ 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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events