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

Update field as mandatory based on previous field using Scruipt runner

Suresh Srini October 8, 2020

I am new to Script Editor. I added the script, but doesn't look like it is working.

I added new behavior, added the field(radio button) and added Service side script. My environment is JIRA server. Any help is much appreciated. I am missing any configuration setting for the 1st time use?

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.priority.Priority
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def CauseCodeField = getFieldByName("TEST SK DropDown Field")
def CurrentField = getFieldById(getFieldChanged())

def selectedOption = CurrentField.getValue() as String
def isYesSelected = selectedOption == "Yes"

CauseCodeField.setRequired(isYesSelected)

2 answers

0 votes
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 9, 2020

Hi @Suresh Srini

I recently encountered such situation. when we add behaviour for checkbox/radio button or select list types it works with selected value ID instead of name(not sure the situation is same for you too)

if so you can modify your script as below

def required= getFieldById(getFieldChanged()).formValue as String == "12345" 
//here 12345 is ID of the value "Yes". you can get this ID in URL tab by editing the value in field context


def CauseCodeField = getFieldByName("TEST SK DropDown Field").setRequired(required)

 

BR,

Leo

Suresh Srini October 12, 2020

Thanks Leo. I tried this as well. But not seems to be working.

Something else blocking it not to work.

0 votes
Raziman Dom - Ricksoft
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 8, 2020

Your scripts look valid, try to print the log to check it:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.priority.Priority
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def CauseCodeField = getFieldByName("TEST SK DropDown Field")
def CurrentField = getFieldById(getFieldChanged())

def selectedOption = CurrentField.getValue() as String
def isYesSelected = selectedOption == "Yes"

log.warn "CurrentField: " + CurrentField
log.warn "selectedOption: " + selectedOption
log.warn "isYesSelected: " + isYesSelected

CauseCodeField.setRequired(isYesSelected)

Check your atlassian-jira.log if the value is true. You can read more here how to get the log file. https://confluence.atlassian.com/adminjiraserver/logging-and-profiling-938847671.html

Suresh Srini October 9, 2020

Thank you. Let me check the log.

Since its the Radio button, the behavior is expected to work when the value in the field is changed I assume.

Since this is new installation of Plug-in, is there any 1st time settings needs to be done so this works for my project and in my environment?

Thanks in advance for your help and time.

Raziman Dom - Ricksoft
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 9, 2020

Since its the Radio button, the behavior is expected to work when the value in the field is changed I assume.

By default the radio button's value is None. Make sense why you need to change the radio button field value to "Yes" to make it required.

What is your requirement?

Suresh Srini October 12, 2020

Hi, When the value "Yes" is selected, I like to have next field ( dorp down type field) as mandatory. Default value for the drow down field is 'none'.

Suresh Srini October 12, 2020

Also, appreciate if you can let me know how do I search the log file.

Could not see any data logged in the log file. When I search, I could not see it.

Raziman Dom - Ricksoft
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 12, 2020

This is the script that I use and tested, you can change is a per your requirement:

/**
* Set custom field hidden/required
*/

def changedCF = getFieldById(getFieldChanged()) // Radio button A
def custField1 = getFieldById("customfield_10115") // num1 field
def changedCFValue = changedCF.getValue() as String

//Check if not null and equal to "No"
if (changedCFValue && changedCFValue.equalsIgnoreCase("No")) {

//Set hidden, not required, and null value, if value equal to "No"
custField1.setHidden(true)
custField1.setRequired(false)
custField1.setFormValue(null)

//Other than "No"
} else {

//Set not hidden, required, if value not equal to "No"
custField1.setHidden(false)
custField1.setRequired(true)

}

Screenshot 2020-10-13 at 8.39.22 AM.png

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events