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

Scriptrunner escalation service set date field

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

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)

 

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

0 votes
Tarun Sapra Community Leader Feb 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

 
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 Feb 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)

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())

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
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 ...

1,099 views 7 0
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