is it possible make default value of select issue filed when issue changes status and popup screen

Виктория
Contributor
March 20, 2023

is it possible make default value of select issue filed when issue changes status and popup screen coming up?
I tried it in behaviours ... but it doesn`t work

 

 

 

def issue = Issue.getByName('Development type') . transition('Design')


if (issue.getValue() == "") {
issue.setFormValue('Low Code')
}

 

4 answers

1 accepted

0 votes
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.
March 20, 2023

Hi @Виктория

If you intend to set the default value for the list, a sample code is available in the Adaptavist Library.

Below is the sample code for your reference:-

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

@BaseScript FieldBehaviours fieldBehaviours

// a single select list custom field name
final String fieldName = "Select List"

// the value to set
final String setValue = "Some Value"

def field = getFieldByName(fieldName)

def optionsManager = ComponentAccessor.optionsManager
def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issueContext.projectId, issueContext.issueTypeId).find {
  it.name == fieldName
}

assert customField : "Could not find custom field with name $fieldName"

def fieldConfig = customField.getRelevantConfig(issueContext)
def options = optionsManager.getOptions(fieldConfig)
def option = options.find { it.value == setValue }

assert option : "Could not find option with value $setValue"

field.setFormValue(option.optionId)

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

Please also note this should be done using the Behaviour Initialiser.

To do this via Server-Side Behaviour, you must not initialise the field using the Field Name. Instead, it would be best if you declare it as:-

def field = getFieldById(fieldChanged)

This ensures that the Behaviour will only take effect if you have made any changes to that field the Server-Side Behaviour has been configured for.

But since you want to set the default value when the Create or Edit screen is loaded, the Behaviour Initialiser is the best approach.

I hope this helps answer your question. :-)

Thank you and Kind regards,
Ram

Виктория
Contributor
March 20, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 
you lost issue status,
i need it in poop up window when transit from status "design"
you example do it like always when issue create i need this field empty

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.
March 21, 2023

Hi @Виктория

Thank you for providing the clarification.

If you intend to set the value for the Multi-Select List on a Transition Screen, all you have to do is:-

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldByName('Sample Multi List')

if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}

Please note that the sample code above is not 100% exact to your environment. Hence you will need to modify it accordingly.

Before adding the code above, it would be best to do basic logging to determine the action name. You will need to add the logging below:-

log.warn "============>>> ${actionName}"

In my environment, the actionName returns In Progress. Hence, I am using 'In Progress' in the if/else condition shown in the code above.

You must use the Behaviour Initialiser when using the code above for it to work as expected.

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

Below is a screenshot of the workflow that I am testing with:-

workflow1.png

I am also including a couple of these screenshots for your reference:-

1. First, I create a sample issue of Bug-type as shown in the image below:-

test1.png

2. Next, I transition the issue to In Progress, as shown in the image below:-

test2.png

3. When the transition takes place, the dialog pops up, and in it, the Multi-Select List has been set to the default value Sample 1, as expected.

test3.png

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


Thank you and Kind regards,

Ram

 

Виктория
Contributor
March 21, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldByName('Sample Multi List')

if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}


like this it doesn`t work
I do use the Behaviour Initialiser 

where in code i need to put this ?

log.warn "============>>> ${Design}"
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.
March 21, 2023

Hi @Виктория 

In my previous comment, I have already mentioned:-

Please note that the sample code above is not 100% exact to your environment. Hence you will need to modify it accordingly.

You need to modify the code accordingly and not just copy and paste the code.

In my environment, my multi-select field is called Sample Multi Select. You need to change the name according to you environment. Similarly, the multi-select options that I am using is also different,  you will need to modify this as well.

Regarding the log, as I mentioned, before  using the sample code below in the Behaviour Initialiser. Do not change this code.

log.warn "============>>> ${actionName}"

If you try and use the approach below, it will not work

log.warn "============>>> ${Design}"

