Need to add customised label if a custom field has value

Naveen kumar December 4, 2024

 

I have created a scripted custom field and if the field is not empty i want Incident to be added as label in the ticket along with other labels and if its empty i want No_Incident to be added in the ticket. I have tried my best to create the script using behaviours but none of them works. can you helpme with the script please. I have script runner and dont have jira automation plugin.

script:

 

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

 

@BaseScript FieldBehaviours fieldBehaviours
def customFieldA = getFieldById("customfield_25202")  
def customFieldValue = customFieldA.getValue()

 

def labelsField = getFieldByName("Labels")
if (customFieldValue ) {
   
    labelsField.setFormValue(['Incident']) 
} else {
    
    labelsField.setFormValue(['No_incident']) 
}
This script is not working , not sure where it went wrong

1 answer

0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 4, 2024

Hi @Naveen kumar 

I don't know what you're seeing when you run the code but I'd change it as below to update labels without overwriting existing ones.

 

def customFieldA = getFieldById("customfield_25202") 
def labelsField = getFieldByName("Labels")

def customFieldValue = customFieldA.getValue()
def existingLabels = labelsField.getValue() as List ?: []

if (customFieldValue) {
if (!existingLabels.contains('Incident')) {
existingLabels.add('Incident')
}
// Remove 'No_incident' if present
existingLabels.remove('No_incident')
} else {
if (!existingLabels.contains('No_incident')) {
existingLabels.add('No_incident')
}
// Remove 'Incident' if present
existingLabels.remove('Incident')
}


labelsField.setFormValue(existingLabels)

 

Suggest an answer

Log in or Sign up to answer