Conditionally Copy Values to Parent from Subtask w/ ScriptRunner Post-Function

Andrew Fluck March 13, 2018

So I have been reading Q&A in the community for days now in the hopes that I'd be able to muddle through this 'simple' post-function script. I have finally decided that I need to ask the community for help.

Here's what I'm trying to accomplish:

I have a parent task with multiple sub-tasks. In the sub-task workflow, I would like a custom script post-function which evaluates the issue type name, and based on that name, then assigns the current date to a specific custom field in the parent issue. Also, based on the issue type name, the status the sub-task is transitioning to is assigned to a specific custom field in the parent issue.

I was thinking this would be a good application for using 'switch'. Below is what I have so far, and I'm sure it's way off base. Any help would be appreciated. I've commented-out some of the lines as I've tried working through this.

Thanks!

 

 

 

 

 

 

import com.atlassian.jira.ComponentManager
//import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
//import com.atlassian.jira.issue.getIssueType
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.util.ImportUtils
import java.sql.Date
import java.sql.Timestamp;

//MutableIssue issue = new MutableIssue()


MutableIssue mutableIssue = issue;
MutableIssue parentIssue = mutableissue.getParentObject()
//Issue parentIssue=issue.getParentObject();

switch (issue.issueType.name) {
case "Name X":
parentissue.setCustomFieldValue(PCF1,issue.getStatus);
parentissue.setCustomFieldValue(PCF2,Timestamp);
break;

case "Name Y":
parentissue.setCustomFieldValue(PCF3,issue.getStatus);
parentissue.setCustomFieldValue(PCF4,Timestamp);
break;
}

indexManager.reIndex(parentIssue);

1 answer

1 accepted

0 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
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.
March 13, 2018

Hi Andrew,

I've slightly modified your script. Can you give it a try?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def parentIssue = issue.getParentObject()
def PCF1 = customFieldManager.getCustomFieldObjectByName("PCF1")
def PCF2 = customFieldManager.getCustomFieldObjectByName("PCF2")
def PCF3 = customFieldManager.getCustomFieldObjectByName("PCF3")
def PCF4 = customFieldManager.getCustomFieldObjectByName("PCF4")
def timeStamp = new Timestamp(new java.util.Date().time)

def changeHolder = new DefaultIssueChangeHolder()

switch (issue.issueType.name) {
case "Name X":
PCF1.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF1), issue.getStatus()), changeHolder)
PCF2.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF2), timeStamp), changeHolder)
break

case "Name Y":
PCF3.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF3), issue.getStatus()), changeHolder)
PCF4.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF4), timeStamp), changeHolder)
break
}
Joshua Yamdogo @ Adaptavist
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.
March 13, 2018

I've assumed the custom field names on the parents are "PCF1", "PCF2", etc. Please feel free to change those to the actual names if they are different.

Regards,

Josh

Andrew Fluck March 13, 2018

Hi Josh,

Thank you so much for the lightning quick response! I implemented the script on my test instance and received the following... Any thoughts on what I may be missing?

Kind Regards,

Andrew

 

2018-03-13 14:22:11,301 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************

2018-03-13 14:22:11,327 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: EGC-897, actionId: 11, file: <inline script>

java.lang.ClassCastException: com.atlassian.jira.issue.status.StatusImpl cannot be cast to java.lang.String

        at com.atlassian.jira.issue.customfields.impl.GenericTextCFType.getDbValueFromObject(GenericTextCFType.java:51)

        at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143)

        at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)

        at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)

        at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)

        at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)

      at Script2.run(Script2.groovy:19)

Joshua Yamdogo @ Adaptavist
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.
March 14, 2018

Hi Andrew,

I think this is because we need to write issue.status.name instead of just issue.status.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def parentIssue = issue.getParentObject()
def PCF1 = customFieldManager.getCustomFieldObjectByName("PCF1")
def PCF2 = customFieldManager.getCustomFieldObjectByName("PCF2")
def PCF3 = customFieldManager.getCustomFieldObjectByName("PCF3")
def PCF4 = customFieldManager.getCustomFieldObjectByName("PCF4")
def timeStamp = new Timestamp(new java.util.Date().time)

def changeHolder = new DefaultIssueChangeHolder()

switch (issue.issueType.name) {
case "Name X":
PCF1.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF1), issue.status.name), changeHolder)
PCF2.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF2), timeStamp), changeHolder)
break

case "Name Y":
PCF3.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF3), issue.status.name), changeHolder)
PCF4.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF4), timeStamp), changeHolder)
break
}

 Regards,

Josh

Andrew Fluck March 14, 2018

Hi Josh,

I can't thank you enough for your help on this, it works exactly as I had hoped. I took your suggestion and also added .toString() as shown in the final script below. 

Kind regards,

Andrew

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def parentIssue = issue.getParentObject()
def PCF1 = customFieldManager.getCustomFieldObjectByName("PCF1")
def PCF2 = customFieldManager.getCustomFieldObjectByName("PCF2")
def PCF3 = customFieldManager.getCustomFieldObjectByName("PCF3")
def PCF4 = customFieldManager.getCustomFieldObjectByName("PCF4")
def timeStamp = new Timestamp(new java.util.Date().time)

def changeHolder = new DefaultIssueChangeHolder()

switch (issue.issueType.name) {
case "Name X":
PCF1.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF1), issue.status.name.toString()), changeHolder)
PCF2.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF2), timeStamp), changeHolder)
break

case "Name Y":
PCF3.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF3), issue.status.name.toString()), changeHolder)
PCF4.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(PCF4), timeStamp), changeHolder)
break
}
Joshua Yamdogo @ Adaptavist
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.
March 14, 2018

Glad to hear it works, Andrew! Feel free to select my answer as the "Best answer" so that others on the Community can find it more easily.

Thanks for using ScriptRunner!

Suggest an answer

Log in or Sign up to answer