Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Update Date/Time field value on cloud using in build function

Kumar, G Sunil October 20, 2025

Hello Everyone,

 

We’re trying to update a custom field by adding 2 days to the current timestamp using a post function. We cannot use JMWE or Automation for Jira because the field becomes read-only immediately after the transition, which causes delays in execution.

We’ve noticed that using %%CURRENT_DATETIME%%+2d sets the value instantly during the transition but isn't wokring. Is there a recommended way to use this expression in a post function to ensure the field is updated before it becomes read-only?

 

 

4 answers

0 votes
Kumar, G Sunil October 20, 2025

I will try both methods. Hopefully there is no delay when using SR and field is updated immediately.

 

Regards,
Sunil

Gor Greyan
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.
October 20, 2025

Hi @Kumar, G Sunil

After checking, please let us know.

0 votes
Gor Greyan
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.
October 20, 2025

Hi @Kumar, G Sunil

Please try the following script in the post-function, putting it before the re-index step.

import java.time.*
import java.util.*
import com.atlassian.jira.component.ComponentAccessor

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Your Date Field")
def newDate = LocalDate.now(ZoneId.systemDefault()).plusDays(2)
issue.setCustomFieldValue(cf, Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant()))

Kumar, G Sunil October 22, 2025

This seems to be for JIRA DC can we get any example for cloud.

 

Regards,

Sunil

Gor Greyan
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.
October 22, 2025

Hi, @Kumar, G Sunil

Yes, sorry, it was for DC.

Please try this one.

import { asApp } from '@scriptrunner/cloud/runas';

export async function postFunction(event, context) {
const issueKey = event.issue.key;

// replace with your field id, e.g. "customfield_25632"
const FIELD_ID = 'customfield_12345';

// If your field is a Date (not Date Time), set this to false
const IS_DATE_TIME = false;

const now = new Date();
const twoDaysMs = 2 * 24 * 60 * 60 * 1000;
const target = new Date(now.getTime() + twoDaysMs);


const dateOnly = target.toISOString().slice(0, 10);
const value = IS_DATE_TIME ? target.toISOString() : dateOnly;

const body = { fields: { [FIELD_ID]: value } };

const res = await asApp().requestJira(`/rest/api/3/issue/${issueKey}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});

if (!res.ok) {
const text = await res.text();
throw new Error(`Failed to update ${FIELD_ID} on ${issueKey}: ${res.status} ${text}`);
}
}

Put this as the first post-function in the transition so it runs before anything that flips the field to read-only.

Kumar, G Sunil October 22, 2025

Import is giving error here{ asApp }

Gor Greyan
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.
October 22, 2025

Dear @Kumar, G Sunil

export async function postFunction(event, context) {
const issueKey = event.issue.key;
const FIELD_ID = 'customfield_12345'; // <-- Replace with your field ID
const IS_DATE_TIME = false; 

const now = new Date();
const twoDaysMs = 2 * 24 * 60 * 60 * 1000;
const target = new Date(now.getTime() + twoDaysMs);

const dateOnly = target.toISOString().slice(0, 10);
const value = IS_DATE_TIME ? target.toISOString() : dateOnly;

const body = { fields: { [FIELD_ID]: value } };

// Use global `api` helper
const res = await api.asApp().requestJira(`/rest/api/3/issue/${issueKey}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});

if (!res.ok) {
const text = await res.text();
console.log(`Failed to update ${FIELD_ID} on ${issueKey}: ${res.status} ${text}`);
} else {
console.log(`Successfully updated ${FIELD_ID} on ${issueKey}`);
}
}

Please try this one.

0 votes
Taliah15
Contributor
October 20, 2025

Hello @Kumar, G Sunil 

To update your custom field by adding 2 days during a post function, using %%CURRENT_DATETIME%%+2d alone won't work as expected in Jira's built-in expressions. A crisp way is to use a scripted post function (like ScriptRunner) where you can programmatically add 2 days to the current date and set the field value right in the transition, ensuring it happens before the field locks as read-only. This guarantees instant updating without delay, bypassing the limitations of JMWE or Automation. Let me know if you want a sample script snippet for that! If it works kindly plz let me know. 

Kumar, G Sunil October 22, 2025

Can you share a sample script.


Regards,

Sunil

0 votes
Trudy Claspill
Community Champion
October 20, 2025

Hello @Kumar, G Sunil 

How is the field being made read-only?

Kumar, G Sunil October 20, 2025

Script Runner Behaviours.

 

Suggest an answer

Log in or Sign up to answer