Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,490
Community Members
 
Community Events
168
Community Groups

Behavior script, value defaults to None in Edit Issue screen which is not as expected

In the code below, everything is working fine except the last option. 

In the last option the in spite of the value I choose from dropdown, it defaults to 'None' on the Edit Issue screen.

Please help to figure out why

 

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

import com.onresolve.jira.groovy.user.FormField

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Option
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Calendar


// @BaseScript FieldBehaviours fieldBehaviours
class E2ETFieldBehaviors extends FieldBehaviours {


FormField getTestStageCf() {
getFieldById("customfield_18904")
}


FormField getReleaseDateCf() {
getFieldById("customfield_21901")
}


void checkReleaseDate() {
if (!isBug()) {
return
}
def releaseDateCf = getReleaseDateCf()
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd")
Date releaseDate = formatter.parse(releaseDateCf.getValue() as String)

def currentDate = new Date()
def cal = Calendar.getInstance()
cal.setTime(releaseDate)
cal.add(Calendar.DATE, -2)
def releaseDateMinus2 = cal.getTime()

def cal1 = Calendar.getInstance()
cal1.setTime(releaseDate)
cal1.add(Calendar.DATE, 30)
def releaseDatePlus30 = cal1.getTime()

// ***** Setting up control on service field dropodown control ******
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def testStageCf = getTestStageCf()
def customField = customFieldManager.getCustomFieldObject(testStageCf.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

final normal = options.collectEntries { [(it.optionId.toString()): it.value] }

final before2 = options.findAll { it.value in ['0-ReqReview', '1-UNIT', '10-PRE-INT',
'2-SYST','3-INT','4-UAT','6-LUT','8-TRN',
'9-CAT']
} .collectEntries { [(it.optionId.toString()): it.value] }
final after30 = options.findAll { it.value in ['5-Warranty','7-PROD'] }
.collectEntries { [(it.optionId.toString()): it.value] }

if (currentDate.after(releaseDatePlus30)) {
testStageCf.setFieldOptions(["-1": "None"] +after30)
//testStageCf.setFormValue(null)
} else if (currentDate.before(releaseDateMinus2)) {
testStageCf.setFieldOptions(["-1": "None"] + before2)
//testStageCf.setFormValue(null)
} else {
testStageCf.setFieldOptions(["-1": "None"] + normal)
//testStageCf.setFormValue(null)
}

}

}

1 answer

1 accepted

0 votes
Answer accepted

Hi @Vidhya Mohan , what do you mean by "last option"? Can you provide better description and screenshots so we have better chance to understand the issue, please? :)

@Martin Bayer _MoroSystems_ s_r_o__ , by last option I meant the highlighted part of code

if (currentDate.after(releaseDatePlus30)) {
testStageCf.setFieldOptions(["-1": "None"] +after30)
//testStageCf.setFormValue(null)
} else if (currentDate.before(releaseDateMinus2)) {
testStageCf.setFieldOptions(["-1": "None"] + before2)
//testStageCf.setFormValue(null)
} else {
testStageCf.setFieldOptions(["-1": "None"] + normal)

When it is not release date plus 30 or release date -2, the code displays all options for Test Stage (phase). So when that option is called, all options are getting displayed and I'm able to create issue. but on the edit screen, it again defaults to 'None' (and not the option I chose on 'Create' issue screen

4-UAT.pngUAT2.png

Hi @Vidhya Mohan , it is clear for me now. Script looks correct to me. Could you log values in the variables 

  • normal
  • after30
  • before2

with

log.error normal
log.error after30
log.error before2

@Martin Bayer _MoroSystems_ s_r_o__ , the issue is resolved. 

WOrking code

void checkReleaseDate() {
if (!isBug()) {
return
}
def releaseDateCf = getReleaseDateCf()
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd")
Date releaseDate = formatter.parse(releaseDateCf.getValue() as String)

def currentDate = new Date()
def cal = Calendar.getInstance()
cal.setTime(releaseDate)
cal.add(Calendar.DATE, -2)
def releaseDateMinus2 = cal.getTime()

def cal1 = Calendar.getInstance()
cal1.setTime(releaseDate)
cal1.add(Calendar.DATE, 30)
def releaseDatePlus30 = cal1.getTime()

// ***** Setting up control on service field dropodown control ******
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def testStageCf = getTestStageCf()
def customField = customFieldManager.getCustomFieldObject(testStageCf.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

final normal = options.collectEntries { [(it.optionId.toString()): it.value] }

final before2 = options.findAll { it.value in ['0-ReqReview', '1-UNIT', '10-PRE-INT',
'2-SYST','3-INT','4-UAT','6-LUT','8-TRN',
'9-CAT']
} .collectEntries { [(it.optionId.toString()): it.value] }
final after30 = options.findAll { it.value in ['5-Warranty','7-PROD'] }
.collectEntries { [(it.optionId.toString()): it.value] }

def changeOptions = normal
if (currentDate.after(releaseDatePlus30)) {
// testStageCf.setFieldOptions(
changeOptions = ["-1": "None"] +after30 //)
log.error "SetFieldOption after30"
log.error after30
//testStageCf.setFormValue(null)
} else if (currentDate.before(releaseDateMinus2)) {
// testStageCf.setFieldOptions(
changeOptions = ["-1": "None"] + before2 //)
log.error "SetFieldOption before2"
log.error before2
//testStageCf.setFormValue(null)
} else {
//testStageCf.setFieldOptions(
changeOptions = ["-1": "None"] + normal //)
log.error "SetFieldOption normal"
//testStageCf.setFormValue(null)
log.error normal
}
def value = testStageCf.getFormValue()
testStageCf.setFieldOptions(changeOptions)
//if (!(value in changeOptions.collectEntries{ it.value} )) {
// value = 'None'
//}
testStageCf.setFormValue(value)

Great :) Did logging help you? What was the issue? :)

Yes it did help indirectly :) . The issue was setFieldOptions.. it was changing the value on edit screen.. 

testStageCf.setFormValue(value) - this piece of code keeps the value as is. 

thank you for your help

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

196 views 1 6
Read article

Atlassian Community Events