Jira Service Desk Scriptrunner - unable to set Due Date

Jon Starbird
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.
March 21, 2019

I am pulling a date from the description from an email request to set the due date. 

I've tried it as a Listeners and Post Function, in both cases it seems to set the due date, no errors, but the due date doesn't end up being set. 

here is the Post Function code -

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import org.apache.log4j.Logger
import org.apache.log4j.Level

@WithPlugin("com.atlassian.servicedesk")

def curUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)
def sourceIssueRequestTypeQuery = requestTypeService.newQueryBuilder().issue(issue.id).build()

def requestTypeEither = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService).getRequestTypes(curUser,sourceIssueRequestTypeQuery)

if (requestTypeEither.isLeft()) {
log.error "${requestTypeEither.left().get()}"
return
}
def requestType = requestTypeEither.right.results[0]

if (requestType.name == "Employee Additions/Changes") {
def curDesc = issue.getDescription()
def lines = curDesc.split('\n')
def hdatefound = false
def curduedate = issue.getDueDate()
log.error "cur - ${curduedate}"
for (line in lines)
{
if (line.contains('Hire Date:') || line.contains('Term Date:')) {
hdatefound = true
def tdate = line.split(':')
def duedate = Date.parse('yyyy-MM-dd',tdate[1].trim()).toTimestamp()
try {
log.error "value - ${duedate}"
issue.setDueDate(duedate)
log.error "Due Date set to ${duedate} for issue ${issue.key}"
} catch (e) {
log.error "Unable to set Due Date for ${issue.key}. Error Info; ${e}"
}
}
}
if (!hdatefound) { log.error "No Hire/Term date found."}
}

The test description has several lines and the Hire Date. It is found and when it gets to the setDueDate I get no errors. Just nothing ends up being saved. 

I have it in the Post Functions just after the Creates Issue Originally step. I did try putting it first but that doesn't work either and nothing gets logged so when it's first it won't get beyond the Request Type check.

1 answer

1 accepted

0 votes
Answer accepted
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 22, 2019

In addition to issue.setDueDate(dueDate), the changes also need to be saved using the IssueManager.updateIssue() method. I'd try this code and see how it works out:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import org.apache.log4j.Logger
import org.apache.log4j.Level

@WithPlugin("com.atlassian.servicedesk")

def curUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)
def sourceIssueRequestTypeQuery = requestTypeService.newQueryBuilder().issue(issue.id).build()
def issueManager = ComponentAccessor.getIssueManager()

def requestTypeEither = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService).getRequestTypes(curUser,sourceIssueRequestTypeQuery)

if (requestTypeEither.isLeft()) {
log.error "${requestTypeEither.left().get()}"
return
}

def requestType = requestTypeEither.right.results[0]

if (requestType.name == "Employee Additions/Changes") {
def curDesc = issue.getDescription()
def lines = curDesc.split('\n')
def hdatefound = false
def curduedate = issue.getDueDate()
log.error "cur - ${curduedate}"
for (line in lines)
{
if (line.contains('Hire Date:') || line.contains('Term Date:')) {
hdatefound = true
def tdate = line.split(':')
def duedate = Date.parse('yyyy-MM-dd',tdate[1].trim()).toTimestamp()
try {
log.error "value - ${duedate}"
issue.setDueDate(duedate)
issueManager.updateIssue(curUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.error "Due Date set to ${duedate} for issue ${issue.key}"
} catch (e) {
log.error "Unable to set Due Date for ${issue.key}. Error Info; ${e}"
}
}
}
if (!hdatefound) { log.error "No Hire/Term date found."}
}
Jon Starbird
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.
March 22, 2019

Thanks. I had just figured that part out myself and got it working in a listener.

Suggest an answer

Log in or Sign up to answer