Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Select list field is not taking value from Dropdown provided by behaviour

sanjay May 16, 2024

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 :

// Script to set options in Project fields as per Category select
 
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FormField
import com.onresolve.jira.groovy.user.FieldBehaviours
import java.util.Map
import groovy.transform.BaseScript
 
@BaseScript FieldBehaviours fieldBehaviours
 
def optionsManager = ComponentAccessor.optionsManager
def category = getFieldById(getFieldChanged())
def categoryValue = category.getValue().toString()
def projectID = getFieldByName("Project ID")
def projectManager = ComponentAccessor.getProjectManager()
def allProjectCategory = projectManager.getAllProjectCategories()
def projectCategory = projectManager.getProjectCategoryObjectByName(categoryValue)
 
if(projectCategory != null)
{
 
    def projects = projectManager.getProjectsFromProjectCategory(projectCategory)
   
    def List = []
   
    if(projects != null || projects.size() > 0)
    {
   
        projects.each { it ->
       
            List.add(it.getName())
       
        }
       
        Map fieldOptions=[:]
       
        fieldOptions = List.collectEntries{ [(it): it] }
       
        FormField projectId = getFieldByName("Project ID")
       
        projectId.setFieldOptions(fieldOptions)

        log.warn("Project ID Field Value = "+projectId.getValue().toString())
       
        projectID.clearError()
       
    }

else
{
    projectID.setError("There is no Project with selected Category")
MicrosoftTeams-image.png

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2024

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

 

 

 

 

sanjay May 17, 2024

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

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2024

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

sanjay May 17, 2024

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

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2024

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:-

behaviour_config.png

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.

test1.png

test2.png

2. If Sprint Project is selected from the Project Category list, only the Mock option is available in the Project Names list.

test3.png

 

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.

test4.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Senior Support Engineer

sanjay May 17, 2024

Hi @

I will try this and will get back you.

Thanks a million

sanjay May 17, 2024

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

sanjay May 17, 2024

MicrosoftTeams-image (1).pngI am trying it on the portal

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2024

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

Like sanjay likes this
sanjay May 17, 2024

Hi @



Sorry to trouble you but Still I am getting null pointer exception for below code :

import com.adaptavist.hapi.jira.projects.Projects
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def projectCategory = getFieldByName("Category")
def projectCategoryValue = projectCategory.value

log.warn(projectCategoryValue)

def projectNames = getFieldByName("Project ID")
log.warn(projectNames) // Rename the field accordingly
def projectNameOptions = ['Test', 'Pillar 5 Capabilities','AAD Programme Delivery','Commercial Commit ART Level'] // 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)

log.warn(projectNameOptions)

 

0 votes
sanjay May 17, 2024

Hi Sir

You are the best finally I can complte it.

Thanks a lot for your precious time and help.

God Bless you Sir.

Sanjay

TAGS
AUG Leaders

Atlassian Community Events