Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

set reporter from text field if value of user is valid by script in scriptrunner

uvo3 main public name
Contributor
July 17, 2023

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.


2 answers

2 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 17, 2023

Hi @uvo3 main public name

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:-

post_function_config.png

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:-

test1.png

2. On the create screen, the Sample Text Field is updated with a valid username as shown in the screenshot below:-

test2.png

3. As expected, the reporter field is updated accordingly once the issue has been created, as shown in the screenshot below:-

test3.png

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.

test4.png

5. As expected, once the issue has been created, the reporter is set to the original reporter currently, i.e. the Admin user.

test5.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

uvo3 main public name
Contributor
July 17, 2023

Hello!
Thanks for the work you've done.

Unfortunately, we are unable to update the plugin at the moment

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 18, 2023

Hi @uvo3 main public name

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 

Like uvo3 main public name likes this
Evgenii
Community Champion
July 19, 2023

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

 

import com.atlassian.jira.component.ComponentAccessor
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.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
UserManager userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()

MutableIssue issue = issueManager.getIssueObject("XYZ-123")

ApplicationUser reportered
CustomField cfreportered = customFieldManager.getCustomFieldObject(12345L)
String username = issue.getCustomFieldValue(cfreportered)
ApplicationUser user = userManager.getUserByName(username)
ApplicationUser loggedUserOrigen = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
reportered = issue.getCustomFieldValue(cfreportered) as ApplicationUser

if (user) {
issue.setReporterId(reportered.getKey())
} else {
issue.setReporterId(loggedUserOrigen.getKey())
}

0 votes
Answer accepted
Evgenii
Community Champion
July 17, 2023

Hi, @uvo3 main public name 

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)
uvo3 main public name
Contributor
July 17, 2023

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?

Evgenii
Community Champion
July 17, 2023

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)
Like uvo3 main public name likes this
uvo3 main public name
Contributor
July 19, 2023

Hello!
Thank a lot for your help.
The script works fine

Like Evgenii likes this
Evgenii
Community Champion
July 19, 2023

If answer helped you, please, accept it. It will help other people with similar problems

Like uvo3 main public name likes this
uvo3 main public name
Contributor
July 19, 2023

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)}

Evgenii
Community Champion
July 19, 2023

Hi, @uvo3 main public name 

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() 

Suggest an answer

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

Atlassian Community Events