• Community
  • Products
  • Jira Core
  • Questions
  • Can I have a conditional postfunction, that will clear the field "Flagged" of parent issue ONLY if parent issues has no flagged subtasks (Alternatively: has no subtatsk with status "impeded")?

Can I have a conditional postfunction, that will clear the field "Flagged" of parent issue ONLY if parent issues has no flagged subtasks (Alternatively: has no subtatsk with status "impeded")?

Maybe someone can help me creating the correct expression for a conditional execution of a post function?

My workflow is
To Do --> In Progress --> Impeded --> Done

I have made following post functions on sub-tasks transitions TO "Impeded"
A. Set Flagged to "impediment" (on sub-task)
B. Copy value of Flagged to parent issue replacing existing values

Post functions on sub-task transitions FROM Impeded
C. Clear Flagged (on sub-task)
D. Copy value of Flagged to parent issue replacing existing values

All seems to be working fine however:
I want a conditional postfunction, that will copy value of "Flagged" to parent issue ONLY if parent issues has no flagged subtasks (Alternatively: has no subtatsk with status "impeded")

I have no experience in groove expression :(
Can anyone help?

/Jacob

1 answer

0 vote
Mahesh S Community Champion Jun 14, 2016

Hope this works. Please let me know the results.

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.comments.CommentManager;
import com.opensymphony.workflow.WorkflowContext;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.config.SubTaskManager;
import com.atlassian.jira.util.JiraUtils;
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField myCustomfield = customFieldManager.getCustomFieldObjectByName("Flagged") 
myCustomfield.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(myCustomfield), "impediment"),changeHolder);

SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
Collection subTasks = issue.getParentObject().getSubTaskObjects()
def count = 0

if (subTaskManager.subTasksEnabled && !subTasks.empty) 
{
subTasks.each 
{
String status = it.getStatus().get("name").toString()
if(status.equals("impeded"))
{
count = count++
}
}
}
if(count != 1)
{
myCustomfield.updateValue(null, issue.getParentObject(), new ModifiedValue(issue.getParentObject().getCustomFieldValue(myCustomfield), "impediment"),changeHolder);
}

Thanks Mahesh S.

It didn't quite work as expected.

I have used  the "Copy Field Value to Parent Function" and inserted you script in the COnditional Execution field - see below

I guess the script is supposed to "return true" if parent has no subtask with status impeded? 

WorkflowFunctionCondition.PNG

Mahesh S Community Champion Jun 14, 2016

You are in Copy Field Value to Parent function now. You can move out and use my first script directly in a scripted post function.

However, please use this script, if you need to use the script as a condition for this post function itself.

import com.atlassian.jira.ComponentManager
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.util.JiraUtils

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
/*CustomField myCustomfield = customFieldManager.getCustomFieldObjectByName("Flagged")
myCustomfield.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(myCustomfield), "impediment"),changeHolder);*/
//hope you did this in a separate post function above this.
 
SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
Collection subTasks = issue.getParentObject().getSubTaskObjects()
def count = 0
def boolean = false
 
if (subTaskManager.subTasksEnabled && !subTasks.empty)
{
subTasks.each
{
String status = it.getStatus().get("name").toString()
if(status.equals("impeded"))
{
count = count++
}
}
}
if(count == 1)
{
boolean = true
}
return boolean

Please let me know the results.

Hi Mahesh

I have tried using your second script as a condition for the post fuction it self (Yes i added the "Clear flag on current item" as a seperate post fuction... No positive result.

Since this is hard to debug, i tried adding your first script as a script postfunction (removing the clear flag and copy value to parent post function)... 

Following errors occured:

The script could not be compiled:
 
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Script4.groovy: 12: unable to resolve class IssueManager 
 @ line 12, column 14.
   IssueManager issueManager = ComponentAccessor.getIssueManager()
                ^

Script4.groovy: 15: unable to resolve class ModifiedValue 
 @ line 15, column 40.
   myCustomfield.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(myCustomfield), "impediment"),changeHolder);
                                          ^

Script4.groovy: 34: unable to resolve class ModifiedValue 
 @ line 34, column 58.
   ull, issue.getParentObject(), new Modifi
                                 ^

3 errors
Mahesh S Community Champion Jun 14, 2016

Add these imports as well ,

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.IssueManager

let me know the remaining errors, I will try with a hands-on and update you.

Hi again

