Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How Update Custom User Field base on Component selection and another custom field value?

Hi Community,

I need help to fix a behaviour script. I have a script that adds user1 and user2 base on the amount added in a CFText. For example, if the amount is 1000 or less it adds user1 to the UserCF or if it is more than 1000 then add user2 to UserCF. That part is working fine and I have the following first script for that.

This is the existing and working script without components added.

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_14000')
def textFormField = getFieldById('customfield_12500')

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

userFormField.setFormValue(user)

Now I'm trying to make it work with two of the components. For example, if component "Refunds" is selected and the amount is 1000 or less it should add user1 to the UserCF or if component "Refunds" is selected and the amount is more than 1000 then it should add user2 to UserCF. For the rest of the components, it should do nothing. The project has about 10 components but we want that condition should apply for two components only. For the rest of the components regardless of the amount, it should be applied.

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def userFormField = getFieldById('customfield_14000')
def textFormField = getFieldById('customfield_12500')

//-----------------------
def components = getFieldById(getFieldChanged()).value as List<ProjectComponent>

if(components.any{it.name == 'Refund'}){
Integer number = textFormField.getValue() as Integer
String user = number > 10000 ? 'user2' : 'user1'

userFormField.setFormValue(user)

}

Thank you for your help.

1 answer

0 votes

So, does this new version of the script work when you select component Refund? It looks to me like it should.

And are you asking how to change that to work with Refund +anotherComponent?

If so, then you can change 

if(components.any{it.name == 'Refund'}){ 

to

if(components.any{it.name in ['Refund','OtherComponent']}){ 

Sorry I didn't explain it well. I meant if the user selects the "Refund" or if the user selects component "Other" then the second condition should be applied. Users only select one component so it should work for one component at a time. I was trying to say that I want that 'amount' condition should be applied to either of the two components, and it should not be applied to the rest of the  8 components. Let suppose there is a component "Refund" and another one is "Purchase". If a user creates a ticket select component "Refund" and the amount is 1000 or less the "UserCF" should have the value "ShiftLead" (user1) or if the component is "Refund" and the amount is more than 1000 then the "UserCF" should have the value "manager"(user2). But if the component is "Purchase" the amount condition should not be applied.

Currently, the script works fine if I won't include the component code in the script as I mentioned earlier, but I'm trying to make it work with components condition and amount condition. Actually there two conditions that need to be true in order to assign a value to UserCF field. One is component, if that not true then the rest of the condition should not be applied but if the first one is true then the second should be applied base on the amount value. Hope that makes sense. Thanks

That's what 

if(components.any{it.name in ['Refund','OtherComponent']}){ 

should do.

If the name of any of the select components are part of the list [Refund, Other] then run the next line.

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def amountComponents= ['Refund', 'Other']

def userFormField = getFieldById('customfield_14000')
def textFormField = getFieldById('customfield_12500')

//-----------------------
def components = getFieldById(getFieldChanged()).value as List<ProjectComponent>


if(components.any{it.name in amountComponents}){
Integer number = textFormField.getValue() as Integer
String user = number > 10000 ? 'user2' : 'user1'

userFormField.setFormValue(user)
}
Like Shah Baloch likes this

It is working but not as I'm expecting to make it works. Whenever I select the "Refund" component it adds user1 to that UserCF then I add the amount (11000) in the amount field it won't change the value of UserCF to user2 till I won't remove the "Refund" component. In this way, I have to enter the amount first then select the component which will cause issues.

For behaviour to react to a change in the amount field, you must place a script on that field too.

Make your script generic then place it in both the components and the amount field:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def amountComponents= ['Refund', 'Other']

def userFormField = getFieldById('customfield_14000')
def amountFormField = getFieldById('customfield_12500')
def componentFormField = getFieldById('components')

def components = componentFormField.value as List<ProjectComponent>


if(components.any{it.name in amountComponents}){
Integer amount = componentFormField.value as Integer
String user = amount > 10000 ? 'user2' : 'user1'

userFormField.setFormValue(user)
}

If you create a script using the script editor, then you can link to that script in both fields and only have 1 place to maintain the code.

Like Shah Baloch likes this

I try to add the same script in both from the behaviour tab, like added component and amount field and add the same script in both. It is not working. How I can link the script from the script editor to fields? I couldn't find a way. Thanks

Delete the server-side script. Then create it again, and use the File Tab (above the script box, aligned right).

You might be able to just delete all the text in the script box... but with some older versions of scriptrunner, that didn't always work to enable switching to "File"

Make sure your field doesn't have "getFieldChanged()" because now this will be a different field based on which one triggered the change.

Like Shah Baloch likes this

Thank you so much, there was a mistyped. I changed "Integer amount = amountFormField" instead of compoenetFormField and it worked. Thank you so much.

Working code

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def amountComponents= ['Refund', 'Other']

def userFormField = getFieldById('customfield_14000')
def amountFormField = getFieldById('customfield_12500')
def componentFormField = getFieldById('components')

def components = componentFormField.value as List<ProjectComponent>


if(components.any{it.name in amountComponents}){
//Integer amount = componentFormField.value as Integer // old one
Integer amount = amountFormField.value as Integer //updated this one
String user = amount > 10000 ? 'user2' : 'user1'

userFormField.setFormValue(user)
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

☕️ Monday coffee with Jexo: Weekly Atlassian news roundup | 21st June 2021

Hi community 👋, as every Monday we're bringing you a quick update on what happened in the Atlassian ecosystem last week. There were a few interesting events like for example the announcement of th...

51 views 0 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you