Testing a checkbox value

Roy Chapman April 9, 2019

I'm confused by this logic. I have an Anywhere to Itself transition to set a field "Escalation Required".  This is a checkbox field and has a single value of Yes (it can also be clear I suppose, so no value is the second value).

The idea is that if "Escalation Required" is set to Yes then I set the Date Escalated field.  If unchecked (there is only a Yes option) then the Date Escalated field is cleared.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import java.sql.Timestamp

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()

//name of the custom field
def customFieldName = 'Escalation Required'
//get the custom field object
def customFieldObject = customFieldManager.getCustomFieldObjectByName(customFieldName)

Issue issue = issue
//get all the values of the custom field
def customFieldValues = customFieldObject.getValue(issue)

def dateCf = customFieldManager.getCustomFieldObject("customfield_17001") // Date Escalated

if (customFieldValues == null) {
log.debug "Escalation Required NOT set to Yes set to " + customFieldValues
issue.setCustomFieldValue(dateCf,null)
} else {
log.debug "Escalation Required set to Yes set to " + customFieldValues
issue.setCustomFieldValue(dateCf, new Timestamp((new Date()).time))
}

The debug logs show that the value of Escalation Required is correctly detected, so the following logic (to set/clear Date Escalated) is executed.  The problem is that I have to select twice (i.e. transition twice) for the date to be updated/cleared.

What am I doing wrong?

Roy

1 answer

1 accepted

1 vote
Answer accepted
Roy Chapman April 9, 2019

I have managed to fix this problem myself.  The problem was I was testing value of Escalation Required before this step

Update change history for an issue and store the issue in the database

So I moved afterwards, now I can test on the field.  This is the changed script.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import java.sql.Timestamp
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def customFieldManager = ComponentAccessor.getCustomFieldManager()

//name of the custom field
def customFieldName = 'Escalation Required'
//get the custom field object
def customFieldObject = customFieldManager.getCustomFieldObjectByName(customFieldName)

Issue issue = issue
//get all the values of the custom field
def customFieldValues = customFieldObject.getValue(issue)

def dateCf = customFieldManager.getCustomFieldObject("customfield_17001") // Date Escalated

if (customFieldValues == null) {
log.debug "Escalation Required NOT set to Yes set to " + customFieldValues
issue.setCustomFieldValue(dateCf,null)
} else {
log.debug "Escalation Required set to Yes set to " + customFieldValues
issue.setCustomFieldValue(dateCf, new Timestamp((new Date()).time))
}

log.debug "Set Date Escalated to " + issue.getCustomFieldValue(dateCf)
def myVal = issue.getCustomFieldValue(dateCf)

def changeHolder = new DefaultIssueChangeHolder()
dateCf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(dateCf), myVal),changeHolder)

Suggest an answer

Log in or Sign up to answer