Set custom field value [Script Runner]

Chang Jikyu May 4, 2023

Hello. I created a script that appends a user list to the existing values in a custom field.
The custom field is a User Picker (multiple users).

I ran it on Scriptrunner > Jobs > escalation service with these conditions and works well.

My question is this : How can I make it update the custom field values while not actually updating the issue itself? This is becuase when a issue is updated, too many notifications will be sent through the slack "Jira Server" App.

Conditions:

Jira server version : 8.10.1
Scriptrunner version : 6.34.0 
User : APIUser
JQL Query : Project = Test AND Created > "2023/05/01" AND reporter = jkjang

Code:

import com.atlassian.jira.component.ComponentAccessor

def user_add = ['jkjang','johanne']

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == '참조자'}
def existing_user = (List) issue.getCustomFieldValue(cf)

def final_user = existing_user + user_add
def string_final_user = final_user.join(",")
def output = string_final_user.replaceAll(/\([^()]*\)/, "")

issueInputParameters.addCustomFieldValue(cf.id, output)

 

1 answer

1 accepted

1 vote
Answer accepted
Radek Dostál
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.
May 4, 2023

To update an issue without history (which doesn't "trigger" an issue updated event), you would need to use the #updateValue method for the CustomField

 

A simple example would look like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

Issue issue; // either from binding, or use IssueManager to get an instanceof Issue

CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12345")
// ^----- Let's assume a text field for proof of concept purposes

String currentValue = issue.getCustomFieldValue(customField) ?: ""
String newValue = currentValue + " appended text"


ModifiedValue modifiedValue = new ModifiedValue(newValue, currentValue)
customField.updateValue(null, issue, modifiedValue, new DefaultIssueChangeHolder())

// Optionally, reindex the issue if you need to search on the new value
// Not sure if this is or isn't needed, but I think it still is
//ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)

 

Chang Jikyu May 4, 2023

Hello, Good Day. 

I just tried this script with only changing the customfieldID but unfortunately this happened

- The customfield (text field) didn't append " appended text"

- The issue was updated (history left)

Chang Jikyu May 4, 2023

For those who searched for this issue. I figured it out somehow thanks to Radek giving me the idea. I'll leave the final code below. You have to run it in the script console of scriptrunner and it will only work for User picker (multiple users) custom field.

The code is pretty messy cause I just made it work but I hope it helps

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.index.IssueIndexingService

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.user.util.UserManager

import com.atlassian.jira.bc.issue.search.SearchService

import com.atlassian.jira.jql.parser.JqlQueryParser

import com.atlassian.jira.web.bean.PagerFilter

JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

SearchService searchService = ComponentAccessor.getComponent(SearchService)

def issueManager = ComponentAccessor.getIssueManager()

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

def query = jqlQueryParser.parseQuery("""Project = SRETM AND Created > "2023/05/01" AND reporter = jkjang""")

    def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

    search.results.each { documentIssue ->

        def issue = issueManager.getIssueObject(documentIssue.id)

CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10400")

    def existing_user = (List) issue.getCustomFieldValue(customField)

    def user_add = ['ecmsshyoon001', 'ecmshijung001', 'ecmsejlee01', 'ecmssypark001']

    def final_user = existing_user + user_add

    def string_final_user = final_user.join(",")

    def output = string_final_user.replaceAll(/\([^()]*\)/, "")

    def userManager = ComponentAccessor.getUserManager()

    def usernames = output.split(',')

    def users = []

    usernames.each { username ->

        def user2 = userManager.getUserByName(username)

        if (user2) {

            users.add(user2)

        }

    }

    ModifiedValue modifiedValue = new ModifiedValue(null, users)

    customField.updateValue(null, issue, modifiedValue, new DefaultIssueChangeHolder())

    ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)

}
Like Jeroen Poismans likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.10.1
TAGS
AUG Leaders

Atlassian Community Events