Forums

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

Script runner behaviours - How set field mandatory on edit screen while editing

Laith Ata
Contributor
December 23, 2020

i want to make a custom field based on another custom field mandatory. The code works but while editing the edit screen form the issue the field is not requierd until i submit the changes.

What I want is dynamically to change the field to mandory  while the user ist editing the issue in the edit screen. 

The second example works exactly how I want it but only with a systemfiled. 

My Code: 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FormField




// different ways of getting field values
def checkbox = getFieldById("customfield_10230")
def begruendung = getFieldById("customfield_10231")
def checkboxInhalt = checkbox.getValue()
log.warn("Test1:" + checkboxInhalt.toString() )


// get the options avaliable for that custom field
def customField = customFieldManager.getCustomFieldObject(checkbox.getFieldId())
log.warn("Test2: " + customField.getValue(underlyingIssue).toString().equalsIgnoreCase("ja"))
// def getOptionById = ComponentAccessor.getOptionsManager().getOptions(config).find {it.optionId == checkbox.fieldOptions.size()}


if(customField.getValue(underlyingIssue) != null && customField.getValue(underlyingIssue).toString().equalsIgnoreCase("ja") )
begruendung.setRequired(true)
else if(customField.getValue(underlyingIssue) == null || customField.getValue(underlyingIssue).toString().equalsIgnoreCase("nein") )
begruendung.setRequired(false)

 

Code from an example with issuelink, which works dynamically:

/**
* Make the links field required when there are no existing link and
* the link type is "is part of"
* Video link: https://youtu.be/q4KfHSTdVeA
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink

def links = getFieldById("issuelinks")
def linksValue = links.getValue()
def linkType = getFieldById("issuelinks-linktype")
def linkTypeValue = linkType.getValue()
def linkField = getFieldById("issuelinks-issues")
def linkFieldValue = linkField.getValue()

log.debug("Links Value: "+ linksValue)
log.debug("Link Type Value: "+ linkTypeValue)
log.debug("Links Value: "+ linkFieldValue)

def existingLinks = ComponentAccessor.getIssueLinkManager().getInwardLinks(underlyingIssue?.getId())

if (existingLinks?.size() == 0 && linkTypeValue == "is blocked by") {

links.setRequired(true)

} else {

links.setRequired(false)

}

 

1 answer

1 accepted

2 votes
Answer accepted
Italo Qualisoni [e-Core]
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.
December 23, 2020

Hi @Laith Ata ,

 

I think your issue is that you are getting the value of customfield in the issue instead of using the value of the edit form.

When you use customField.getValue(underlyingIssue) you are getting the field's value of the issue and not from your edit form. In behaviour in order to get the field value's in the edit form you should use the Field.getValue() method, see below a suggestion modification of your first code:

Just a notice that checkbox can have both String and ArrayList types, it depends if the user select one or more options in the form. For this reason we have an condition to check the value type and I get this piece from the below documentation 

https://library.adaptavist.com/entity/control-form-field-based-on-selected-checkbox-value

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FormField


// different ways of getting field values
def checkbox = getFieldById("customfield_10230")
def begruendung = getFieldById("customfield_10231")

def checkboxInhalt = checkbox.getValue()
def begruendungInhalt = begruendung.getValue()

log.warn("checkboxInhalt:" + checkboxInhalt)
log.warn("begruendungInhalt:" + begruendungInhalt)

// seee https://library.adaptavist.com/entity/control-form-field-based-on-selected-checkbox-value
// Collect the selected checkbox values
// If a single item is selected, the checkbox value type will be String
// If more than one option is selected, the checkbox value will be an ArrayList
log.debug("The checkbox field value: '${checkBoxFieldValue}' of type '${checkBoxFieldValue.class.simpleName}'")
def chosenValuesList = []
if (checkboxInhalt in String) {
chosenValuesList.add(checkboxInhalt)
} else if (checkboxInhalt in ArrayList) {
chosenValuesList.addAll(checkboxInhalt)
}


if("ja" in chosenValuesList){
begruendung.setRequired(true)
}
else if("nein" in chosenValuesList){
begruendung.setRequired(false)
}

 

 

Laith Ata
Contributor
December 23, 2020

Thanks! 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events