Hi
Still new to Groovy - i am wondering if my code is incorrect as my groovy script does not seem to want to execute, i did have it as a script listener but for company purposes it needs to be changed to a service. The only thing i did when i changed it from listener to service was add the JQL search part, looking through it - it all seems to be fine (to the best of my knowledge anyhow). My code is below - (things are named test as to protect company name etc)
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager;
import java.util.logging.Logger
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug("start of code")
def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11401)
def cFieldValue = issue.getCustomFieldValue(estField)
log.debug(cFieldValue)
jqlSearch = 'updated ≥ -4w'; // Create the JQL query to perform
theUser = "jira_bot"; // Specify the user that should execute the query if not logged in
SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
UserUtil userUtil = ComponentAccessor.getUserUtil();
User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
// If the user isn't specified, then set it to the default script running user
if (!user) {
user = userUtil.getUserObject(theUser);
}
// Set the default list of issues to null just to be safe
List<Issue> issues = null;
// Perform the search as a user and return the result so it can be validated.
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);
Issue issue = event.issue
if(parseResult.isValid()){
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
issue.each(){
if ( cFieldValue == 0.0) {
def subject = "${issue.key} has no Estimate"
def body = "https://test.atlassian.net/issue/${issue.key} \n This issue does not have an estimate"
def emailAddr = "test@test.com"
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr);
email.setSubject(subject);
email.setBody(body.toString());
mailServer.send(email);
log.debug("Mail sent")
}
else {
log.debug("Please make sure that a valid mailServer is configured")
}
}
}
}
else{
log.error("Invalid JQL: " + jqlSearch);
}
Also this is a screenshot of my service i am executing-

