Hi, community,
I have a script that I'm using that it'll display an error if the right person is not selected as an approver from the "Change Manger" drop-down field. It's working fine, the only issue is that whenever a user tries to create an issue it displays the first error message about the manager when the "Change Manager" field value is "None", which is the default value from Jira. Is it possible to not display an error when the value is "None"? I want to display the error message when there is a value. When it's none it should not display a message.
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def changeManager = getFieldById('customfield_19001')
def components = getFieldById(getFieldChanged()).getValue() as List<ProjectComponent>
if(components.any{it.name == 'Change Level1'}){
if (!(changeManager.value in ['User One', 'User Two'])) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
} else {
changeManager.setError(" ")
}
} else if(components.any{it.name == 'Change Level2'}){
if (!(changeManager.value in ['User Three', 'User Four'])) {
changeManager.setError("$changeManager.value is not a director, please select a director to approve the change")
} else {
changeManager.setError(" ")
}
} else if(components.any{it.name == 'Change Level3'}){
if (!(changeManager.value in ['User Five', 'User Six'])) {
changeManager.setError("$changeManager.value is not a c-suite, select a c-suite to approve the change")
} else {
changeManager.setError(" ")
}
}
else {
changeManager.setError("please select a change approver from the list.")
}
Thank you,
Hi @Shah Baloch
For your requirement, to ensure that the error message does not trigger unless the wrong option is selected, you should clear the error message in the very beginning with something like this:-
def customField = getFieldByName('Custom Field')
customField.clearError()
Below is the updated code you should test with:-
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def changeManager = getFieldById('customfield_19001')
def components = getFieldById(fieldChanged).value as List<ProjectComponent>
changeManager.clearError()
if(components.find {it.name == 'Change Level1'} && !(changeManager.value in ['User One', 'User Two'])) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
} else if(components.find {it.name == 'Change Level2'} && !(changeManager.value in ['User Three', 'User Four'])) {
changeManager.setError("$changeManager.value is not a director, please select a director to approve the change")
} else if(components.find {it.name == 'Change Level3'} && !(changeManager.value in ['User Five', 'User Six'])){
changeManager.setError("$changeManager.value is not a c-suite, select a c-suite to approve the change")
}
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
Hi @Shah Baloch
In your previous code you seem to be setting getFieldById(getFieldChange()) for the Components field, i.e.:-
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def changeManager = getFieldById('customfield_19001')
def components = getFieldById(getFieldChanged()).getValue() as List<ProjectComponent>
if(components.any{it.name == 'Change Level1'}){
if (!(changeManager.value in ['User One', 'User Two'])) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
} else {
changeManager.setError(" ")
}
} else if(components.any{it.name == 'Change Level2'}){
if (!(changeManager.value in ['User Three', 'User Four'])) {
changeManager.setError("$changeManager.value is not a director, please select a director to approve the change")
} else {
changeManager.setError(" ")
}
} else if(components.any{it.name == 'Change Level3'}){
if (!(changeManager.value in ['User Five', 'User Six'])) {
changeManager.setError("$changeManager.value is not a c-suite, select a c-suite to approve the change")
} else {
changeManager.setError(" ")
}
}
else {
changeManager.setError("please select a change approver from the list.")
}
I assume that you have configured a Server-Side Behaviour for the Components field. Please clarify.
However, in your latest comment, you mentioned:-
I removed component part and just kept the custom field code like below, still getting the error. Not sure what I'm doing wrong.
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def changeManager = getFieldById('customfield_19001')
changeManager.clearError()
if (!(changeManager.value in ['User One', 'User Two'])) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
}And this
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def changeManager = getFieldById('customfield_19001')
changeManager.clearError()
if (!(changeManager.value in ['User One', 'User Two'])) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
}
else {
changeManager.clearError()
}
Can you please clarify that upon removing the Component field from the if/else conditions, you have also changed the field to which the Server-Side Behaviour is configured for?
Suppose you have not changed the Server-Side Behaviour configuration and are still using the Server-Side Behaviour for the Components field. In that case, your code is guaranteed not to work, which is expected.
Also, if you are doing this on the Initialiser, it will not work as expected. The Initialiser is only meant to trigger the Behaviour when the screen is first loaded and not when a change is made to a field. I suggest looking at the ScriptRunner Behaviour documentation for more information on this.
On the other hand, each Server-Side Behaviour is set for a particular custom field and is based on the change/modification of values on these fields.
Since you have modified your code only to include the if/else conditions from the Change Manager field, can you also confirm you have updated the Server-Side Behaviour and set it for the Change Manager field?
It would be best if you could also share a screenshot of your Server-Side Behaviour configuration so we could get more clarity on whether you are on the right track or not.
Another point, from your original code, I missed out one point, i.e. since the condition is based on two fields, two separate Server-Side Behaviour configurations are required as shown below:-
For the Components field:-
import com.atlassian.jira.bc.project.component.ProjectComponentImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def components = getFieldById(fieldChanged)
def componentsValue = components.value as List<ProjectComponentImpl>
def sampleList = getFieldByName('Sample List')
def sampleListValue = sampleList.value as String
sampleList.clearError()
if (componentsValue.first().name == 'GUI' && !sampleListValue in ['Option 1','Option 2']) {
sampleList.setError('Wrong Options Selected for GUI Component')
} else if (componentsValue[1].name == 'Database' && !sampleListValue in ['Option 3','Option 4'] ) {
sampleList.setError('Wrong Options Selected for Databse Component')
} else if (componentsValue.last().name == 'Networking' && !sampleListValue in ['Option 5','Option 6']) {
sampleList.setError('Wrong Options Selected for Databse Component')
}
Below is a screenshot of the Component/s field Server-Side Behaviour configuration:-
And for the List:-
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
import com.atlassian.jira.bc.project.component.ProjectComponentImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def components = getFieldById(COMPONENTS)
def componentsValue = components.value as List<ProjectComponentImpl>
def sampleList = getFieldByName(fieldChanged)
def sampleListValue = sampleList.formValue.toString()
sampleList.clearError()
if (componentsValue.first().name == 'GUI' && !sampleListValue in ['Option 1','Option 2']) {
sampleList.setError('Wrong Options Selected for GUI Component')
} else if (componentsValue[1].name == 'Database' && !sampleListValue in ['Option 3','Option 4'] ) {
sampleList.setError('Wrong Options Selected for Databse Component')
} else if (componentsValue.last().name == 'Networking' && !sampleListValue in ['Option 5','Option 6']) {
sampleList.setError('Wrong Options Selected for Databse Component')
}
Below is a screenshot of the Server-Side Behaviour configuration for the List:-
Please note that the sample codes provided above are not 100% exact to your environment. Hence, you will need to make the required modifications.
Lastly, please clarify what version of ScriptRunner and Jira you currently use.
Kindly continue on this thread so we don't loose track.
I am looking forward to your feedback and clarification.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, @Ram Kumar Aravindakshan _Adaptavist_ I'm using ScriptRunner version 8.0.0 with Jira v8.20.10. After applying your code it still shows the error when no value is selected from the change approver field. The goal is not to show an error when there isn't a value selected. I made the field required user will select a value for sure but it won't select the right person for the right change then it should display the error. I'm attaching the screenshot if it 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.
Hi @Shah Baloch
Can you please confirm that you have created 2 separate Behaviour configurations as I have shown in my previous comment?
I'm asking this because I am not encountering this problem in my environment.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, @Ram Kumar Aravindakshan _Adaptavist_ yes created two separate behavior configurations. Once of component field and send one for the "Approvers". I copied your code, just changed the custom field name and name of the users, and added component options. I added the same code in both fields' configurations. I tried to create an issue using option three and the wrong approvers, it was supposed to show an error. It isn't showing an error with any option at all. I kept changing users from the list, it didn't show an error for any of the users. Though I just mapped two users for each option. Not sure what I'm doing wrong.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shah Baloch
After going through the code you shared in your latest comment, it is an error in the code.
The Behaviour code you put for the Component/s field is meant for the custom field. This is the root cause of its failure.
I am adding the code once again for you to refer to:-
import com.atlassian.jira.bc.project.component.ProjectComponentImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def components = getFieldById(fieldChanged)
def componentsValue = components.value as List<ProjectComponentImpl>
def sampleList = getFieldByName('Sample List')
def sampleListValue = sampleList.value as String
sampleList.clearError()
if (componentsValue.first().name == 'GUI' && !sampleListValue in ['Option 1','Option 2']) {
sampleList.setError('Wrong Options Selected for GUI Component')
} else if (componentsValue[1].name == 'Database' && !sampleListValue in ['Option 3','Option 4'] ) {
sampleList.setError('Wrong Options Selected for Databse Component')
} else if (componentsValue.last().name == 'Networking' && !sampleListValue in ['Option 5','Option 6']) {
sampleList.setError('Wrong Options Selected for Databse Component')
}
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
import com.atlassian.jira.bc.project.component.ProjectComponentImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def components = getFieldById(COMPONENTS)
def componentsValue = components.value as List<ProjectComponentImpl>
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.formValue.toString()
sampleList.clearError()
if (componentsValue.first().name == 'GUI' && !sampleListValue in ['Option 1','Option 2']) {
sampleList.setError('Wrong Options Selected for GUI Component')
} else if (componentsValue[1].name == 'Database' && !sampleListValue in ['Option 3','Option 4'] ) {
sampleList.setError('Wrong Options Selected for Databse Component')
} else if (componentsValue.last().name == 'Networking' && !sampleListValue in ['Option 5','Option 6']) {
sampleList.setError('Wrong Options Selected for Databse Component')
}
Please note that the sample codes above are not 100% exact to your environment. Hence, you will need to make the required modifications.
To proceed, I suggest copying the sample codes I provided to the Server-Side Behaviours of the fields accordingly and making the required modifications.
Another point is that the Component names I added to the if/else condition is according to my environment. You must modify this for the components to work.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shah Baloch
Is the updated code working?
If yes, plesse accept this answer.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ram Kumar Aravindakshan _Adaptavist_Unfortunately it's working for me. Last time I forgot to change the component field. I added the component id and custom field name, then added component options into your code, still didn't work. Not sure if it's because of the environment or if I'm doing something wrong. Thanks,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shah Baloch
Looking at your latest comment, there is still a mistake in your updated code.
Please view the updated screenshot below:-
If you observe your code, you still have declared the Component/s field as:-
def components = getFieldById("components")
As I have mentioned earlier, when setting the Component/s field for the Server-Side Behaviour, it must be declared as:-
def component = getFieldById(fieldChanged)
else, the Server-Side Behaviour will not work. I have highlighted the error in your code in the updated screenshot.
Kindly observe the screenshot and make the required modification that I have advised.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ I'm unsure if I understood correctly. Are you asking to use "Component/s id instead of name? If yes then Component/s system field id is "components". I doubled checked from the system field list. I'm attaching my instance's screenshot. If you mean something else then please explain what field are you asking to add there. Thank you,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Shah Baloch
In your last comment, you mentioned:-
I'm unsure if I understood correctly. Are you asking to use "Component/s id instead of name? If yes then Component/s system field id is "components".
I'm afraid you have failed to understand how Server-Side Behaviour works. Yes, the Component/s id is "components" I am aware of this. That is not the issue.
This format applies to any system field. For example, the id for the System's Description Field is "description"; for the Summary field, it is "summary"; for the Affected Version/s, it's "affectedVersions"; Component/s is "components", and so on.
However, in the case of Server-Side Behaviour, this is different. When a Server-Side Behaviour is added for a field, irrespective of whether it is a System Field or Custom field, when the field is declared, it must use the fieldChanged parameter, i.e.:-
def field = getFieldById(fieldChanged)
Else, the behaviour will not work as expected.
In your scenario, the Server-Side Behvaiour has been added for the Component/s field, which is a System field. Hence when declaring the Components field, it must be declared as:
def components = getFieldById(fieldChanged)
I suggest going through the ScriptRunner Documentation first to understand better how to use the Server-Side Behaviour.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ram Kumar Aravindakshan _Adaptavist_I'm sorry I misunderstood. Thank you for the clarification, it makes sense now. Thank you for all your help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, @Shah Baloch
One more variant, without many if conditions, and easier changing, in case if you will have more than 3 comparisons
/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
FormField changeManager = getFieldById('customfield_19001')
List<ProjectComponent> components = getFieldById(fieldChanged).value as List<ProjectComponent>
changeManager.clearError()
Map managers = [
'Change Level1': ["users": ['User One', 'User Two'], "message": "is not a manager, please select a manager to approve the change"],
'Change Level2': ["users": ['User Three', 'User Four'], "message": "is not a director, please select a director to approve the change"],
'Change Level3': ["users": ['User Five', 'User Six'], "message": "is not a c-suite, select a c-suite to approve the change"]
]
List intersections = components.name.intersect(managers.keySet())
if (changeManager.value && intersections) {
intersections.each { String key ->
if (!changeManager.value.toString() in managers[key]['users']) {
changeManager.setError("$changeManager.value ${managers[key]['message']}")
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Shah Baloch updated script.
Now it removes error text after "Change Manager" field is cleared. Checked it in my instance, it makes what you need. Don't use it in initialiser script in Behaviour. Insert it in Behaviour in script section for added field "Change Manager"
/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
FormField changeManager = getFieldById(getFieldChanged())
List<ProjectComponent> components = getFieldById("components").getValue() as List<ProjectComponent>
changeManager.clearError()
Map managers = [
'Change Level1': ["users": ['User One', 'User Two'], "message": "is not a manager, please select a manager to approve the change"],
'Change Level2': ["users": ['User Three', 'User Four'], "message": "is not a director, please select a director to approve the change"],
'Change Level3': ["users": ['User Five', 'User Six'], "message": "is not a c-suite, select a c-suite to approve the change"]
]
List intersections = components.name.intersect(managers.keySet())
if (changeManager && intersections) {
intersections.each { String key ->
if (!(changeManager.value.toString() in managers[key]['users'])) {
changeManager.value ? changeManager.setError("$changeManager.value ${managers[key]['message']}") : changeManager.clearError()
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, @Shah Baloch
Try to check my script, from upper post. I hope it will help (well, in my test instance it worked)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry guys I'm still having trouble clearing the error when there isn't a value in the custom field. I removed component part and just kept the custom field code like below, still getting the error. Not sure what I'm doing wrong.
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def changeManager = getFieldById('customfield_19001')
changeManager.clearError()
if (!(changeManager.value in ['User One', 'User Two'])) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
}
And this
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def changeManager = getFieldById('customfield_19001')
changeManager.clearError()
if (!(changeManager.value in ['User One', 'User Two'])) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
}
else {
changeManager.clearError()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The behaviour is firing off the field that is being changed which is the component. You are going to have to use the logic that if there is nothing in the component field, do nothing.
The clearError() needs to work off the component field. If you look at @Ram Kumar Aravindakshan _Adaptavist_ and @Evgenii answers that is what they are doing.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Shah Baloch
You could try having a check to see if it is empty and do nothing as your first if statement
if(!component){
return null}
else if(...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Tim Perrault I'm not displaying the error for components. It's for the custom drop-down field "Change Manager". By default, Jira set the value of the drop-down field "None". When I click create it displays the error under the custom field (Change Manager). I want to have the error there when the right people are not selected for the right component. The issue is error is already there when nothing is selected from the custom field. I'm not sure I can apply the same logic with a custom field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok I see. You could check to see if component is empty and then maybe you could combine conditions.
if(!component){
return null}
else if((components.any{it.name == 'Change Level1'}) && (!(changeManager.value in ['User One', 'User Two']))) {
changeManager.setError("$changeManager.value is not a manager, please select a manager to approve the change")
}
else{
}
*I haven't tested this but logically it seems sound.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.