How add user to a custom field based on value of other field?

Shah Baloch
Contributor
May 11, 2023

Hi Community, there is an existing Behaviour script that I need to modify. The current script working fine. If the number is 10000 or less it add the user1 to a user field but if the number is greater than 10000 it add user2 to the user field. There is an additional requirement now. If the number is 5000 or less add reporter to user field, if number is more than 5000 and 1000 or less add user1 to user field greater than 10000 should be the same. Here is an example:
5000 or less = Issue Reporter
Greater than 5000 and less than 1000 = User1
Greater than 10000 = User2

How can modify and achieve the requirement?

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_11305')
def textFormField = getFieldById('customfield_11306')

Integer number = textFormField.getValue() as Integer
String user = number > 10000 ? 'user2' : 'user1'
userFormField.setFormValue(user)

Thank you,

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 11, 2023

There are many ways to achieve that.

One way is to use the existing construct and just expand it:

import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_11305')
def textFormField = getFieldById('customfield_11306')
def reporterField = getFieldById(IssueFieldConstants.REPORTER)
Integer number = textFormField.getValue() as Integer
String user = number > 10000 ? 'user2' : number > 5000 ? 'user1' : reporterField.value
userFormField.setFormValue(user)

But that might be hard to read.

Another way can be a bit more explicit:

import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_11305')
def textFormField = getFieldById('customfield_11306')
Integer number = textFormField.getValue() as Integer
String user
if (number > 10000) {
user = 'user2'
} else if (number <= 10000 && number > 5000) {
user = 'user1'
} else {
def reporterField = getFieldById(IssueFieldConstants.REPORTER)
user = reporterField.value
}
userFormField.setFormValue(user)

This assumes the reporter field is available to the behaviour (present on the screen).

If it is not and what you really want is the currently logged-in user, then you can try it like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_11305')
def textFormField = getFieldById('customfield_11306')
Integer number = textFormField.getValue() as Integer
String user
if (number > 10000) {
user = 'user2'
} else if (number <= 10000 && number > 5000) {
user = 'user1'
} else {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
user = currentUser.name
}
userFormField.setFormValue(user)
Shah Baloch
Contributor
May 11, 2023

Hi @Peter-Dave Sheehan I'm having an odd issue with first two scripts. I mean when the number is less than 5000 it's not adding the reporter instead it's adding an inactive user who used to be part of the project. The user field doesn't have default value. it works fine when the number is greater than 5000 and 10000 but it didn't add reporter into that field. However the last script is working fine.

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 12, 2023

If the last script is working, but the first 2 were not is because the reporter field was not available on the screen so it didn't return a real value.

Glad that last script worked.

Like Shah Baloch likes this
Shah Baloch
Contributor
May 16, 2023

@Peter-Dave SheehanI forgot to add Reporter field on create issue screen. Added field and it worked. Thank you so much.

Shah Baloch
Contributor
June 2, 2023

Hi @Peter-Dave Sheehan ,  I'm trying to modify the script to add another condition but it's not working. I added a custom dropdown field "Department" and the field has all the departments, such as Accounts, HR, IT, Administration, etc. I would like to add an additional condition for example if the number is > 10000 and the Department field has the value Accounts then it should add user2 to the user field. I tried the below script but it didn't work. How can I add multiple conditions?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_11305')
def textFormField = getFieldById('customfield_11306')
def dapartment = getFieldById('customfield_11307')
Integer number = textFormField.getValue() as Integer
String user
if (number > 10000) {
department.value == 'Accounts'
user = 'user2'
} else if (number > 10000) {
department.value == 'Human Resource'
user = 'user3'

}else if (number <= 10000 && number > 5000) {
user = 'user1'
}else {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
user = currentUser.name
}
userFormField.setFormValue(user)

Thank you,

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023

You have a typo.

The first instance of the field variable is dapartment ... then subsequent ones are department.

But beyond that, you need to structure your nested condition a bit more:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_11305')
def textFormField = getFieldById('customfield_11306')
def department = getFieldById('customfield_11307')
Integer number = textFormField.getValue() as Integer
String user
if (number > 10000) {
if (department.value == 'Accounts') {
user = 'user2'
} else if (department.value == 'Human Resource'){
user = 'user3'
} else {
//is there a default value for >10000 and neither account or HR are selected?
}
} else if (number <= 10000 && number > 5000) {
user = 'user1'
} else {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
user = currentUser.name
}
userFormField.setFormValue(user)
Shah Baloch
Contributor
June 2, 2023

Thank you, I'll test it on Monday. Thank you so much!

TAGS
AUG Leaders

Atlassian Community Events