Forums

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

Copy date field from parent to related issue

Peter Ellis
Contributor
January 30, 2024

Hi,

I have the following code that is creating a related issue via a behaviour,

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()

if (getBehaviourContextId() == "create-change-pmo") {
    getFieldById("project-field").setReadOnly(true)
    getFieldById("issuetype-field").setReadOnly(true)
    def contextIssue = issueManager.getIssueObject(getContextIssueId())
    getFieldById("issuelinks-linktype").setFormValue("Linked by Change").setReadOnly(true)
    getFieldById("issuelinks-issues").setFormValue(contextIssue.key).setReadOnly(true)
    getFieldById("assignee").setFormValue(contextIssue.assigneeId).setReadOnly(false)

getFieldByName("Name of 3rd Party Issues")setHidden(true)
getFieldByName("Name of Dependant Project or Programme").setHidden(true)
getFieldByName("Other Details").setHidden(true)

}
This is from the ScriptRunner library - it works fine. However I also need to copy a customfield date from the parent and I am using this code,
getFieldById("customfield_33008").setFormValue(contextIssue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_33008"))).setReadOnly((false))
This example attempts to put a long number into the date field (see attachments) I don't understand why that would be happening as they are both date fields of the same name. If I try changing the code (removing the FieldManger part it does nothing). can someone assist?300124_A.png 300124_B.png

2 answers

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
January 30, 2024

Behavious methods run against/witch the form data.

While the native jira methods run against/with database data.

So when you retrieve the value of the customfield using contextIssue.getCustomFieldValue() you get a "Timestamp" object (that a jira api call from the com.atlassian.jira.issue.Issue class)

But the "setFormValue" needs a valid string since that's what the form expects.

Something like this should work:

def sourceDate = underlyingIssue.getCustomFieldValue(33008) //this uses a HAPI extension method that bypasses the need for the customFieldManager
getFieldById('customfield_33008').setFormValue(sourceDate.format('d/MMM/yyyy')).setReadOnly(true)
Peter Ellis
Contributor
January 31, 2024

Hi @PD Sheehan

I tried your suggestion but it didn't work. What I finally got working was a variation of yours,

def sourceDate = contextIssue.getCustomFieldValue("Baseline M2 Date")

getFieldById("customfield_33008").setFormValue(sourceDate.format('dd/MMM/yy'))

 Appreciate your help :-)

0 votes
Harsh
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.
January 30, 2024

Hi @Peter Ellis 

I would suggest to use jira automation. That would be easier to do so. 

And wrt your code. Please check the date format in parent and child ticket (if they are different) and Convert the date to correct format. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events