Forums

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

I am trying to write a groovy where I need to save the date and time to a custom field

Arjun Hazari May 13, 2019

can anyone help me I have a custom Field named Product where I need to capture the timestamp of this field changed and save this to different custom field which will be of Date type.

 

I am trying to achieve this by  using a Script-runner Addon.

The new field where I will save this will be Hidden field.

Any guidance would be very appreciated :) Thank you!

1 answer

0 votes
Fabio Racobaldo _Herzum_
Community Champion
May 13, 2019

Hi @Arjun Hazari ,

if you want to change your field during edit/transition, you can setup a behaviour (https://scriptrunner.adaptavist.com/5.4.47/jira/behaviours-overview.html) based on Product field with the following code :

 

import java.util.Date;

FormField productFF = getFieldById(getFieldChanged());
Date product = (Date)productFF.getValue();
FormField yourNewFieldFF = getFieldById("customfield_12345");//YOUR CF ID HERE
yourNewFieldFF.setFormValue(product);
yourNewFieldFF.setHidden(true);

 Please, make sure that your field is into edit/transition screen.

Hope this helps.

Fabio

Arjun Hazari June 5, 2019

Hello @Fabio Racobaldo _Herzum_ ,

 

Thanks for your reply actually I used the below code 

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp
import com.atlassian.jira.issue.MutableIssue

//Issue issue = issue   this will not work since we are making this change for a listener so Issue class may not work.
MutableIssue issue = event.issue as MutableIssue


def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "mySourceField"}
if (change) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("myTargetField")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}

 

Suggest an answer

Log in or Sign up to answer