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

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

Vidhya Mohan March 7, 2021

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
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 7, 2021

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? :)

Vidhya Mohan March 7, 2021

@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

Martin Bayer _MoroSystems_ s_r_o__
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 8, 2021

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
Vidhya Mohan March 10, 2021

@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)

Martin Bayer _MoroSystems_ s_r_o__
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 10, 2021

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

Vidhya Mohan March 10, 2021

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

TAGS
AUG Leaders

Atlassian Community Events