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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

582 views 15 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you