Using ScriptRunner Post Function to set issue Due Date based on custom field

I'm totally new to ScriptRunner and Groovy. I'm trying to automatically set an issue's Due Date based on a custom field. My Groovy script is set as a post function that runs on Issue Create in the workflow.

So far I've come up with the following based on a previous question from 2013. My script runs, I get the expected results in the log file, but the Due Date on the issue isn't actually set.

Log entries: 

2017-01-27 17:31:18,905 http-nio- WARN username 1051x82316x1 5n1r8, /secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] ASZ Timeline Value: 1 - Immediately
2017-01-27 17:31:18,952 http-nio- WARN username 1051x82316x1 5n1r8, /secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] ASZ SetDueDate dueTimestamp is:2017-01-30 17:31:18.936

I feel like I'm missing something obvious, but I've been teaching myself some Groovy and trying to get this to work all afternoon. Hopefully someone who knows JIRA/Groovy better than I do spots what I'm missing.



import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfTimeline = customFieldManager.getCustomFieldObjectByName("Timeline")
def cfTimelineValue = issue.getCustomFieldValue(cfTimeline).toString()
// Days in the future when the issue is due. 
int DueInDays = 0
log.warn ("ASZ Timeline Value: "+ cfTimelineValue )
MutableIssue myIssue = issue;
// Set the number of days. 
switch (cfTimelineValue) {
         case "1 - Immediately": DueInDays = 1; break;
         case "2 - Today": DueInDays = 2; break;
         case "3 - Business Week": DueInDays = 5; break;
         case "4 - 30 Days": DueInDays = 30; break;
         default: DueInDays = 365; break;
// Make sure the due date doesn't fall on a weekend. 
Date today = new Date();
Calendar MyDueDate = Calendar.getInstance();
while ([Calendar.SATURDAY, Calendar.SUNDAY].contains(MyDueDate.get(Calendar.DAY_OF_WEEK))) {
def dueTimestamp = new Timestamp(MyDueDate.getTimeInMillis())
log.warn ("ASZ SetDueDate dueTimestamp is:" + dueTimestamp)

1 answer

1 accepted

This widget could not be displayed.

Well figured it out: Add Store updates to the post functions in the workflow. Script was working exactly as expected!

I would remove the store, and just move the post-function to the top of the list.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

193 views 1 3
Join discussion

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