The screenshot that I have shared in my previous comment explicitly shows where the Behaviour Initialiser is located, i.e.:-

behaviour_config.png

In the screenshot above, I have added the end code after I have determined what is the action name. 

As I mentioned earlier, you will need to first add the log parameter and try to do a basic transition to see what is the actionName that printed out. Once you know what is the action name, you will need to modify the code and update action name condition accordingly.

I suggest reading through the Adapatavist Documentation first to get a better understanding on how to use the Behaviour Initiailser.

Thank you and Kind regards,
Ram

1 vote
Виктория
Contributor
March 21, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

 @Oday Rafeh 

this works! thank you

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldByName('Sample Multi List')

if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}

Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 21, 2023

Greate, very happy to hear that

0 votes
Виктория
Contributor
March 23, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 
can we update code and make a check if field is empty?

 

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldByName('Sample Multi List')

if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}  

 

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.
March 23, 2023

Hi @Виктория

To answer your question, yes you can. You can include an if/else check like:-

multiSelect.clearError()

if (!multiSelect.value) {
multiSelect.setError('Value has not been set')
}

Below is the updated sample code for your reference:-

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldByName('Sample Multi List')

multiSelect.clearError()


if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}  

if (!multiSelect.value) {
multiSelect.setError('Value hoas not been set')
}

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

Thank you and Kind regards,

Ram 

Виктория
Contributor
March 24, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

I just need to check that 

Sample Multi List

has not beet set before, if it already has a value then to do nothing.
rules above not really works  

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.
March 24, 2023

Hi @Виктория

If you want to check whether the field has already been set, then you will need to use

if (multiSelect.value) {
return
}

 Your updated code should be like this:-

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldByName('Sample Multi List')


if (multiSelect.value) {
return
}

if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}  

 

So if the multi-select field already has a value set for it, it will ignore it and not do anything.

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

Thank you and Kind regards,

Ram

Виктория
Contributor
March 24, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 
now it`s ignore this part

if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}  

because if field like empty it`s value "none"  what the code translate

Screen Shot 2023-03-24 at 08.16.10.png

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.
March 25, 2023

Hi @Виктория

Sorry, I missed out the None value.

I have tested the updated code below, and it works as expected:=

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldByName('Sample Multi List')

if (underlyingIssue) {
if (multiSelect.value != [null]) {
return
}

if (actionName == 'In Progress') {
multiSelect.setFormValue(['Sample 1'])
}
}

If any value other than None is set for the Multi-Select list, it will not update. If the value is None, it will automatically set it to Sample 1.

Also,  using the if(underlyingIssue) will ensure that the validation is done once the issue has already been created, i.e. for the Issue transition screen.

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

Thank you and Kind regards,

Ram

0 votes
Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2023

Hi @Виктория , 

