Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

222 views 2 1
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