IF/ELSE Script-Runner Script for Custom Field Jira issue Create

장부현 February 16, 2023

Hello.

In my case, it is proceeded when jira issue is created.

For example

Jira issue Field

1#Custom_Field : User Picker (single user) << Field Required

2#Custom_Field : User Picker (single user)

Q1.

If the 2#Custom_Field value is included, Change Assignee(2#Custom_Field) and

Go to Jira's specific status

Q2.

If the 2#Custom_Field value is Not included, Change Assignee(1#Custom_Field)

 

I don't know how to implement it using Script-Runner.
Can I help you?

Thank U . EveryOne

 

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 24, 2023

Hi @장부현

For your requirement, I suggest upgrading your ScriptRunner to the latest release, i.e. 7.11.0 and trying to use the HAPI features.

Below is a sample working code for your reference:-

def userPicker1 = issue.getCustomFieldValue('Sample User Picker 1')
def userPicker2 = issue.getCustomFieldValue('Sample User Picker 2')

issue.set {
setAssignee(userPicker1.name)
if (userPicker1 && userPicker2) {
setAssignee(userPicker2.name)
}
}

Please note that the sample code above is not 100% exact to your environment.

Below are the screenshots for the Post-Function configuration:-

1. Go to the Workflow Diagram and select the Create transition. Once the property dialog appears, click on the Post-Function as shown in the screenshot below:-

post_fuction1.png

2. In the Post-Function options, select the Custom script post-function [ScriptRunner] option as shown below:-

post_fuction2.png

3. Finally, once you are on the Custom script post-function page, you can add the sample code I provided above. Please ensure that you make the required changes.

image_2023-02-27_194345615.png

One point, please ensure that the Post-Function is added to the very top of the Create transition as shown in the screenshot below:-

image1.png

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

Thank you and Kind regards,
Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 23, 2023

Hi @장부현

Has your question been answered?

If yes, please accept the solution provided.

Thank you and Kind regards,

Ram

0 votes
Jeroen Poismans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 18, 2023

Hi there!

Before going into scriptrunner, do you have automation on your instance because with that I think you would not have to write a single line of code.

That being said, I have a similar script that really should get you on the way:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField

IssueManager issueManager = ComponentAccessor.getIssueManager()

MutableIssue editIssue = issue
def userpickerFieldId = 13401L

CustomField userPickerField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(userpickerFieldId)
def user = editIssue.getCustomFieldValue(userPickerField)
def updateUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (user == null) {
return false
}

editIssue.setAssignee(user)
try {
issueManager.updateIssue(updateUser, editIssue, EventDispatchOption.ISSUE_UPDATED, true)
} catch(Exception e) {
log.error("Error setting assignee $user.username on issue $editIssue.key")
}

 

This script:

  • Gets the value of a user picker field
  • Checks if it's empty or not
  • If it's not empty, it takes the user from the field and puts it as the Assignee

 

With some change you can get it to work for your case. When finished this should be in a postfunction of the Create transition.

 

Hope this helps!

Jeroen

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events