Thanks for your patience on this smile
Still not working.
When dragging a sub task from "impeded" to In Progress" flags are NOT cleared on sub-task and flag is NOT clear on Parent (even if no other subtask are

In the inline script box the scrip now produce "error markers" as follows, though now i am able to actually add the script

image2016-6-15 11:10:42.png

image2016-6-15 11:13:36.png

 

image2016-6-15 11:13:58.png

image2016-6-15 11:14:55.png

image2016-6-15 11:15:26.png

image2016-6-15 11:15:56.png

image2016-6-15 11:16:21.png

image2016-6-15 11:16:43.png


After saving/adding the script, and execute it by moving a card from "impeded" to "in progress" 
Following information were registered in the log... 

ime (on server): Wed Jun 15 2016 10:58:49 GMT+0200 (Romance Daylight Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2016-06-15 10:58:49,264 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-06-15 10:58:49,264 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SUP-11, actionId: 51, file: <inline script>
groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: Script12
	at Script12.run(Script12.groovy:14)

 

 

 

 

Mahesh S Community Champion Jun 14, 2016

Oh!! I will try it with a hands-on soon and come back with the results.

Mahesh S Community Champion Jun 15, 2016

Hold on!! This script is for our requirement during the subtask's transition from In Progress to Impeded status. This has to be added in the post function available in the In Progress -> Impeded transition step. Make sure the script is placed just below this post function 'Set issue status to the linked status of the destination workflow step.'

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.comments.CommentManager;
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.util.JiraUtils
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField myCustomfield = customFieldManager.getCustomFieldObjectByName("Flagged")
myCustomfield.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(myCustomfield), "impediment"),changeHolder)
SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
Collection subTasks = issue.getParentObject().getSubTaskObjects()
def count = 0
String currentStatus = issue.getStatus().get("name").toString()
if (subTaskManager.subTasksEnabled && !subTasks.empty)
{
	subTasks.each
	{
		String status = it.getStatus().get("name").toString()
		if(status.equals("Impeded"))
		{
			count = count++
		}
	}
}
if(count == 1)
{
	myCustomfield.updateValue(null, issue.getParentObject(), new ModifiedValue(issue.getParentObject().getCustomFieldValue(myCustomfield), "impediment"),changeHolder);
}

For the post function of Impeded to In Progress, I will share the script separately. Now please test this script for the In Progress -> Impeded transition from the subtask ALONE !!

I have now added the script to the transition In "Progress --> Impeded" . Still the same errors as mentioned in my previous comment.... But even if it worked, it would really not solve my problem...

 I think I need to clarify my original intensions further as it might not have been all that clear...


I was able to succesfully create the following Post funtions:

Post function on Transtion: In Progress --> Impeded:

  1. The Flagged of the issue will be set to Impediment. (Update Issue Custom Field Function)
  2. The value(s) of field Flagged will be copied to the issue's parent's field (replacing existing values). (Copy Field Value to Parent Function)

This allows me to verify the following when a subtaks is moved from in progress to Impeded:

  • Flag will be set on Sub-task - (Works as intended)
  • Flag will be Copied to Parent issue- (Works as intended)

 

I was also able to succesfully create the following Post functions 

Post function for transition:  Impeded --> In Progress 

  1. The contents of the field Flagged will be purged. (Clear Field Value Function)
  2. The value(s) of field Flagged will be copied to the issue's parent's field (replacing existing values).(Copy Field Value to Parent Function)

This Allow me to verify the following when a subtask is moved from Impeded to in Progress:

  • Flag will be removed on sub-taks (Works as intended)
  • Flag will be removed from parent issue (Works fine, but this is the challenging part)

The challenging part is that I only want to remove the flag from the parent issues, if the parent issue has no sub-tasks marked as Impeded/flagged.  

 

Therefore my issue lies only on the transition from Impeded --> In progress. I want to replace the second postfunction with one like this:

    • The value(s) of field Flagged will be copied to the issue's parent's field (replacing existing values) if parentIssue has 0 sub-tasks marked as Impeded/Flagged
      (Copy Field Value to Parent Function with a condition)

In other word, i need help to write the condition only, as all other postfunction works for me... 

Does this helps you in any way?

Mahesh S Community Champion Jun 15, 2016

Yes. Thanks a lot for your patience as well. smile

Alas, lets give a try again with your approach. Lets focus on the first scenario alone on transition step Impeded --> In Progress . Lets add a condition in your Copy Field Value to Parent Function as given below.

import com.atlassian.jira.component.ComponentAccessor;
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.config.SubTaskManager

