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-127.0.0.1-8080-exec-25 WARN username 1051x82316x1 5n1r8 12.31.137.4:25256,127.0.0.1 /secure/QuickCreateIssue.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] ASZ Timeline Value: 1 - Immediately
2017-01-27 17:31:18,952 http-nio-127.0.0.1-8080-exec-25 WARN username 1051x82316x1 5n1r8 12.31.137.4:25256,127.0.0.1 /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.

 

Thanks!

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();
MyDueDate.add(Calendar.DATE,DueInDays)
while ([Calendar.SATURDAY, Calendar.SUNDAY].contains(MyDueDate.get(Calendar.DAY_OF_WEEK))) {
    MyDueDate.add(Calendar.DATE,1)
}
def dueTimestamp = new Timestamp(MyDueDate.getTimeInMillis())
myIssue.setDueDate(dueTimestamp);
log.warn ("ASZ SetDueDate dueTimestamp is:" + dueTimestamp)

1 answer

1 accepted

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
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
Posted 12 hours ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

35 views 0 5
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