Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×I'd like to use Script Runner to execute a search filter periodically. Here's what I'm trying to do, if someone has another suggestion, I'm open.
Is this possible using Script Runner?
Hi, this definitely seems possible, see the following links for pointers:
https://answers.atlassian.com/questions/134531
https://answers.atlassian.com/questions/211505
https://answers.atlassian.com/questions/189532
I have created some scripts for the Script Runner to perform other tasks, it is relatively easy given all the information you can find. Good luck!
Geert
This got me on the right track. I have integrated all three concepts and have a functional solution now.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Damon Morda ,
I have a similar requirement, could you please tell me what feature u used to trigger a script?
I mean, was it Script Listener, Send a custom email feature or what?
I have the code to hit the JQL and fetch results and traverse through them.
But I dont know how to run it in the background in every 15 min or how to send an email using script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The code below will automatically send an email to the reporter once an issue meets certain criteria specified in a JQL query. It works by doing a JQL search for issues that don't have a value of "true" for a custom field we created called "Survey Sent". When the script runs, it will send emails to all the issues returned and then update that custom field to "true".
Implementation Steps
You will need to modify some of the code below such as the JQL search. I put <> around anything to represent it's a variable you'll want to edit.
// This groovy script automatically sends a customer satisfaction survey // to the reporter once an issue has been resolved and not updated for a // defined period of time. import com.atlassian.crowd.embedded.api.User; import com.atlassian.jira.bc.issue.search.SearchService; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.util.IssueChangeHolder import com.atlassian.jira.user.util.UserUtil; import com.atlassian.jira.util.ImportUtils import com.atlassian.jira.web.bean.PagerFilter; import com.atlassian.mail.Email; import com.atlassian.mail.server.MailServerManager; import com.atlassian.mail.server.SMTPMailServer; // Setup the various variables jqlSearch = '<project = "IT Help Desk" AND "Survey Sent" is EMPTY AND status in (Resolved,Closed) AND issuetype NOT IN (Task, Sub-task) AND updated <= -2d>'; // Create the JQL query to perform theUser = "<your cron user>"; // Specify the user that should execute the query if not logged in surveyField = "Survey Sent"; // The field used to determine if the survey was already sent surveySentValue = "true"; // The value to stick in the custom field to indicate a survey was sent SearchService searchService = ComponentAccessor.getComponent(SearchService.class); UserUtil userUtil = ComponentAccessor.getUserUtil(); User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); IssueManager issueManager = ComponentAccessor.getIssueManager(); def componentManager = ComponentManager.instance def customFieldManager = componentManager.getCustomFieldManager(); def changeHistoryManager = ComponentAccessor.getChangeHistoryManager(); // 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); // Loop through each issue returned in the JQL and set a value for the custom field "Survey Sent" if (parseResult.isValid()) { def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter()); // Setup mail server MailServerManager mailServerManager = componentManager.getMailServerManager(); SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer(); // Transform issues from DocumentIssueImpl to the "pure" form IssueImpl (some methods don't work with DocumentIssueImps) issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)} // Loop through each issue an update the Survey Sent cusotm field to indicate the survey has been sent issues.each() { Issue issue -> MutableIssue myIssue = issueManager.getIssueObject(issue.id); def tgtField = customFieldManager.getCustomFieldObjects(myIssue).find {it.name == surveyField} def changeHolder = new DefaultIssueChangeHolder(); tgtField.updateValue(null, myIssue, new ModifiedValue(issue.getCustomFieldValue(tgtField), surveySentValue),changeHolder); // Reindex the field because if you don't, when the JQL search runs again it might not pick up on the // fact that you put "true" in the "Survey Sent" custom field. boolean wasIndexing = ImportUtils.isIndexIssues(); ImportUtils.setIndexIssues(true); ComponentAccessor.getIssueIndexManager().reIndex(issue); ImportUtils.setIndexIssues(wasIndexing); // Setup the reporter variables reporter = userUtil.getUserByName(issue.reporter.name); // Get the reporter object reporterEmail = reporter.getEmailAddress(); // Get the reporter email address reporterFirstName = reporter.displayName; // Get the reporter full name // If the mail server configuration is valid, send an email. if (mailServer) { Email email = new Email(reporterEmail); // Set the TO address, optionally CC and BCC email.setSubject("(${issue.key}) Customer Service Survey"); // Set the SUBJECT email.setFrom("IT Help Desk <ithelpdesk@yourdomain.com>"); // Set the FROM address String content = """\ Hello ${reporterFirstName}, Ticket: ${issue.key} Summary: ${issue.getSummary()} IT recently resolved your ticket and we hope you were happy with our support. We would appreciate it if you could take a quick 6 question survey to tell is about your experience. <some url> Your feedback is important as we continuously strive to improve the services provided to our user community. Thanks in advance for helping us help you. IT Help Desk ithelpdesk@mydomain.com (555) 555-1212"""; email.setBody(content) mailServer.send(email) } else { log.error "[Send Survey Cron] No SMTP mail server defined."; } } } else { // Log errors if invalid JQL is used so we can fix it log.error("[Send Survey Cron] Invalid JQL: " + jqlSearch); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi ,
After adding the service in JIRA , i am getting the below error in the logs.
JIRA Version = 7.0.9
2016-03-14 13:11:00,025 Caesium-1-1 ERROR anonymous Email_Esclation [c.o.jira.groovy.GroovyService] Script service failed: /var/atlassian/application-data/jira/scripts/sendsurvey.groovy
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.ComponentManager.getCustomFieldManager() is applicable for argument types: () values: []
at sendsurvey.run(sendsurvey.groovy:34)
2016-03-14 13:11:00,427 Caesium-1-4 ERROR vijay.sridhar [c.a.p.automation.scheduler.RuleCallable] Exception while executing the rule
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
at com.tuncaysenturk.jira.plugins.enhancer.customfield.sla.c.a(SlaIndicatorCFType.java:85)
at com.tuncaysenturk.jira.plugins.enhancer.customfield.a.getValueFromIssue(AbstractJepCFType.java:136)
at com.atlassian.jira.issue.fields.CustomFieldImpl.getValue(CustomFieldImpl.java:386)
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
at com.atlassian.jira.issue.IssueImpl.setCustomFieldValue(IssueImpl.java:906)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateIssue(CustomFieldImpl.java:1074)
at com.atlassian.jira.bc.issue.DefaultIssueService.updateIssueFromFieldsWithoutScreenCheck(DefaultIssueService.java:702)
at com.atlassian.jira.bc.issue.DefaultIssueService.updateIssueFromFields(DefaultIssueService.java:661)
at com.atlassian.jira.bc.issue.DefaultIssue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The first error is because you use ComponentManager instead of ComponentAccessor. The right way to get the customFieldManager is
import com.atlassian.jira.component.ComponentAccessor def customFieldManager = ComponentAccessor.getCustomFieldManager()
Now for your second error I will need more information (the code in the line that exception is thrown). But I think it depends on the type of your custom field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
After changing to ComponentAccessor. it's working fine .
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.util.DefaultUserManager
def tester= userUtil.getUserByName(issue.tester.name); // Code Executing Successfully ,
Getting Below Error if changed to UserManager.getUserByName(issue.tester.name);
Error.png
No signature of method: static com.atlassian.jira.user.util.UserManager.getUserByName() is applicable for argument types: (java.lang.String) values: [vijay.sridhar]
Any other things needs to changed for using UserManager to get the name ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can I ask you why you need this ? I mean,
ApplicationUser getReporter = issue.reporter will return to you exactly the same object with
ApplicationUser getReporter = userManager.getUserByName(issue.reporter.username)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Was using UserManager to get the reporter name which shows warning symbol during compile ,after using "" ApplicationUser getReporter = issue.reporter " the warning has gone
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.