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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
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.