Hi All,
Any suggestion if I want to add a new label via Script Runner Listener when the page is removing the specific label in existing page.
Thanks
Could you provide a little more information, i.e. what label do you want to include when the previous label is removed?
Are you expecting this Listener to work only when a specific label is removed or if any label is removed?
I am looking forward to your feedback and clarification.
Thank you and Kind regards,
Ram
e.g., if label in the page "label_a" has removed from the page then I want to add "label_b" to the same page where the label_a has removed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For your requirement, it would be best to use ScriptRunner's Custom Listener and the ChangeHistoryManager object.
For your Listener, you need to use the IssueUpdated event.
Below is a sample working code for your reference:-
import com.atlassian.jira.component.ComponentAccessor
def issue = event.issue
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def changeHistories = changeHistoryManager.getChangeHistories(issue)
def field = changeHistories.changeItems['field'].last() as List
if (field[0].toString() == 'labels') {
def lastLabelList = changeHistories.changeItems['oldstring'].last() as List
def lastLabel = lastLabelList[0].toString()
if (lastLabel.length() > 0) {
def number = lastLabel.replaceAll('[^0-9]', '') as Integer
def label = lastLabel.replaceAll('[0-9]', '') as String
issue.update {
setLabels("${label}${number+1}")
}
}
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Also, please ensure you have updated your ScriptRunner plugin to the latest release, 8.10.0 or at least version 7.11.0, so you can use the ScriptRunner HAPI feature to simplify your code. The example above is done using the HAPI feature.
Below is a screenshot of the Listener configuration:-
Below are a couple of test screenshots for your reference:-
1. When the issue is created, a label Test_1 is added, as shown in the screenshot below:-
2. Next, the Label is deleted and saved as shown in the screenshot below:-
3. As expected, a new Label is automatically added once the Label is removed and saved. This time it's Test_2, as shown in the screenshot below:-
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If your question has been answered, kindly accept the answer provided.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.