import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
def authContext = ComponentAccessor.getJiraAuthenticationContext()
def currentUser = authContext.getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserManager()
def myCustomfield = customFieldManager.getCustomFieldObjectsByName('Approved By').first()
issue.setCustomFieldValue(myCustomfield, currentUser)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Figured it out!
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
def customFieldManager = ComponentAccessor.getCustomFieldManager() def userManager = ComponentAccessor.getUserManager()
// Get the current user def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Get the custom field def customField = customFieldManager.getCustomFieldObjectByName('Approved By') // Get the current value of the custom field def currentUsers = issue.getCustomFieldValue(customField) as List<ApplicationUser> // Set the value of the custom field if (currentUsers == null) { currentUsers=[currentUser] } if (currentUser != null) { currentUsers.add(currentUser) } // Update the value of the custom field issue.setCustomFieldValue(customField, currentUsers)
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserManager()
// Get the current user
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Get the custom field
def customField = customFieldManager.getCustomFieldObjectByName('Approved By')
// Update the value of the custom field
issue.setCustomFieldValue(customField, [currentUser])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Norma Pena
Can you please clarify what field you use for the Approved By? Is it a single-user picker, a multi-user picker or maybe a text field?
I can get this to work in my environment without issues using a code similar to yours and testing with a single-user picker.
Please note that the sample codes below are not 100% exact to your environment. Hence you will need to make the required modifications.
Below is the code that I have tested for the single-user picker:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def approver = customFieldManager.getCustomFieldObjectsByName('Approved By').first()
issue.setCustomFieldValue(approver, loggedInUser)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Below are a few test screenshots for your reference:-
1. When the issue is first created, the Approver By field is not set:-
2. Once the issue has transitioned to the In Progress status, the Approved By field is updated as shown below:-
If it is a multi-user picker, you need to try something like this:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def issue = issue as MutableIssue
def customFieldManager = ComponentAccessor.customFieldManager
def groupManager = ComponentAccessor.getGroupManager()
def group = groupManager.getGroup('jira-administrators') // set group name
def usersInGroup = groupManager.getUsersInGroup(group)
def approvers = customFieldManager.getCustomFieldObjectsByName('Approved By Multi').first()
approvers.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(approvers), usersInGroup), new DefaultIssueChangeHolder())
When using the multi-user picker, when the issue transitions, the field will be updated like:-
If you are still encountering an error, it would be helpful if you could share some of your log output for further investigation.
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.
Hi @Norma Pena
Try this and replace my field's name with yours. I've tried it on scriptrunner's console and works fine:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
IssueManager issueManager = ComponentAccessor.getIssueManager()
def cfObj = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName("Test User Picker (single)")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setCustomFieldValue(cfObj, user)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED ,false)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Norma Pena
You could try this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// a user custom field
def userCf = customFieldManager.getCustomFieldObjectByName("Approved By")
def currenrUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setCustomFieldValue(userCf, currenrUser)
ComponentAccessor.getIssueManager().updateIssue(currenrUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.