Forums

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

Copy the numeric value of a custom field to Time Tracking fields (Original Estimated, Remaining Esti

Ashok Barik
Contributor
April 9, 2022

Copy the numeric value of a custom field to the Time Tracking fields (Original Estimated, Remaining Estimated, and Logged).

Could someone please assist me in using Scriprunner to copy custom field values to time tracking?
I have three custom fields (number fields in hours) that I need to copy to Time Tracking when we update the issue in the inline edit view (not in transition).

For example:

Field1 -> Original Estimated.
Field 2 -> Remaining Estimated.
Field3 -> Logged.

3 answers

0 votes
henrik_gerdtsson June 21, 2022

I used the following script to copy from a custom field to system field Original Estimate. Replace customfield_13858 to your custom field id.

The trick is to use

  • issue.setOriginalEstimate(newOriginalValue) for Original Estimate
  • issue.setEstimate(newRemainingValue) for Remaining Estimate 
  • issue.setTimeSpent(workedSeconds) for Work Logged

All values should be in seconds and of type Long.

//
// This script will copy estimates from a custom number field to the system field Original Estimate

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import java.sql.Timestamp

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
// Set the custom field id for the different custom fields
// Values will be copied from Estimated Time, customFieldId=13858
def estimatedTime = cfManager.getCustomFieldObject("customfield_13858")

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

log.warn("Running script for copying times to Original Estimate.")

// Find all issues with Estimated Time set that does not have an Original Estimate
def query = jqlQueryParser.parseQuery("project = OEYAC and \"Estimated Time\" is not EMPTY AND originalEstimate is EMPTY")
def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
log.warn("Total issues: ${search.total}")

// Loop through all the issues with Estimated Time set
search.results.each { documentIssue ->
log.warn("Processing issue: ${documentIssue.key}")

// Get a mutable issue
def issue = issueManager.getIssueObject(documentIssue.id)

//Get the current date and create the new date value
def timeEstimate = issue.getCustomFieldValue(estimatedTime) as Long
// Estimated Time is in hours and Original Estimate deals with seconds. 1h = 3600sec
def originalEstimate = timeEstimate * 3600

// Update the new field in the issue
issue.setOriginalEstimate(originalEstimate)
log.warn("Estimated Time: ${timeEstimate}")
log.warn("OriginalEstimate: ${originalEstimate}")

// Save the changes
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

henrik_gerdtsson June 21, 2022

Don't forget to reindex the project to make the new estimates available in searches.

0 votes
Nic Brough -Adaptavist-
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.
April 9, 2022

Theres a good script for working with fields at https://library.adaptavist.com/entity/update-the-value-of-custom-fields-through-the-script-console 

But it doesn't demonstrate the use of system fields.

For the original estimate, you can simply say issue.setEstimate(#) but bear in mind that it stores the estimate as a simple number.  I think the number is seconds (it's been a long time since I looked at it) - so you will need to enter 28800 for an estimate of 1 working day (8 hours).

You don't want to set remaining estimate or logged time - they're calculated from the data entered in other places.

0 votes
Pramodh M
Community Champion
April 9, 2022
Ashok Barik
Contributor
April 10, 2022

Hello @Pramodh M , copy from custom field to custom field works for me but I didn't fine method to copy from a custom field to Time Tracking Origianl Estimate and same for other, Reamining Estimated and Logged. 

Could you please help me here how can I do that? 

Suggest an answer

Log in or Sign up to answer