Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Service Desk Scriptrunner extract substring from Description

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 20, 2019

I have a listener on the Create Event for a certain request type and I want to pull a substring from the description to use for the due date. This is done because this particular request type is only coming in via email. 

The problem I'm having is that in the Script Console my code works but when it's in the listener it doesn't find the substring using the same code. 

The sample description looks like this:

New hire Joe Smith. Setup all accounts.
Hire Date: 2019-04-29
Manager: Bob Johnson

Here is the code i'm using:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.operation.IssueOperations
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.issue.comments.Comment;
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(event.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]

def issue = event.issue as MutableIssue
log.error "${requestType.name}"

if (requestType.name == "Employee Additions/Changes") {

def curDesc = event.issue.getDescription()
log.error "${curDesc}"
def findstr = (curDesc =~ /(?m)^Hire Date:.+$/)
if (!findstr) {
log.error "didn't find hire"
findstr = (curDesc =~ /(?m)^Term Date:.+$/)
}

if (findstr) {
log.error "found the string"
def tmp = findstr[0]
def tdate = tmp.split(':')
def duedate = tduedate[1].trim()
try {
issue.setDueDate(duedate)
log.error "Due Date set for ${issue.key}"
} catch (e) {
log.error "Unable to set Due Date for ${issue.key}. Error Info; ${e}"
}
}
else
{ log.error "No Hire/Term date found."}
}

I've debugged it down to find it doesn't find the substring of Hire Date so I end up at the no hire/term date found. However, when I do this same code to find the substring it does find it. So I know that part works. my guess is it has to do with the description field itself and how it's stored, would it be considered a multiline string?

Any ideas? 

1 answer

1 accepted

0 votes
Answer accepted
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 solved this by changing the way I find the substrings. Changed to loop thru description lines and find the strings I needed.

Now just having an issue with setting due date which I will post in new question.

Suggest an answer

Log in or Sign up to answer