Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

When I use the API setFieldOptions(...) for a Custom Version Picker Field - previous value is delete

Eran Flom January 9, 2022

I have a custom field of type Version Picket (multiple values).

I have a behaviour that initiate the options for this field (for example when you edit the ticket).

The possible options for that field are versions from another Jira project - this is working fine.

The problem is when you have value in this field and edit the ticket again - the previous value of the field is being deleted.

Why?

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 9, 2022

Hi @Eran Flom

Could you please specify which version of ScriptRunner you are using when testing this?

Also, could you please share your behaviour configuration and code for better understanding?

Thank you and Kind Regards,

Ram

Eran Flom January 9, 2022

I am using Adaptavist ScriptRunner for JIRA version 6.41.0 (on a Jira server v8.16.0)

 

The behaviour is set with all the default values (Use Validator Plugin is unchecked and there is no Guide workflow).

The code is in the Initialiser (to be activated when the edit screen is entered). This is the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.project.version.Version

// Specify the master project - to get the versions from
def projectManager = ComponentAccessor.getProjectManager()
def project = projectManager.getProjectObjByName("Proj A")

// Get all the vesrions from "Proj A"
def versionManager = ComponentAccessor.getVersionManager()
def List<Version> versions = versionManager.getVersions(project)

def versionOptionsFld = getFieldByName("Version Picker Field Name")
versionOptionsFld.setFieldOptions(versions)

Eran Flom January 17, 2022

@Ram Kumar Aravindakshan _Adaptavist_ any insight about this problem?

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 27, 2022

Hi @Eran Flom

Sorry for the delay in responding.

After going through your code, I found out why the version picker keeps resetting. You have not set any conditions to control when the version picker is reset.

def versionOptionsFld = getFieldByName("Version Picker Field Name")
versionOptionsFld.setFieldOptions(versions)

What this does is then whenever you are in either the create screen or edit screen, the value for the Version Picker will automatically be reset to the original value.

To control when the Version Picker options are reset, you should include an if condition. For example:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def projectManager = ComponentAccessor.projectManager
def versionManager = ComponentAccessor.versionManager
def project = projectManager.getProjectByCurrentKey('BT')
def versions = versionManager.getVersions(project)

def versionOptionsFld = getFieldByName("Multi Version Picker")

if(versionOptionsFld == null) {
versionOptionsFld.setFieldOptions(versions)
}

Please note the working sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

If you notice in the sample code above, the if condition is added to control when the options in the version picker are reset, i.e. 

if(versionOptionsFld == null) {
versionOptionsFld.setFieldOptions(versions)
}

In other words, if the version picker has some options already selected, it will ignore it.

Please give this a try and see if it helps resolve your problem.

Thank you and Kind Regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 1, 2022

Hi @Eran Flom

Does the suggested solution help to solve your issue?

If yes, please accept the answer.

Thank you and Kind Regards,

Ram

Eran Flom February 1, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Sorry, it took me a couple of days to try this.

I tried your suggestion. What happen is that I replaced one problem with another...

Let me explain:

  1. The purpose of this code is to provide a version picker field in tickets from project A - that allows selecting versions from project B
  2. When I am using setFieldOptions - than I get the versions from project B - but than the field content is deleted every time I try to edit it.
  3. When I am adding the condition you suggested - the field content is not cleared - but the list of possible versions is from Project A (and I need it to be from project B).

So, the bottom line is that I still don't have a solution.

I thought that setFieldOptions is just setting the possible options for the field - but doesn't change it's value (even if it is not empty). Not sure what to do now...

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 1, 2022

Hi @Eran Flom

Could you please share the updated code that you are using? It would be beneficial if you could provide a GIF file to explain your issue.

Also, I need to clarify your last comment; for your version picker for Project A, are you trying to add versions from Project A and Project B or Project B alone?

Thank you and Kind Regards,

Ram

Eran Flom February 15, 2022

@Ram Kumar Aravindakshan _Adaptavist_ sorry for the late response (was sick last week...)

The updated code is here:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours


// Specify the master project - SpecV - to get the versions from
def projectManager = ComponentAccessor.getProjectManager()
def project = projectManager.getProjectByCurrentKey("SV")

// Get all the vesrions from SV
def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(project)

def versionOptionsFld = getFieldByName("CCUC Current Relevant VRMF min Version")
if(versionOptionsFld == null) {
        versionOptionsFld.setFieldOptions(versions)
}

The behaviour of the field in this case is that on first access to the field (create screen) - it will ask me to select a version from project SV (project B), but on the second attempt to update the field, it will:

  • Show an empty field (meaning it will not preserve the previous value, and...
  • Show me the list of version from project A

If I don't use the if condition, it will always show me the version list of Project SV (B) - as wanted, but it will also clear the field (not wanted)

Just to clarify, I always want to see as option versions from project B only (even if the ticket is created in project A)

Pleas clarify which gif do you want me to add.

Thank you

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 16, 2022

Hi @Eran Flom

Thank you for providing the clarification.

From your requirement, the approach you are taking will not work. If you intend to get the versions from Project B and display them in Project A, you will need to use a REST Endpoint to first invoke the versions from Project B.

Please note the sample working codes provided are not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a sample working code for the REST Endpoint:-

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate
convertToJson { MultivaluedMap queryParams ->

def hostUrl = "http://localhost:9091"
def userName = "admin"
def password = "q"
def projectKey = "BT"

def httpBuilder = new HTTPBuilder(hostUrl)

def issueJson = httpBuilder.request(Method.GET, ContentType.JSON) { req ->

uri.path = "/rest/api/2/project/${projectKey}/versions"

headers.'Authorization' = "Basic ${"${userName}:${password}".bytes.encodeBase64()}"

response.failure = { resp, reader ->
log.warn "Failed to query JIRA API: ${reader.errorMessages}"
return
}
}

def issueMap = [
items : issueJson.collect { Map row ->
[
value: row.get("name"),
html : row.get("name"),
label: row.get("name")
]
}
]

return Response.ok(new JsonBuilder(issueMap).toPrettyString()).build()
}

 Below is a print screen of the REST Endpoint configuration:-

rest_config.png

Below is a print screen of the output from the REST Endpoint:-

rest_output.png

 

Once you have setup the REST Endpoint for Project B, you need to pass the value to a Behaviour to display the value in Project A.

Since you indeed to pass to a multi-select list, you can follow the example below, i.e. convert a single-line text field into a multi-select list. Please note you will need to use the Behaviour Initialiser and not the Server-Side Behaviour for this.

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def linkedProjectVersions = getFieldByName("Multi Version Picker")

linkedProjectVersions.convertToMultiSelect([
ajaxOptions: [
url : "${baseUrl}/rest/scriptrunner/latest/custom/convertToJson",
keyInputPeriod: 500,
formatResponse: "general"
]
])

Below is a print screen of the Behaviour configuration:-

behaviour_config.png

Below is a print screen of how the options are now displayed in Project A's create screen:-

create_screen.png

Below are how the options are displayed in the Edit screen:-

edit_screen.png

Also, if there are any updates of the versions, i.e. added, removed, released or archived in Project B, it will be updated and displayed in Project A accordingly.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer