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?
I will try both methods. Hopefully there is no delay when using SR and field is updated immediately.
Regards,
Sunil
Hi @Kumar, G Sunil
After checking, please let us know.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This seems to be for JIRA DC can we get any example for cloud.
Regards,
Sunil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.