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

Testing a checkbox value

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

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
TAGS
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

3,888 views 11 5
Join discussion

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