boolean result = true
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
Collection subTasks = issue.getParentObject().getSubTaskObjects()
def count = 0

if (subTaskManager.subTasksEnabled && !subTasks.empty)
{
	subTasks.each
	{
		String status = it.getStatus().getName().toString()
        //log.error(status)
		if(status.equals("In Progress"))
		{
			count = count+1
            //log.error(count)
		}
	}
}
if(count != 1)
{
	result = false
}
return result

Make sure the script is placed just below this post function 'Set issue status to the linked status of the destination workflow step.'

 Is it working?

Well no succes... sad In my desperation of finding out why this doesn't work i tried different random stuff, including delelting a lot of code. During that process i discovered something really weird...

I found that the following script obviously returns true and therefore executing the post function, since it is only imports and a "return true"

import com.atlassian.jira.component.ComponentAccessor;
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.config.SubTaskManager
 
//boolean result = true
//SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
//Collection subTasks = issue.getParentObject().getSubTaskObjects()
return true

However, for some reason, the script below doesn't execute the post function, so apperantly this doesn't return true.

import com.atlassian.jira.component.ComponentAccessor;
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.config.SubTaskManager
 
//boolean result = true
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
Collection subTasks = issue.getParentObject().getSubTaskObjects()
return true

I may be overlooking something, as my knowledge in scripting is very limited, but it seems like adding those two lines, for some reason "overwrites" the return true...??

I have no clue if this can help the debugging process... 

Mahesh S Community Champion Jun 15, 2016

The last script of mine had worked for me. Can you please share a screenshot of your post function used and all postfunction's order in that transition?

image2016-6-16 15:23:29.png
Here you go... Your script has been added as condition to the function marked with X

Mahesh S Community Champion Jun 16, 2016

1) How are you having a IN PROGRESS status on both the sides? IN PROGRESS -> IN PROGRESS doesn't seems valid.

2) Hope you have created the workflow from the diagramatic view. Here, these post functions will be shared while other transitions like TO DO -> IN PROGRESS as well.

However, if my script works fine here, the result will be,

  • Flagged value will be purged in the subtask
  • Subtask Transition happens to In Progress
  • Sets new status as In Progress in Subtask
  • The flagged value will be set as null to parent also.
  • and so on....

    Is this correct?

Also share a screenshot of your Copy field value to Parent post function you have configured.

 

1) My workflow allows all statuses to transition into all statusses. It looks like this:
image2016-6-16 20:5:30.png 

I believe this should be valid, as it is a standard this to "allow all statusses to transition to this status".

 

2) Yes... You are right.

Here is the screen shot of the Postfunction configuration of the Copy Field Value to Parent function:

image2016-6-16 20:10:30.png

 

Mahesh S Community Champion Jun 16, 2016

This means that your code is working fine for that scenario. Lets check the other scenario from In Progress -> Impeded state. Please set the post functions in this order.

  • Set field value for Flagged as 'Impediment'.
  • Transition the subtask (system)
  • Set the new status as Impeded. (system)
  • Value of the field Flagged should be copied to the issue's parent field with this script condition.

    import com.atlassian.jira.component.ComponentAccessor;
    import com.opensymphony.workflow.WorkflowContext
    import com.atlassian.jira.ComponentManager
    import com.atlassian.jira.issue.MutableIssue
    import com.atlassian.jira.issue.Issue
    import com.atlassian.jira.config.SubTaskManager
    
    boolean result = true
    SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
    Collection subTasks = issue.getParentObject().getSubTaskObjects()
    def count = 0
    
    if (subTaskManager.subTasksEnabled && !subTasks.empty)
    {
        subTasks.each
        {
            String status = it.getStatus().getName().toString()
            //log.error(status)
            if(status.equals("Impeded"))
            {
                count = count+1
                //log.error(count)
            }
        }
    }
    if(count != 1)
    {
        result = false
    }
    return result

    and so on.

Test 1:
Check this post function when no other subtasks are in Impeded state.
Expected result : Script returns true and hence Flagged is set as 'Impediment' in parent as well.

Test 2:
Check this post function when other subtasks are in Impeded state.
Expected result : Script returns false and post function fails. (Flagged wont be copied).

Please try it.

Have to go abroad for a few weeks.  I'll look more into this and get back your you when I am back.  Thanks for your patience :) 

Mahesh S Community Champion Jun 17, 2016

Alright.. Happy journey!! smile

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

79 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you