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,
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)
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Peter-Dave SheehanI forgot to add Reporter field on create issue screen. Added field and it worked. Thank you so much.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
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.