Issue or Project context in Custom Picker Scripted Field?

For a Custom Picker scripted field is there any way to access the issue or project context of the field, similar to the issue which is available in a standard scripted field?

The following code (or similar for an issue parameter) works fine in isolation but I can’t get it “connected” to the issue/project in the field. I could, possibly, set the project key as a parameter, but was hoping to avoid it

    /**
     * Filter a given project's list of components by locating components with a given {prefix}
     * @param descriptionPrefix The description prefix (inside the {})
     * @param project A project object whose components will be filtered (defaults to the ST project if null)
     * @return A filtered list of the project's components
     */ 
    static def filterComponentsByProject(String descriptionPrefix, Project project) {
        def projectManager = ComponentAccessor.getProjectManager()
        def componentManager = ComponentAccessor.getProjectComponentManager()

        project = project ?: projectManager.getProjectByCurrentKey("ST")

        def components = componentManager.findAllForProject(project.id).findAll {
            it.description?.startsWith("{${descriptionPrefix}}")
        }

        return components.collect {
            [
                value: it.id.toString(),
                displayName: it.name
            ]
        }
    }

Hi.
Inside the scripted field, you have those variables in the context:

Based on that variables, you will be able to obtain the project of the issue
https://docs.atlassian.com/software/jira/docs/api/9.5.1/com/atlassian/jira/issue/Issue.html

And the information about the custom field context: CustomField (Atlassian Jira - Server 9.5.1 API)

Those links that I send are the definition of the classes of the context variables issue and customField.

Let me know if this helps.

Thanks, Ivan.

For a plain “Scripted Field” that is correct, yes, but for a Custom Picker scripted field the list is as follows:

Okey is a little tricky but I will show you how I do it.

As you can check, in this page there is a section called ‘Definitions’ where you can see which how the closures are defined. Also this example is available in the snippets of the custom picker.

Inside those closures, there are two of them that could receive an Issue object, those are search, and toOption. Using those closures allows you to get the Issue object of the current issue, so are able to get the project or any information that you need.

This is a silly example of what I say.

def issueKey

search = { String inputValue, Issue issue ->
    issueKey = issue.key
    userSearchService.findUsers(inputValue, userSearchParams)
}

renderItemViewHtml = { ApplicationUser user ->
    "user.displayName - $issueKey"
}
1 Like