The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

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

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
John Funk
Community Champion
May 14, 2025

Hi @Naveen kumar  - Did you get this resolved? And if so, what did you do?

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
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)

 

Naveen kumar
December 11, 2024

@Tuncay Senturk _Snapbytes_ not related to this one, can we hide the child option (secondary one of the field) in a cascading field? is there a way to do it ?. If yes can you show me the way please.

 

Meanwhile ill try with the above code, thanks so much