Forums

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

Use Scriptrunner update one custom field with value of another field in the same issue.

Eric Sebian
Contributor
April 30, 2020

This may seem weird but I am using a scripted field to get the sum total of  (budget) - (spend to-date) - (accrual) to the scripted field (Remaining Budget). The problem is we're using Portfolio and Portfolio wont display scripted fields. So I think that I need to create a listener that will update a number custom field with the results from the scripted field (Remaining Budget). Can someone please help. 

Thanks.  

3 answers

0 votes
Andre Serrano
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.
May 6, 2020

Hi Eric

Considering that you don't have much experience with Groovy scripting, this webinar might be useful to you: https://www.adaptavist.com/webinars/groovy-scripting-for-scriptrunner/

Eric Sebian
Contributor
May 7, 2020

thanks.

Eric Sebian
Contributor
May 7, 2020

thanks. I watched some of it and it explains a few things that I didnt know before.

Andre Serrano
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.
May 12, 2020

This is good to hear :)

0 votes
Sebastian Krzewiński
Community Champion
May 1, 2020

Hi @Eric Sebian 

 

Please check https://library.adaptavist.com

There you can find a lot of example and working scripts that you can easily modify. I did it few times and save a lot of time :)

 

Regards,

Sebastian

Eric Sebian
Contributor
May 1, 2020

I checked there, but unfortunately I'm not familiar with groovy scripting but I'll try. Thanks for the link.

Eric Sebian
Contributor
May 4, 2020

Below is what I'm using so far to calculate the scripted field. What I need now is a way to take that calculated value of the scripted field and copy it to a custom field. I'd like to do all of this within the scripted field Is it possible? :

import com.atlassian.jira.component.ComponentAccessor
def aval = getCustomFieldValue("Budget")
def bval = getCustomFieldValue("Accruals")
def cval = getCustomFieldValue("Spend to-date")

// check both fields contain valid numeric values and if not set them to 0
if (aval == null) {
aval = 0
}
if (bval == null) {
bval = 0
}
if (aval != null && bval != null){
return cval - bval - aval
} else {
// return to some code to indicate a null value in one of the fields
return 0
}

Sebastian Krzewiński
Community Champion
May 4, 2020

If you need to set field value you should use

issue.setCustomFieldValue(field, value)
Eric Sebian
Contributor
May 4, 2020

Hi, Sebastian. I dont need to set it. I need to copy the value from the scripted field to another custom field on the same issue.

 

Thanks.

Sebastian Krzewiński
Community Champion
May 5, 2020

Copying value from one field to second field is basically setting second field with value from first field.

Eric Sebian
Contributor
May 7, 2020

So I couldn't figure out how to do it all in the scripted field so I left the scripted field as is and added a listener. I wish that I could have figured how the do it all in the one scripted field, but this is working. If someone has a better way, I'm all ears.

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjectByName("Remaining Budget(hidden)")
def cFieldValue = issue.getCustomFieldValue(cField)
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Remaining Budget"}
def changeHolder = new DefaultIssueChangeHolder()
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), cFieldValue as Double),changeHolder)

Like Sebastian Krzewiński likes this
lujmo March 15, 2022

@Eric Sebian  Did you ever figure out how to update another script field from within a script field ? 

lujmo March 15, 2022

actually i just got it to work I think .. 

i created a new mutableissue

MutableIssue mutableIssue = (issueManager.getIssueByCurrentKey(issue.getKey()))

and used that to update the other custom field:

mutableIssue.setCustomFieldValue(group, yellow)
issueManager.updateIssue(loggedInUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

Suggest an answer

Log in or Sign up to answer