Update value of custom field from scriptrunner - subtask

fabricio.borges January 14, 2021

How to change a custom subtask field from the parent issue using the scriptrunner?

issue daughter field "Pontos Historia" = issue parent field "Pontos Historia" * 0.25


Was it something like that?

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

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager ()
def Campo = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Pontos de Historia'}


def changeHolder = new DefaultIssueChangeHolder ()
Campo.updateValue (null, issue, new ModifiedValue (issue.getCustomFieldValue (Campo) * 0.25, changeHolder)

 

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 14, 2021

Hi @fabricio.borges welcome on the community. You just need to set it up on subtasks. THe rest of the script looks fine:

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

final customFieldName = 'Pontos Historia'

def issueManager = ComponentAccessor.issueManager

def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue).find({
it.getFieldName() == customFieldName})
assert customField : "Could not find custom field with name $customFieldName"

def parentIssue = issue.getParentObject()

issue.setCustomFieldValue(customField, parentIssue.getCustomFieldValue(customField) * 0.25)

I have no environment set up to test the script but if it won't work, just let me know and we will fix it.

fabricio.borges January 15, 2021

how to calculate the value in the subtask by multiplying by a fixed number?

Example.: 

new ModifiedValue(mutableSubtask.getCustomFieldValue(customField) * 0.25 

 

?

fabricio.borges January 15, 2021

in this line.... 

customField.updateValue(null, mutableSubtask, new ModifiedValue(mutableSubtask.getCustomFieldValue(customField) * 0.25, issue.getCustomFieldValue(customField)), new DefaultIssueChangeHolder())

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

Hi @fabricio.borges it is almost correct, but first parameter of new ModifiedValue is oldValue, and second is newValue. So it should be:

customField.updateValue(null, mutableSubtask, new ModifiedValue(mutableSubtask.getCustomFieldValue(customField), issue.getCustomFieldValue(customField) * 0.25), new DefaultIssueChangeHolder())

fabricio.borges January 15, 2021

ok ... sorry but how do i fix this error?

 

test.jpg

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

It might be only static check error, but compiler can't know what is the type of result of  getCustomFieldValue(...). Did you try to run the code?

fabricio.borges January 15, 2021

yes, but the error below happens.

 

Detalhes técnicos

Número de referência Log's: 53096f08-a15f-498c-858d-b6345cbeec8e

Causa

URL do referenciador: http://suporte.maximatech.com.br/browse/TESTEDV-98

java.lang.AssertionError: Could not find custom field with name Pontos Historia. Expression: customField. Values: customField = null
java.lang.AssertionError: Could not find custom field with name Pontos Historia. Expression: customField. Values: customField = null
 at Script744.run(Script744.groovy:11) [?:?]
 at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runScriptAndGetContext(ScriptRunnerImpl.groovy:175) [?:?]
 at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$6.callCurrent(Unknown Source) [?:?]
 at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runStringAsScript(ScriptRunnerImpl.groovy:164) [?:?]
 at com.onresolve.scriptrunner.runner.ScriptRunner$runStringAsScript$7.call(Unknown Source) [?:?]
 at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:70) [?:?]
 at com.onresolve.scriptrunner.canned.jira.utils.AdditionalCustomScriptDelegate.super$2$doScript(AdditionalCustomScriptDelegate.groovy) [?:?]
 at com.onresolve.scriptrunner.canned.jira.utils.AdditionalCustomScriptDelegate.doScript(AdditionalCustomScriptDelegate.groovy:22) [?:?]
 at com.onresolve.scriptrunner.canned.jira.utils.AdditionalCustomScriptDelegate$doScript.call(Unknown Source) [?:?]
 at com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils.doAdditional(ConditionUtils.groovy:232) [?:?]
 at com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils$doAdditional$3.call(Unknown Source) [?:?]
 at com.onresolve.scriptrunner.canned.jira.utils.AbstractCloneIssue.doScript(AbstractCloneIssue.groovy:221) [?:?]
 at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CreateSubTask.super$2$doScript(CreateSubTask.groovy) [?:?]
 at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CreateSubTask.doScript(CreateSubTask.groovy:144) [?:?]
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

I adjusted script on the beginning of this branch... give it a try, please.

fabricio.borges January 15, 2021

the script below did not give an execution error. However, it did not update the value of the custom field in the subtask.

 

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

final customFieldName = 'Pontos de História'

def issueManager = ComponentAccessor.issueManager

def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue).find({
it.getFieldName() == customFieldName})
assert customField : "Could not find custom field with name $customFieldName"

for(def subtask: issue.getSubTaskObjects()){

def mutableSubtask = issueManager.getIssueObject(subtask.id)

customField.updateValue(null, mutableSubtask, new ModifiedValue(mutableSubtask.getCustomFieldValue(customField), issue.getCustomFieldValue(customField) * 0.8), new DefaultIssueChangeHolder())

}

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

Are you invoking the script as part of the postfunctions? Or how/when do you invoke it?

fabricio.borges January 15, 2021

in postfunctions... see below: 

 

test2.jpgtest3.jpg

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

Ok, I think the code is invoked on new subtask, so data must be copied from parent. I adjusted script. Is it ScriptRunner's postfunction?

fabricio.borges January 15, 2021

Yes, but unfortunately not yet certain. The value of the custom field in the subtask continues to come from the issue.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

I tried to use 

issue.setCustomFieldValue

method, so give it a try, otherwise we will have to debug it a little :) 

fabricio.borges January 15, 2021

got it here .. Thank you so much!

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Pontos Historia'}
issue.setCustomFieldValue(cf, sourceIssue.getCustomFieldValue(cf) * 0.8)

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2021

ok great :D it is tough to try to programme virtually :D, have a nice weekend :)

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 19, 2021

@fabricio.borges could you accept the answer please? :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events