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

Andrew Zbikowski January 27, 2017

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
Answer accepted
Andrew Zbikowski January 27, 2017

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

JamieA
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.
February 3, 2017

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

0 votes
Danison Rarama November 9, 2018

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