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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Update value of custom field from scriptrunner - subtask

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

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.

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

Example.: 

new ModifiedValue(mutableSubtask.getCustomFieldValue(customField) * 0.25 

 

?

in this line.... 

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

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())

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

 

test.jpg

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?

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) [?:?]

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

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())

}

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

in postfunctions... see below: 

 

test2.jpgtest3.jpg

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?

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

I tried to use 

issue.setCustomFieldValue

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

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)

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

@fabricio_borges could you accept the answer please? :)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

116 views 1 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you