Auto populate custom date field on change of status

kammili tejraj May 9, 2022

Hi Team,

My requirement is date field should be added into a custom field automatically, when the XXX issue type state is moved to "xxxxxx". Using server version and also using script runner for jira plugin.

 

Thanks,

 

1 answer

0 votes
Andy Rusnak
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 10, 2022

Hi Kammili, 

There are a few ways that I believe you can accomplish this.  But just to make sure I understand what you are trying to do you want to accomplish the following: 

  1. Issue is status A and issue has a custom date field. 
  2. User transitions the issue to status B and during the transition a custom date field is automatically set to a value

Is that correct? 

If my understanding is correct and you want to use ScriptRunner, you should be able to accomplish this using a Workflow Transition Post Function and the ScriptRunner Custom script post-function.  I think you can do the following: 

  1. Edit the Workflow in question: Jira > Gear Icon > Issues > Workflows > ( Select Workflow ) > Edit. 
  2. Select the transition you wish this to happen on and choose Post Functions
  3. Add Post Function and select the Custom script post-function [ScriptRunner]
  4. Set up your ScriptRunner script that will set your custom date field to the value you need.  Below is a very simple example I set up to set the field 'Test Custom Date' to today's date: 

 

import com.atlassian.jira.component.ComponentAccessor

// Name of the custom field to change

final customFieldName = "Test Custom Date"

// New value for that field

final newValue = new Date();

log.fatal(newValue);

def customFieldManager = ComponentAccessor.customFieldManager

def customField = customFieldManager.getCustomFieldObjects(issue).find { it.name == customFieldName }

assert customField: "Could not find custom field with name $customFieldName"

issue.setCustomFieldValue(customField, newValue.toTimestamp())

This was adopted from Custom Field Post Function script in the Adaptavist library to use a date rather than a text field.  

Once you have set this up you should be able to publish your Workflow and the custom date field will be set when this post function runs upon transition.  

Of course whenever making these types of changes it would be strongly recommended to test these types of changes out in a non-production workflow first.  This is very simplified example I tested, so there certainly could be additional complications in your business process that need fine tuning. 

But this quick example seemed to accomplish what I believe you are attempting to do in my environment.  

Please let me know if this works for you or if you have any questions related to this and I would be happy to help out further.  

Best, 

Andy

 

Suggest an answer

Log in or Sign up to answer