Forums

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

Update Custom Field value with Scriptrunner Script Fields

RMA September 25, 2020

Hi,

I want to create a script field which calculates a value and depending on the value it should update another custom field. So far I have created following script:

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import java.lang.Math

IssueService issueService = ComponentAccessor.getComponent(IssueService)
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager)
def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def A = getCustomFieldValue("Planned Project hours of work")
def B = getCustomFieldValue("Actual Project hours of work + ETC (hours)")
def result = (B-A)/A
def percent = result * 100

return (Math.floor(percent*100)/100 + "%")

def KPI_Project_Work = getCustomFieldValue("KPI: Project Work")

Options options = optionsManager.getOptions(KPI_Project_Work.getConfigurationSchemes().first().getOneAndOnlyConfig())

if (percent < 10)
{
def selectedOptions = options.findAll {
it.value == "Green"
}.collect {
it.optionId.toString()
}
}

 

I already get the value of "percent", but when I try to update the field value "KPI_Project_Work", just nothing happens. Can anyone help me out?

 

Best Regards

Robert

 

2 answers

1 vote
Pedro Felgueiras
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.
September 25, 2020

Are you able to update the CF value in the scripted console?

 

Look at this documentation, it may help 

https://library.adaptavist.com/entity/update-the-value-of-custom-fields-through-the-script-console

0 votes
Gustavo Félix
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.
September 25, 2020

I think that when you put the "return" your code execution ends. 
So, If you want to update or do any other operation, just do it before you return a value.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.4.2
TAGS
AUG Leaders

Atlassian Community Events