Scriptrunner escalation service set date field

Tom Lister February 20, 2017

Hi

I'm struggling to find a syntax that will update a custom date field using scriptrunner escalation service

my code is 

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.id == 'customfield_13202'}
Date curr = new Date()
Calendar cal = Calendar.getInstance()
cal.setTime(curr)
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
String dateFormat = "d/MMM/yy"
try {
issueInputParameters.addCustomFieldValue(cf.id, new java.text.SimpleDateFormat(dateFormat).format(cal.getTime()))
} catch (Exception e) {
log.debug(e.getMessage())
}

The date format is accepted by date field in edit. I can set fields like description, Field id is correct

debugging doesn't highlight an issue

log.debug("cal 1 " + cal.getTime())
log.debug("cal 2 " + new java.text.SimpleDateFormat(dateFormat).format(cal.getTime()))
log.debug("field " + cf.id +"/"+ cf.getIdAsLong())

->

[c.onresolve.jira.groovy] 
[c.onresolve.jira.groovy] cal 1 Mon Feb 20 11:47:36 GMT 2017
[c.onresolve.jira.groovy] cal 2 20/Feb/17
[c.onresolve.jira.groovy] field customfield_13202/13202
[c.onresolve.jira.groovy] [20/Feb/17]

has anyone got this to work. I've seen some similar posts but no conclusive approach

Tom

2 answers

1 accepted

1 vote
Answer accepted
Tom Lister February 20, 2017

I have a solution

Putting the field onto the edit screen allowed it to be updated

my code was

issueInputParameters.skipScreenCheck()

which I thought bypassed the need to put the field on a screen.

Should have been

issueInputParameters.setSkipScreenCheck(true)

 

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2017

Yes - the first format just returns whether skip screen check is set. 

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 20, 2017

Hello Tom,

Can you try this format for dateFormat

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy")

This should give the same o/p while debugging - 20/Feb/17 and should do the trick

 
Tom Lister February 20, 2017
none of the formats I've tried are updating the customf field
dd/MM/yyyy has debug

[c.onresolve.jira.groovy] cal 1 Mon Feb 20 14:21:11 GMT 2017
[c.onresolve.jira.groovy] cal 2 20/02/2017
[c.onresolve.jira.groovy] field customfield_13202/13202
[c.onresolve.jira.groovy] [20/02/2017]

dd/MMM/yyy has debug

[c.onresolve.jira.groovy] cal 1 Mon Feb 20 14:26:40 GMT 2017
[c.onresolve.jira.groovy] cal 2 20/Feb/2017
[c.onresolve.jira.groovy] field customfield_13202/13202
[c.onresolve.jira.groovy] [20/Feb/2017]

if i put a bad date in the edit field i will get the message

"You did not enter a valid date. Please enter the date in the format "d/MMM/yy", eg. "20/Feb/17"

and the debug for that format is

[c.onresolve.jira.groovy] cal 1 Mon Feb 20 14:35:52 GMT 2017
[c.onresolve.jira.groovy] cal 2 20/Feb/17
[c.onresolve.jira.groovy] field customfield_13202/13202
[c.onresolve.jira.groovy] [20/Feb/17]

I think there is an error somewhere that is not reported. I'll see if I can turn some JIRA logging onto DEBUG for more info

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 20, 2017

Just a hunch,

in the method "addCustomFieldValue"  instead of cf.id did you try, cf.getIdAsLong() , as your method expects "fullCustomFieldKey" -->> addCustomFieldValue(String fullCustomFieldKey, String... values)

 and I am not sure if cf.id returns full custom field key. thus better to use the method

 

addCustomFieldValue(Long customFieldId, String... values)
Tom Lister February 20, 2017

yes i've tried getId, getName, getIdAsLong

debug line [c.onresolve.jira.groovy] field customfield_13202/13202 

is from log.debug("field " + cf.id +"/"+ cf.getIdAsLong())

Alex Gallien November 4, 2018

Jumping into an old thread in case it helps someone in the future. The way I was ultimately able to work around this was by ending my code with:

def update = issueService.validateUpdate(user, issue.id, issueInputParameters)

log.info(update.getErrorCollection().toString())

if (update.isValid()) {
issueService.update(user, update)
}

 this returned

INFO : Errors: {customfield_27220=Invalid date format. Please enter the date in the format "dd/MMM/yy h:mm a".}
Error Messages: []

So I knew I needed to use SimpleDateFormat("dd/MMM/yy h:mm a") for my date. Hope this helps someone!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events