Forums

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

I can't update parent issue field value by Custom script post-function

jack ma August 7, 2021

Hi ,

I want to update the parent issue custom field value after creating the child issue . I use  the post-function of workflow , but it doesn't work .

This is the script :

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
Issue pIssue = issue.getParentObject();
CustomField totalAmount = customFieldManager.getCustomFieldObjectByName("my custom field");
MutableIssue mutableIssue = (MutableIssue) pIssue;
mutableIssue.setCustomFieldValue(totalAmount, 200);

 Please help confirm the cause of the problem . Any and all help would be appreciated. Thanks

3 answers

2 accepted

1 vote
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
August 7, 2021

hi @jack ma can you check your log file?does it contain any error?

jack ma August 7, 2021

Hi, I did not find any error logs

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
August 8, 2021

Hi @jack ma regarding to your last response where you describe that only subtask is updated but parent task is not updated. The problem is, that postfunction is invoked on workflow of subtask, so it handles "store" operation for it.

You need to at least use following code:

issueManager.updateIssue(user, pIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(issueManager.getIssueObject(pIssue.id))
ImportUtils.setIndexIssues(wasIndexing)

But if you want to do it totally correctly, you should use methods

Like jack ma likes this
jack ma August 8, 2021

Hi @Martin Bayer _MoroSystems_ s_r_o__ @Vikrant Yadav  I really appreciate your help very much. I have found a way to update the value of the parent task field, Use the following method can meet my needs.

customField.updateValue(fieldLayoutItem,mutableIssue,modifiedValue,issueChangeHolder);
Like # people like this
Vikrant Yadav
Community Champion
August 8, 2021

😎 Glad to know.. we were able to help you.

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 9, 2021

Hi @jack ma

You could try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def count = customFieldManager.getCustomFieldObjectsByName('Count')[0]

def issue = issue as MutableIssue

if (issue.isSubTask() && issue.isCreated()) {

def parentIssue = issue.parentObject as MutableIssue

if(parentIssue.subTaskObjects.size() == 0) {
parentIssue.setCustomFieldValue(count, parentIssue.subTaskObjects.size() + 1 as Double)
} else {
parentIssue.subTaskObjects.findAll {
def size = parentIssue.subTaskObjects.size() as Double
parentIssue.setCustomFieldValue(count, size + 1 as Double)
}
}
issueManager.updateIssue(loggedInUser, parentIssue, EventDispatchOption.ISSUE_UPDATED, false)
}

Please note, this sample code is not 100% exact to your environment. Hence you will need to make the required modifications.

Once the issue's sub-task(s) gets created in the example above, it will automatically update the Count, i.e. Number field, accordingly.

You will need to modify the value added to the counter.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

jack ma August 9, 2021

Hi@Ram Kumar Aravindakshan _Adaptavist_ Thanks very much for your help. I have implemented the requirement in another way, but I just tried your method and it can also worked, amazing!

Best Regards

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 10, 2021

Hi @jack ma

Great to hear the solution worked for you. :)

The code can actually be further simplified like below:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def count = customFieldManager.getCustomFieldObjectsByName('Count')[0]

def issue = issue as MutableIssue

if (issue.isSubTask() && issue.isCreated()) {
def parentIssue = issue.parentObject as MutableIssue
def size = parentIssue.subTaskObjects.size() as Double

if (parentIssue.subTaskObjects.size() >= 0) {
parentIssue.setCustomFieldValue(count, size + 1 as Double)
}
issueManager.updateIssue(loggedInUser, parentIssue, EventDispatchOption.ISSUE_UPDATED, false)
}

Thank you and Kind Regards,

Ram 

0 votes
Vikrant Yadav
Community Champion
August 7, 2021

Hi @jack ma  Are you trying to update a text field ? 

jack ma August 7, 2021

Hi @Vikrant Yadav ,I have tried to update the field value, but the Custom script post-function will not be executed if I do so.

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
Issue pIssue = issue.getParentObject();
CustomField totalAmount = customFieldManager.getCustomFieldObjectByName("my field name");
MutableIssue mutableIssue = (MutableIssue) pIssue;
mutableIssue.setCustomFieldValue(totalAmount, 200);
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
FieldLayoutItem fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(pIssue).getFieldLayoutItem(totalAmount);
//update
totalAmount.updateValue(fieldLayoutItem, mutableIssue,new ModifiedValue(pIssue.getCustomFieldValue(totalAmount),200), issueChangeHolder);

tu_220782422649.pngtu_220782436537.png

jack ma August 7, 2021

Hi@Vikrant Yadav ,What I am updating is the number field

Vikrant Yadav
Community Champion
August 7, 2021

Hi @jack ma @Martin Bayer _MoroSystems_ s_r_o__  I have applied post function at 2nd position, below Create issue Post function and getting below error in logs. 

 

2021-08-07 10:29:07,316 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue VY-30 for user 'vikrant-yadav2'. View here:  java.lang.Integer cannot be cast to java.lang.Double at com.atlassian.jira.issue.customfields.impl.NumberCFType.getDbValueFromObject(NumberCFType.java:46) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.updateValue(AbstractSingleFieldType.java:152) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:432) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:402) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source) at Script1059.run(Script1059.groovy:23)

Like jack ma likes this
jack ma August 7, 2021

Hi@Vikrant Yadav, Thanks for your answer. This is a data type conversion problem, I have made changes. but I can only modify the field value of the subtask. The field value of the parent task has not been updated.

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.user.ApplicationUser;
log.setLevel(org.apache.log4j.Level.DEBUG);
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
//Get parent issue
Issue pIssue = issue.getParentObject();
CustomField parentTotalAmount = customFieldManager.getCustomFieldObject("customfield_11256");
CustomField subTotalAmount = customFieldManager.getCustomFieldObjectByName("myfield");
MutableIssue mutableIssue = (MutableIssue) pIssue;
//update parent issue
mutableIssue.setCustomFieldValue(parentTotalAmount, Double.parseDouble("200.23")); //It didn't work
//update sub task
issue.setCustomFieldValue(subTotalAmount, Double.parseDouble("123.45")); // It worked 

Suggest an answer

Log in or Sign up to answer