Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events