Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,462,557
Community Members
 
Community Events
176
Community Groups

Error in groovy script in Scriptrunner

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 Feb 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

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

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 Feb 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?

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

I am using cloud version - 

  • v8.13.1

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

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. 

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 Feb 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 ?

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 Feb 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?

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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events