Hello!
post function script scriptrunner in Jira server 8
Is it possible to set reporter by value of text field , validate the value and set default reporter if validation failed
explanation
I have a custom text field for user login (account).
How to set the value from the field to Reporter of ticket,
if value in text field is user in jira , if not - set default reporter.
You can use a very simple approach via ScriptRunner's HAPI feature for your requirement.
In your case, if you are still using Jira 8.11, I suggest upgrading your ScriptRunner plugin to version 7.11.0.
Please note that the Jira version you are currently using has reached its End of Life. It would be best to upgrade your Jira instance to at least 8.20 to use the latest plugin releases.
Below is a simplified working code using ScriptRunner's HAPI:-
import com.adaptavist.hapi.jira.issues.Issues
import com.adaptavist.hapi.jira.users.Users
def customIssue = Issues.getByKey(issue.key)
def sampleTextField = customIssue.getCustomFieldValue('Sample Text Field')
// set the user's username into the text field
def existingUser = Users.getByName(sampleTextField)
if (sampleTextField && existingUser) {
customIssue.update {
setReporter(existingUser)
}
}
Please note that the sample working code above is not 100% exact to your environment. Hence you will need to make the required modifications.
Below is a screenshot of the Post-Function configuration:-
Below are a couple of test screenshots for your reference:-
1. When creating a new issue, the Reporter field is set to the currently logged-in user by default, as shown in the screenshot below:-
2. On the create screen, the Sample Text Field is updated with a valid username as shown in the screenshot below:-
3. As expected, the reporter field is updated accordingly once the issue has been created, as shown in the screenshot below:-
4. In the next test, an invalid username will be added to the Sample Text Field, as shown in the screenshot below. The user julia doesn't exist.
5. As expected, once the issue has been created, the reporter is set to the original reporter currently, i.e. the Admin user.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
Hello!
Thanks for the work you've done.
Unfortunately, we are unable to update the plugin at the moment
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It is doable if you want to use the old format with a few more lines of code.
Below is a sample working code for your reference:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
def userManager = ComponentAccessor.userManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def sampleTextField = customIssue.getCustomFieldValue('Sample Text Field')
def existingUser = userManager.getUserByName(sampleTextField)
if (sampleTextField && existingUser) {
issue.setReporter(existingUser)
}
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
The test result of the code above is the same as the test with ScriptRunner's HAPI, as I have shown in my previous comment.
I hope this helps to solve 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.
Made working example. But I want to mention, that there is no necessity to use user ID, because you already have ApplicationUser objects, which can be used in .setReporter() methods
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure
Try this
/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
UserManager userManager = ComponentAccessor.getUserManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
// Set Customfield name or ID
CustomField cField = customFieldManager.getCustomFieldObject(12345)
String username = issue.getCustomFieldValue(cField)
ApplicationUser user = userManager.getUserByName(username)
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Set default user login
ApplicationUser defaultUser = userManager.getUserByName("defaultUser")
if (user) {
issue.setAssignee(user)
} else {
issue.setAssignee(defaultUser)
}
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
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.
Hello!
Thank you for your support before.
One thing. The task is to set task reporter if user in test field is valid.
In this case I need to modify string SetAssignee -> Setreporter
Is it correct?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oh, missed it. Slightly corrected, changed string for setting service account, with required permission rights
/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/
package Examples.Console
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
MutableIssue issue = issue as MutableIssue
UserManager userManager = ComponentAccessor.getUserManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
// Set Customfield name or ID
CustomField cField = customFieldManager.getCustomFieldObject(12345)
String username = issue.getCustomFieldValue(cField)
ApplicationUser user = userManager.getUserByName(username)
// Set default user login
ApplicationUser defaultUser = userManager.getUserByName("defaultUser")
// Set service account login, with permissions to change reporter
ApplicationUser serviceUser = userManager.getUserByName("serviceUser")
if (user) {
issue.setReporter(user)
} else {
issue.setReporter(defaultUser)
}
issueManager.updateIssue(serviceUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
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.
Hello!
Thank a lot for your help.
The script works fine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello!
Could you please assist me with an error.
How to modify the script above to use a method with setReporterId
I'm struggeling due a compile hole script together from console.
For setReporter scriptrunner compiller retorn an error message - Can not find matching method.
I didn't figure out a valid parameter for that.
@field IssueService issueService = ComponentAccessor.getIssueService()
@field IssueLinkManager linkManager = ComponentAccessor.getIssueLinkManager()
@field CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
@field CommentManager commentManager = ComponentAccessor.getCommentManager()
@field UserManager userManager = ComponentAccessor.getUserManager()
@field ApplicationUser reportered=null
@field CustomField cfreportered = customFieldManager.getCustomFieldObject(12345L)
@field String username = issue.getCustomFieldValue(cfreportered)
@field ApplicationUser user = userManager.getUserByName(username)
public getFields()
{
reportered = issue.getCustomFieldValue(cfreportered) as ApplicationUser
}
public IssueService.IssueResult createIssue()
{
if(user){
newIssue.setReporterId(reportered.name)
} else {
newIssue.setReporterId(loggedUserOrigen.name)}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The problem is, that if you want to use .setReporterId() method, user ID != username.
User ID - it's his unique identifier in Jira, which looks like JIRAUSER12345.
Use .getKey() method for objects ApplicationUser. It will look like reportered.getKey(), loggedUserOrigen.getKey()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online 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.