Forums

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

Use Scriptrunner to store Original Estimate in a custom field field

Modix Atlassian August 21, 2018

We created a issue type "work package" and we want to copy the original estimate (sum of stories in structure under this issue type) on the transition to a status "baselined" in a custom field. 

Can someone please help me with the inline script for the scripted field we need to create for the custom field (created via scriptrunner). 

(And is there somehow a possibility to lock this field after the transition?)

2 answers

0 votes
Nir Haimov
Community Champion
August 24, 2018

The complete code for your needs:

enableCache = {-> false}
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.getId())
def subElements = issueLinks.findAll { it.issueLinkType.name == 'Work package' }
def totalStoriesEstimation = 0

for (def i = 0; i < subElements.size(); i++ ) {
totalStoriesEstimation = totalStoriesEstimation + issueManager.getIssueObject(subElements[i].destinationId).getOriginalEstimate()/3600/8
}

return totalStoriesEstimation

:) 

0 votes
Nir Haimov
Community Champion
August 21, 2018

Hi,

To understand your needs.

you have epic with multiple stories, and you want to see inside the epic the sum of original estimate of all the stories in it?

Modix Atlassian August 21, 2018

Hi :)

yes - but this we already have. 

We want to "save" the value of the original estimate in a separate field after a specific status is reached. We are using an issue type called work package, which summarises via structure the estimates like the epics. 

Nir Haimov
Community Champion
August 21, 2018

OK,

I don't understand why you need it, but fine :)

Do you want it to happen after an Epic reached to a specific status or when Story reach to a specific status?

Modix Atlassian August 21, 2018

we don't use epics in our customer projects, we use the issue type work package there - and when the work package reaches a status, we need to have the sum of the original estimate in a separate field. 

Modix Atlassian August 21, 2018

So basically my problem is, that I'm not able to configure a custom field with the setting, that I'm able to copy the original estimate into this field via a workflow post function. I used a scripted field from scriptrunner.. but when I select "duration (time tracking)" as field type, I need to fill in inline script or a path to a script - and I don't know what I need to do there. 

Nir Haimov
Community Champion
August 21, 2018

in the inline script write this:

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor


def changeHolder = new DefaultIssueChangeHolder();

def tgtField = customFieldManager.getCustomFieldObject("customfield_11111") /*replace customfield_11111 with your field id*/
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getOriginalEstimate()),changeHolder);

 This will update your customfield with the value of your originalEstimate of the issue.

Modix Atlassian August 21, 2018

is is possible to add to this script, that it only copies the value on the transition to the status baselined? because otherwise it will update every time the value changes..

Nir Haimov
Community Champion
August 21, 2018

Sure...

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor

if (issue.getStatus().getName() == "baselined") {
def changeHolder = new DefaultIssueChangeHolder();

def tgtField = customFieldManager.getCustomFieldObject("customfield_11111") /*replace customfield_11111 with your field id*/
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getOriginalEstimate()),changeHolder);
}

Modix Atlassian August 21, 2018

I get some errors then: (with the right number for the custom field)

   def tgtField = customFieldManager.getCustomFieldObject("customfield_11111")

Static type checking - the variable customfieldmanager is undeclared. 

 

 

tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getOriginalEstimate()),changeHolder);

Cannot find matching method com.atlassain.jira.issue.issue#getcustomfieldvalue (java.lang.obect). Please check if the declared type is right and if the method exists.

Cannot find matching method java.lang.object#updatevalue(<unknown parameter type>, com.atlassian.jira.issue.issue, com.atlassian,jira.issue.modifiedvalue, com.atlassian.jira.issue.util.deufaltissuechangeholder. please check if the declared type is right and if the method exists. 

Nir Haimov
Community Champion
August 21, 2018

You right,

i missed something, here is the complete code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

if (issue.getStatus().getName() == "baselined") {
def changeHolder = new DefaultIssueChangeHolder();

def tgtField = customFieldManager.getCustomFieldObject("customfield_11111") /*replace customfield_11111 with your field id*/
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getOriginalEstimate()),changeHolder);
}

checked it on my instance and it works

 

Just don't forget to replace the "customfield_11111" with the id of your field

Modix Atlassian August 24, 2018

Hi, sorry for the late reply.

 

This is what I have now: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

if (issue.getStatus().getName() == "baselined") {
def changeHolder = new DefaultIssueChangeHolder();

def tgtField = customFieldManager.getCustomFieldObject("customfield_12412")
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getOriginalEstimate()),changeHolder);
}

 

But there is nothing happening during the transition to baselined. 

Nir Haimov
Community Champion
August 24, 2018

Hi,

In which step of your post-function did you put your script?

Try to put it one above the "Re-index an issue to keep indexes in sync with the database."

Modix Atlassian August 24, 2018

Oh - in a postfunction?

 

I have put it in the "script" section in scriptrunner for the scripted fields.

Because I need the custom field with the possibility have values as duration in the fields.

Nir Haimov
Community Champion
August 24, 2018

Do you have skype? talking in chat mode it will be more easy

Modix Atlassian August 24, 2018

Yes - I send you an linkedin request to add me (Eva) - so we can exchange the Skype addresses there.

Nir Haimov
Community Champion
August 24, 2018

I rejected your request by mistake, can you send again? :)

Nir Haimov
Community Champion
August 24, 2018

Sent you connection request, please approve, otherwise we can't message to each other.

Suchithra s October 14, 2021

@Nir Haimov I am facing the same issue and I kept the code in script post function but the code is not working for me.

Can you help me here please?

Suggest an answer

Log in or Sign up to answer