Error in groovy script in Scriptrunner

Nagappan Murugappan February 24, 2021

I am referring to the script written down under the link --> 

https://community.atlassian.com/t5/Jira-questions/Using-Jira-Script-Runner-s-Escalation-Service-to-send-an-email/qaq-p/871519

 

I wanted to send email for every issue result returned from the search query used. But it looks like the class or methods used is depreciated now, could some suggest alternate fetch option ?

The error description, please find the attached screen shot.

 

Thanks.ErrorDesc.PNG

3 answers

1 accepted

0 votes
Answer accepted
Dirk Ronsmans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 24, 2021

hey @Nagappan Murugappan ,

it could be the error doesn't really mean anything. I noticed you defined your array just as def and thus didn't specify a type that will go into ti.

I assume that below the code you loop through the issues?

if so the interpreter cannot find what the type is and thus not find a proper method. I would suggest to properly cast/define your variable first so your methods are found in those classes

0 votes
Nagappan Murugappan February 25, 2021

@Kristian Walker _Adaptavist_ Thanks for your support, it was very helpful. 

0 votes
Nagappan Murugappan February 24, 2021

Hi Dirk

Thanks for your reply.

I think the screen shot was hiding the code, here is the complete code. (from the link above)

 

import com.atlassian.mail.Email
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter

def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = "project = foobar AND Updated < -2w AND resolution is empty AND status != Backlog ORDER BY status ASC"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// eventually this array will contain all the issues that returned from the JQL
def issues = []

// hashmap to store username and associated issue(s)


def map = [:].withDefault {
[]
} //The default value will be created every time a non-existing key is accessed.
// Need this to be able to dynamically expand/update map


SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}


// We are breaking out the issues per assignee. Will use later when we email the assignees
issues.each {
def assignee = it.assigneeId

map[assignee].add(it)

}

// following section creates separate emails for each assignee from the map, including all related issues in the same email.

map.each { key, value ->
def assignee = key
def theirIssues = value as List


def assigneeEmail = ""
String cc = "someguy@somedomain.com"
String bcc = ""

def body = "The following issues not been updated in over two weeks. " +
"Please review and update your assigned issues with comments, updated status and (if available) a due date." +
"<br/><br/>" +
"<table>\n" +
" <tr>\n" +
" <th>Issuekey</th>\n" +
" <th>Summary</th>\n" +
" <th>Assignee</th>\n" +
" </tr>"

 

theirIssues.each {

assigneeEmail = it.assignee.emailAddress

body += " <tr>\n" +
" <td><a href=\"https://somejirainstance.com/browse/${it.key}\">${it.key} </a></td>\n" +
" <td>${it.getSummary()}</td>\n" +
" <td>${it.assigneeId}</td>\n" +
" </tr>"
}

body += "</table>\n" +
"\n" +
"<br></br>\n" +
"Regards, <br></br>\n" +
"\n" +
"Jira Automation"


sendEmail(assigneeEmail, cc, bcc, "The following issues need your assistance", body)


}

// Create an email
def sendEmail(String emailAddr, String cc, String bcc, String subject, String body ) {
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
def email = new Email(emailAddr)
email.setMimeType("text/html")
email.setSubject(subject)
email.setBody(body)
email.setCc(cc)
email.setBcc(bcc)
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}

Dirk Ronsmans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 24, 2021

Hmm so seems the line(s) that are giving you issues are:

def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }

 

Could you clarify if you are using Cloud or Server? I'm a bit confused about that.. and if Server what version?

Nagappan Murugappan February 24, 2021

Yes, you are the correct, that's where the error appears. 

I am using cloud version - 

  • v8.13.1
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 24, 2021

Hi Nagappan,

I can confirm version 8.13.1 is a Jira Server version and your screenshots show your code is from Jira Server.

You will only be using Jira Cloud if your instance URL ends in .atlassian.net.

Regards,

Kristian

Like Nagappan Murugappan likes this
Nagappan Murugappan February 25, 2021

Hi Kristian

Sorry About the confusion,  Yes its on Server, but we have deployed this version on our AWS environment, that's where I got confused. 

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

Hi Naggappan,

Thank you for your response and no problem. 

I can confirm then you should be able to make the changes as suggested by Dirk above and I can confirm that the warnings you are seeing are just static type checking warnings as documented here and these may not stop the script from running, so I would advise you to test the script and to see if it still runs despite these warnings.

Regards,

Kristian

Like Nagappan Murugappan likes this
Dirk Ronsmans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

so I'm going through the Javadocs for 8.13.1 

https://docs.atlassian.com/software/jira/docs/api/8.13.1/com/atlassian/jira/bc/issue/search/SearchService.html#search-com.atlassian.jira.user.ApplicationUser-com.atlassian.query.Query-com.atlassian.jira.web.bean.PagerFilter-

So when we look at the search method this returns a SearchResults object (List), on that class shouldn't you use the getResults() method then?

 

(I could be missing the point completely here) but that seems more logical to me.

A hint would be to output first a log where you print searchResult.getClass().toString() so we can be sure what is in there and then maybe just a searchResult.toString() so we can see the List ?

Nagappan Murugappan February 25, 2021

Hello Dirk

Please find the results below,

if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.getClass().toString()
log.warn(issues)
}

WARN [runner.ScriptBindingsManager]: class com.atlassian.jira.issue.search.SearchResults

 

if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.getClass().toString()
log.warn(issues)
}

WARN [runner.ScriptBindingsManager]: com.atlassian.jira.issue.search.SearchResults@41d98968

 

 

Actually with getResults, i could see the output :

if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.getResults()
log.warn(issues)
}

 

WARN [runner.ScriptBindingsManager]: [DocumentIssueImpl[issueKey=TEST-7], DocumentIssueImpl[issueKey=TEST-1], DocumentIssueImpl[issueKey=TEST-8]]

Dirk Ronsmans
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2021

Well then I think you could rewrite your script to use the searchResult.getResults() no?

The first 2 calls were just to see what it would output. We now know the Class and we also see that there is an object inside. (this appears to be the List).

and in the last call we actuallly have the List in there so you can now loop through the List and get the data you want.

Isn't that what you needed?

Nagappan Murugappan February 25, 2021

Thanks Dirk. Yes thats exactly i wanted to know, and its working.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events