Hi Team,
I have written a script where if user select category in Category field then project related to selected category will appear in Project field and then user can select any proect as a options from it.
I have used SetFieldOptions to Project field but even though providing and selecting options the field returning null and hence ticket is not getting created.
Here is the behaviour code for it :
Hi @sanjay
I can confirm your code will not work.
Firstly, you have declared the category field as below:-
def category = getFieldById(getFieldChanged())
Secondly, instead of using the if/else condition based on the category, you have used the condition on the projectCategory variable.
The fieldChanged / getFieldChange() value ensures that the Server-Side Behaviour will only trigger when the field it is configured for is updated.
In your case, I am going to assume that the Behaviour is to trigger based on the change made on the Project Category field. If this is the case, you must ensure the Server-Side Behaviour is configured for the Project Category field.
Please also clarify what type of fields you are using for Project Category and Project ID. I am requesting this to prepare a sample working code for your reference.
Thank you and Kind regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
Thanks a lot for your suggetions.
Below are the information you need :
1 : I have set the Behaviour on Project Category field (Select List single choice)
2 : Project ID is also select list single choise field (Where we need to set the options)
3 : There are 40 category present in Project Category field and there can be number of project present for single category and hence I didnt used else if here.
4 : Whenever user will select any category in First field then Project related to onlt that categoru we want for selection in Project ID field.
Hope this will help!
Thanks,
Sanjay
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sanjay,
Here's my next question: you mentioned that the second field is Project ID, but in the screenshot you shared, it looks like it's the project name that's being used.
Please clarify which is the value actually.
Thank you and Kind regards,
Ram
Senior Support Engineer
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_ ,
The field name is for user on portal and as it is select list field I only need to set project name options in it so that user can seelct any project for selected category.
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @sanjay
Thank you for your clarification.
Below is a fully working code for your reference:-
import com.adaptavist.hapi.jira.projects.Projects
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def projectCategory = getFieldById(fieldChanged)
def projectCategoryValue = projectCategory.value
def projectNames = getFieldByName('Project Names') // Rename the field accordingly
def projectNameOptions = ['Mock', 'Service Test'] // Add the name of all the projects
if (projectCategoryValue) {
def projectCategoryId = Projects.allProjects*.projectCategory.find { category ->
category.name == projectCategoryValue.toString()
}.id
projectNameOptions = Projects.allProjects.collect { project ->
if (project.projectCategory && project.projectCategory.id == projectCategoryId) {
project.name
}
}
}
projectNames.setFieldOptions(projectNameOptions)
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Behaviour configuration:-
I used ScriptRunner's HAPI feature to simplify the code.
Below are a couple of test screenshots for your reference:-
1. In the first 2 screenshots below, when no option is selected from the Project Category list, as shown in the 1st screenshot, all options from the Project Names list are accessible, as shown in the 2nd screenshot.
2. If Sprint Project is selected from the Project Category list, only the Mock option is available in the Project Names list.
3. Similarly, if the Custom Support Project option is selected from the Project Category list, only the Service Test option is available in the Project Names list.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
Senior Support Engineer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @
Can we use this code on Service Portal for JSM Project.
I am trying but not getting expected results.
Getting Below error :
2024-05-17 13:28:06,064 WARN [runner.ScriptBindingsManager]: Commercial MVP 2024-05-17 13:28:06,091 ERROR [behaviours.BehaviourManagerImpl]: ************************************************************************************* 2024-05-17 13:28:06,092 ERROR [behaviours.BehaviourManagerImpl]: Script function failed on issue: (create issue) project/issuetype: CT1/Service Request, user: sdhanda5, fieldId: customfield_11258, file: <inline script>, edit behaviour: https://jira-preprod.devops.jlr-apps.com/plugins/servlet/scriptrunner/admin/behaviours/edit/96 java.lang.NullPointerException: Cannot get property 'name' on null object at 87fce3309510e24a49e026e544bee3f9$_run_closure1.doCall(87fce3309510e24a49e026e544bee3f9.groovy:18) at 87fce3309510e24a49e026e544bee3f9.run(87fce3309510e24a49e026e544bee3f9.groovy:17)
Category ID : customfield_11258
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @sanjay
Yes you can.
You need to ensure your behavior config is set for the service desk.
Please double check this and let me know.
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 @
Sorry to trouble you but Still I am getting null pointer exception for below code :
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.