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)
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)
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.