How do I set a custom field with the current date/time in a post function?

Randy
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.
November 17, 2016

Hi Everyone,

We're on JIRA cloud and are trying out the ScriptRunner addon.  Is it possible to set a custom date picker field with the current date/time in a workflow transition's post function?  If so, how would I do so?

 

7 answers

1 accepted

3 votes
Answer accepted
Geoff Wilson
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.
November 17, 2016

By playing around with a test workflow, I think I found what you need:

  1. Decide which workflow transition you want this to take effect (perhaps all of them)
  2. Select the transition and open its Post Functions
  3. Click the "Add Post Function" button on the top right of this table
  4. Choose "Update Issue Custom Field" and click Add
  5. From the Issue Custom Field dropdown, select your date field
  6. In the Custom Field Value text area, enter %%CURRENT_DATETIME%% and click Add
  7. Publish your workflow


I have not tested this, but I'm trusting what I read on screen on Step 6 as JIRA shows:

Please make sure that the value you enter is valid for custom field its datatype
and context configuration for the project using this workflow.
Otherwise, the transition may fail at execution time.
Additionally to enter text or numbers as value, you may use the following:

  • You may use macro '%%CURRENT_USER%%' to insert the function caller.
  • You may use macro '%%CURRENT_DATETIME%%' to insert the current date and time.
  • You may use macro '%%ADD_CURRENT_USER%%' to append the function caller. Obsolete please use append option instead with user macro above.
  • For Cascading Select fields, you may either use the value of the option to be selected, no need to add the parent for childs. Or simply enter the ID of the option to be selected.

 

Randy
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.
November 21, 2016

This turned out to be the solution!  Thanks!

Dani Miles February 25, 2020

This didn't work for me I just get an error saying unable to update custom field. 

I have tried using a date picker and a single line text field. Neither work?

Lexi Pike November 25, 2020

Hi. Does anyone know of a way to do this for a future date. I want to automatically populate with current date + 2 weeks upon transition. Thanks. 

Like # people like this
Randy
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.
November 25, 2020

@Lexi Pike you would have to do it with a scripted post function with something like script runner

Lexi Pike November 25, 2020

Thanks @Randy I am not very technical so think we will live with populating the field manually for now! appreciated.

Randy
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.
November 25, 2020

@Lexi Pike Just occurred to me that you can probably use the jira automation feature to set it if your instance has it available or if you're on cloud.  It allows for smart dates that you can assign offset dates to fields on.  It's slightly technical but all point and click with text boxes to do.

Like # people like this
Lexi Pike December 29, 2020

@Randy I've finally found time to look into this - and have successfully added an automation rule that does the job for me. Thanks! :)

{{now.plusDays(14)}}

Like # people like this
Marcel Kleinlütke November 12, 2021

Trying to figure out how this could work with creating Issues through an URL/LINK
No problems to select the project or the issuetype but it is tricky to set the due date to today or tomorrow or today plus 10 days...

Rob Sargent March 22, 2022

Step 4 mentions "Update Issue Custom Field".  That's not available on our Community server-based version, or is perhaps a configuration issue?  I see only "Update Issue Field" (desc: Updates a simple issue field to a given value) and I would like to edit a custom field (date picker) and the custom field does not appear in the drop down of applicable fields.

Like Amin Faridi Rad likes this
1 vote
Marco Hödtke February 15, 2018

Thank you Jon! As I am still new to ScriptRunner: What would be the correct way to update a custom field in a scripted post-function when using a Jira Server?

Randy
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 15, 2018

Marco, refer to Vasiliy Zverev 's answer in this thread for the server solution.

Marco Hödtke February 20, 2018

THX, Randy, I tried it and it works!

David Mayer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 11, 2018

What do you enter for Timestamp?

1 vote
Vasiliy Zverev
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.
November 17, 2016

Here is code for custom script post-function:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest

import java.sql.Timestamp

issue.setCustomFieldValue(
        ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("customFieldname")
        , new Timestamp()
)

//only for create transition
/*
ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        , issue
        , UpdateIssueRequest.builder().sendMail(false).eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).build()
)
*/
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.
November 17, 2016

This answer is for server, not cloud wink

But for server, you should not need the commented out bit even on create.

Like Heather R likes this
Krishnan Rajagopalan March 4, 2020

The custom field updation using the post function as depicted above works for cloud JIRA too.   

Josef Zahlauer January 26, 2022

Hello Vasiliy_Zverev,

Sorry, what's wrong?

image.png

image.png

0 votes
Manrique Soto September 4, 2020

How do I know what is the process flow I'm working on . ? I'm using a nextgen project but now sure what is the workflow I;m using. 

0 votes
Vinay Ravi May 29, 2018

@Jon Bevan [Adaptavist]

Hi,

I dont have script runner.

I want only the month to be updated in the custom field. Is there any macro similar to %%CURRENT_DATETIME%% available for the same.

Could there be any solution, either a direct macro for month, or to extract only the month part from the datetime, and populate it to another custom field.

Thanks,
Vinay 

Randy
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.
May 30, 2018

What you want to do isnt possible without add-on's

0 votes
Vinay Ravi May 29, 2018

Hi,

I want only the month to be updated in the custom field. Is there any macro similar to %%CURRENT_DATETIME%% available for the same.

Thanks,
Vinay 

Randy
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.
May 30, 2018

There isnt.

0 votes
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.
November 17, 2016

Edit your workflow, add new post-function on the action of type "Script Post-function".

Choose "Modify Issue" function.

Enter the following code:

issueInput.fields.customfield_10132 = new Date().format("yyyy-MM-dd")

where 10132 is the ID of the custom field you want to update.

Jon Bevan [Adaptavist]
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.
November 17, 2016

You'll be better off using a ZonedDate to make sure you get the date in the correct timezone:

issueInput.fields.customfield_10132 = ZonedDateTime.now(ZoneId.of("UTC")).format(DateTimeFormatter.ISO_LOCAL_DATE)
Randy
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.
November 21, 2016

I'm getting this error now.  Is there any way to set this field without having to surface it on my screens?  I'd rather not allow users to change this value.

 

2016-11-21 19:09:28,388 INFO - Serializing object into 'interface java.util.Map'
2016-11-21 19:09:28,390 INFO - PUT /rest/api/2/issue/80258 asObject Request Duration: 326ms
2016-11-21 19:09:28,403 ERROR - assert resp.status == 204
       |    |      |
       |    400    false
       status: 400 -` Bad Request
       body: [errorMessages:[], errors:[customfield_15500:Field 'customfield_15500' cannot be set. It is not on the appropriate screen, or unknown.]]
Marco Hödtke February 14, 2018

I use version 3.0.6 of ScriptRunner. The above mentioned script fragment 

issueInput.fields.customfield_11100 = new Date().format("yyyy-MM-dd")

didn't throw an error but it did not have any visible result. Does it work for my companies old version of ScriptRunner? 

Jon Bevan [Adaptavist]
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 14, 2018

Marco - that code snippet will only work for Jira Cloud.

If you have version 3.0.6 of ScriptRunner then you are using Jira Server.

Like Igor_Brazhevskiy likes this
Jon Bevan [Adaptavist]
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 14, 2018

Randy - apologies for the extremely slow reply but yes you can.

The Jira Cloud REST API docs state you can use the overrideScreenSecurity query parameter as long as your script is being executed by the ScriptRunner Add-on user.

Randy
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 14, 2018

Thanks Jon!

Suggest an answer

Log in or Sign up to answer