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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.