Appending the update of custom field of type label to already existing value in the field

Jagadish October 15, 2019

Whenever i try to update my custom field , the old value gets deleted and the new value takes its place . But here i need the new value to be appended to the old one .

I am using ScriptRunner for jira .

1 answer

0 votes
Maksim Smetannikov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2019
Jagadish October 15, 2019

Hi @Maksim Smetannikov

addLabel() doesnt work . Its asking to check if the method exists .

Maksim Smetannikov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2019

Sorry,  Jagadish

I did not notice that the label field is custom. For a custom field of type label, the answer will look like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption


def issueManager = ComponentAccessor.getIssueManager()
MutableIssue issue = issueManager.getIssueObject("MYP-1")

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customLabelFieldID = customFieldManager.getCustomFieldObject("customfield_10100").getIdAsLong()
def labelManager = ComponentAccessor.getComponent(LabelManager)

labelManager.addLabel(currentUser, issue.getId(), customLabelFieldID, "123", false)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
Hubert Rzyha July 29, 2020

If you want to just add label to existing labels on current issue using post function use this. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.issue.IssueEventBundle;
import com.atlassian.jira.event.type.EventType;
def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()

issueManager = ComponentAccessor.getIssueManager()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def labelManager = ComponentAccessor.getComponent(LabelManager.class)

labelManager.addLabel(currentUser, issue.getId(), "LABEL_NAME", false)

 

It need to be in scirpt runner post function then issue object exist and you use issue.getId()

Suggest an answer

Log in or Sign up to answer