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?
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
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)
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Eran Flom
Does the suggested solution help to solve your issue?
If yes, please accept the 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.
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:
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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_ 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:
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
Below is a print screen of the output from the REST Endpoint:-
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:-
Below is a print screen of how the options are now displayed in Project A's create screen:-
Below are how the options are displayed in the Edit screen:-
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
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.