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)

2 answers

1 accepted

1 vote
Accepted answer

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.

Trying to Automatically assign a due date to all created ticket excluding weeks.

I tried this script but giving me an error for the first 3 lines
unable to resolve class com.atlassian.jira.component.componentaccessor

Im using Script runner cloud and just want to make sure all created tickets are automatically set for 5 days not including weekends.

im not using the custom field im try to change the due date already integrated

 

```

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("Due Date")
def cfTimelineValue = issue.getCustomFieldValue(cfTimeline).toString()
// Days in the future when the issue is due. 
int DueInDays = 5
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 = 5; 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)

 

```

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,723 views 17 21
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