Seems to me that you are using Adaptavist ScriptRunner ( Please mention the other name if it's not) 

So if yes you need to create a ScriptRunner behaviour.

I can help you with the steps, if you want help with this let me know. 

 

Regards 

Oday 

Виктория
Contributor
March 20, 2023

@Oday Rafeh 
that is write.
help please

Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2023

Okay fine @Виктория 

The correct approach to set a default value for the 'Development type' custom field during a transition:

  • In Jira, go to 'Manage apps' and then navigate to 'Behaviours' under 'ScriptRunner. '
  • Click on 'Create Behaviour' and give it a name and description.
  • Click on 'Add Mapping' and select the transition screen you want to apply the behaviour to.
  • Add a new server-side script for the 'Development type' field:
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def developmentTypeField = getFieldByName("Development type")

// Replace 'Low Code' with the default value you want to set
def defaultValue = "Low Code"

if (developmentTypeField.getValue() == null) {
developmentTypeField.setFormValue(defaultValue)
}


Save the script and behaviour.

Let me know if this helped.

Виктория
Contributor
March 20, 2023

@Oday Rafeh 

you lost issue status,
i need it in poop up window when transit from status "design"
you example do it like always when issue create i need this field empty

Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2023

@Виктория 

Can you try this : 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def developmentTypeField = getFieldByName("Development type")

// Replace 'Low Code' with the default value you want to set
def defaultValue = "Low Code"

def issue = underlyingIssue
def issueStatus = issue.getStatus().name

// Check if the issue is transitioning from the "Design" status
if (issueStatus == "Design" && developmentTypeField.getValue() == null) {
developmentTypeField.setFormValue(defaultValue)
}
Виктория
Contributor
March 20, 2023

@Oday Rafeh 
looks good but doesn`t work((
ir is select list single choose

Screen Shot 2023-03-20 at 10.30.43.png

Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2023

@Виктория it seems that you want to set the default value only during the transition, not while the issue is in that status. 

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

@BaseScript FieldBehaviours fieldBehaviours

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def developmentTypeField = getFieldByName("Development type")

// Replace 'Low Code' with the default value you want to set
def defaultValue = "Low Code"

def issue = underlyingIssue
def issueStatus = issue.getStatus().name

// Check if the issue is transitioning from the "Design" status
if (getAction()?.name == "your_transition_name") {
if (developmentTypeField.getValue() == null) {
developmentTypeField.setFormValue(defaultValue)
}
}

Can you check this ? 

Виктория
Contributor
March 20, 2023

@Oday Rafeh 
nope... same no results...

Виктория
Contributor
March 21, 2023

@Oday Rafeh 
may be any other options?

Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 21, 2023

Hi @Виктория , Sure I will keep help to we can find a solution 

 

Please try this script : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.workflow.TransitionOptions
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

// Name of the select list custom field
final String fieldName = "Select List"

// Default value to set
final String defaultValue = "Default Value"

def field = getFieldByName(fieldName)

def issue = underlyingIssue
def issueStatus = issue.getStatus().getName()

// Check if the issue is transitioning to a specific status
if (issueStatus == "Target Status Name") {
// Set the default value for the select list field
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName(fieldName)
def fieldConfig = customField.getRelevantConfig(issue.context)

// Find the option for the default value
def options = optionsManager.getOptions(fieldConfig)
def option = options.find { it.value == defaultValue }

// Set the default value for the field
if (option) {
field.setFormValue(option.optionId)
}
}

// Continue with the transition
WorkflowTransitionUtil workflowTransitionUtil = ComponentAccessor.getComponentOfType(WorkflowTransitionUtil.class);
IssueInputParameters issueInputParameters = workflowTransitionUtil.createIssueInputParameters(
issue,
fieldBehaviours.getModifiedFields()
)
TransitionOptions transitionOptions = new TransitionOptions.Builder().skipConditions().skipValidators().build()
workflowTransitionUtil.progress(issue, issueInputParameters, transitionOptions)

 Let me know the results please 

Виктория
Contributor
March 21, 2023

 @Oday Rafeh 
no good results...
and look like a mistakes in code (red lines on right side)

Screen Shot 2023-03-21 at 09.27.06.png

Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 21, 2023

@Виктория 

It looks like the issue is related to unresolved dependencies. Try adding the following import statements at the top of the script:

import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.FieldConfigScheme
import com.atlassian.jira.issue.fields.config.FieldConfigSchemeManager
import com.atlassian.jira.issue.fields.config.manager.FieldConfigManager

Then, modify the code for setting the default value of the select list field as follows: 

// Find the option for the default value
def options = optionsManager.getOptions(customField.getConfigurationSchemes().find {
it.getOneAndOnlyConfig().getId() == fieldConfig.getId()
}.getOneAndOnlyConfig())
def option = options.find { it.value == defaultValue }

// Set the default value for the field
if (option) {
field.setFormValue(option.value)
}
Виктория
Contributor
March 21, 2023

@Oday Rafeh  nope, still doesn`t work...
may be need to create behaviours to the custom screen what I use for transition?

Suggest an answer

Log in or Sign up to answer