The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

How to set default custom field value from another custom field? Both values are numeric.

KatySpade
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 28, 2022

Hi! Im newbie in ScriptRunner so I need some help. I have to set moving numeric value from customfield1 to customfield2 on workflow transition. This value must be editable. Below I pass my code. This code is copiled okey, but I dont see any changes on issue.

Regards and Thanks!!!

 

 

import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
//import com.atlassian.jira.issue.CustomFieldManager
//import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder


def customFieldManager = ComponentAccessor.customFieldManager

// the name of the custom field to update
def customFieldName = 'Cost'
def fundingFieldName = 'Funding'
def fundingField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)

// the new value of the field
def newValue = customField.getValue(issue)

fundingField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fundingField), newValue), new DefaultIssueChangeHolder())

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 3, 2022

Hi @KatySpade

I have observed something in your code, i.e. if you intend to pass the value from one field to another, should you not be initialising two different fields?

If you observe your code, you appear to be initialising the same field twice, i.e.

def customFieldName = 'Cost'
def fundingFieldName = 'Funding'
def fundingField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)

 In both cases, you appear to be initialising the Cost field. 

Try to modify your code slightly to:-

def customFieldName = 'Cost'
def fundingFieldName = 'Funding'

def fundingField = customFieldManager.getCustomFieldObjectsByName(fundingFieldName).first()
def customField = customFieldManager.getCustomFieldObjectsByName(customFieldName).first()

Also, try to use

issue.setCustomFieldValue(fundingField, issue.getCustomFieldValue(customField))

And see if you can get the expected result.

So your full updated code should be:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def customFieldName = 'Cost'
def fundingFieldName = 'Funding'

def fundingField = customFieldManager.getCustomFieldObjectsByName(fundingFieldName).first()
def customField = customFieldManager.getCustomFieldObjectsByName(customFieldName).first()

issue.setCustomFieldValue(fundingField, issue.getCustomFieldValue(customField))

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

TAGS
AUG Leaders

Atlassian Community Events