Forums

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

How do I copy a date custom field from issue 1 to linked issue 2? Jira 7.6

Linda H June 12, 2018

I am new to script runner. I want to copy a date custom field from issue 1 to linked issue.  I have Jira 7.6.1 sever and script runner.  

I have successfully been able to do this with other fields for description, people, but the date field won't populate with a date.  It returns "invalid date" and under these words I can see this $datePickerFormatter.format($value)  but very briefly.  I do not have listeners setup for any of the fields that do work. 

my code is this: I do have a parent script set up, but not sure that it is needed.  Should I allow for fields without a date and how would I do that?

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def fieldSizingStatusDate = customFieldManager.getCustomFieldObject("customfield_16900")
def linkedSizingStatusDate

issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
if (issueLink.issueLinkType.name == "Sizes") {
linkedSizingStatusDate = issueLink.getDestinationObject().getCustomFieldValue(fieldSizingStatusDate)
}
}

return linkedSizingStatusDate

2 answers

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
June 12, 2018

Hi,

Assuming this is a code for a scripted field, make sure that:

1) You have created your scripted field using the 'Date Time Picker' template.

2) The searcher for that field is set to 'Date Time searcher'

3) If your 'customfield_16900' is a 'Date Picker' type field, then you should get its value like so:

issueLink.getDestinationObject().getCustomFieldValue(fieldSizingStatusDate)

and then return it in your scripted field

Linda H June 12, 2018

Hi Ivan,

Changing the code work. Thanks!

With the template set to Date Time Picker and the searcher set to Date Time Range Picker.  The date field appears on the right with all the other date fields (yeah) but instead of the date it says "2 days ago", when it should say 06/11/2018, which could be 2 days ago.

With the template set to Text Field or Absolute Date Time and the searcher set to Date Time Range picker then the field appears on the left (boo) but with the date (yeah).  

Any ideas? I have no idea how it is getting the "2 days ago"

Thank you so very much for your reply!!

Ivan Tovbin
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.
June 13, 2018

That one's easy enough. The reason why you see things like '2 days ago' is because by default Jira has relative dates enabled. Check out this KB article on how to disable it.

Linda H June 13, 2018

Hi Ivan, 

I will have to have my server team make that change.  I was wondering why it was working in one environment but not the other.  This explains it.  

Thank you so very much for your reply!!  I really appreciate it!

0 votes
Prakhar Srivastav {Appfire}
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.
June 12, 2018

Hi @Linda H

Try like below :

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import static java.lang.Math.*
import java.sql.Timestamp

// get current issue
Issue issue = issue
customFieldManager = ComponentManager.getInstance().getCustomFieldManager()


// Getting custom field value
sourceDate = issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("sourceDateField") ) as Date


// Getting custom field object
destinationDateObj = customFieldManager.getCustomFieldObjectByName("destinationDateField")


issue.setCustomFieldValue(destinationDateObj, sourceDate)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events