Hi everyone, I wrote a validator script that would check all the dates of tickets within a certain project and issuetype. My script is posted below:
ScriptRunner workflow function - Simple scripted validator (condition apply). import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.attachment.Attachment; import com.atlassian.jira.issue.MutableIssue import com.opensymphony.workflow.InvalidInputException; import com.atlassian.crowd.embedded.api.User; import com.atlassian.jira.user.ApplicationUser; import com.atlassian.jira.bc.issue.search.SearchService; import com.atlassian.jira.web.bean.PagerFilter; import org.apache.log4j.Logger; import org.apache.log4j.Category; import java.lang.Object boolean check =0; int m1=0; int y1=0; // Create Logger for Level DEBUG def Category log = Category.getInstance("com.onresolve.jira.groovy") log.setLevel(org.apache.log4j.Level.DEBUG) // Get Information about Month End (Day and Month) CustomField MonthEnd = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Month End Date") def MonthEndDate = (issue.getCustomFieldValue( MonthEnd ) as Date) if(MonthEndDate!=null){ m1 = MonthEndDate.getMonth() y1 = MonthEndDate.getYear() } def ProjectID = issue.getProjectObject().getKey() ?: ""; def issueType = "\"Month End\" "; def project = "\"Finance\" "; // Abort if something is wrong with the received Information if(MonthEndDate == null){ log.debug("There is something wrong with the Month End Date!") def invalidInputException = new InvalidInputException("There is something wrong with the Month End Date!") } if(ProjectID == ""){ log.debug("There is something wrong with the ProjectID!") def invalidInputException = new InvalidInputException("There is something wrong with the ProjectID!") } // Search for issues with same Vendor and Invoice in the same project but not the current Key! String jql = 'project = ' + project + ' AND issuetype = ' + issueType + ' AND "Month End Date" != ' + MonthEndDate // Get a search-Object for JIRA SearchService searchService = ComponentAccessor.getComponent(SearchService.class) // For JIRA 7.X must be ApplicationUser def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def issues; SearchService.ParseResult parseResult = searchService.parseQuery(user, jql) if (parseResult.isValid()) { issues = (searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues()) } else { // JQL couldn't be parsed, show Error log.debug("Invalid JQL: " + jql); def invalidInputException = new InvalidInputException("Invalid JQL: " + jql); } // Check for other Issues! if(issues!=null){ for(int i =0; i <= issues.size()-1 ; i++){ def MonthEndDate2 = issues.get(i).getCustomFieldValue(MonthEnd) as Date; int m2 = MonthEndDate2.getMonth() int y2 = MonthEndDate2.getYear() if((y1 == y2) && (m1==m2)){ check =1; } } } check ==0
I am getting errors with the JQL string, saying that it is invalid. I am also trying to compare date values of past tickets with new tickets (don't replicate tickets). Can someone let me know what my error is and if I am doing the right approach
Hey Andrew,
Can you provide the error that you are seeing from the JQL?
I am assuming that you have one issue per month, whence comparing against the month end date. Is this correct?
Thanks for the reply, I fixed the error a couple of moments ago. Here is the new script that works:
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.attachment.Attachment; import com.atlassian.jira.issue.MutableIssue import com.opensymphony.workflow.InvalidInputException; import com.atlassian.crowd.embedded.api.User; import com.atlassian.jira.user.ApplicationUser; import com.atlassian.jira.bc.issue.search.SearchService; import com.atlassian.jira.web.bean.PagerFilter; import org.apache.log4j.Logger; import org.apache.log4j.Category; import java.lang.Object boolean check =true; int m1=0; int y1=0; // Get Information about Month End (Day and Month) def MonthEnd = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Month End Date") def MonthEndDate = (issue.getCustomFieldValue( MonthEnd ) as Date) if(MonthEndDate!=null){ m1 = MonthEndDate.getMonth() y1 = MonthEndDate.getYear() } // Search for issues with same Vendor and Invoice in the same project but not the current Key! String jql = 'project = Finance AND issuetype = "Month end"' // Get a search-Object for JIRA SearchService searchService = ComponentAccessor.getComponent(SearchService.class) // For JIRA 7.X must be ApplicationUser def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def issues; SearchService.ParseResult parseResult = searchService.parseQuery(user, jql) if (parseResult.isValid()) { issues = (searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues()) } // Check for other Issues! if(issues.size()>=0){ for(int i =0; i <= issues.size()-1 ; i++){ def MonthEndDate2 = issues.get(i).getCustomFieldValue(MonthEnd) as Date; if(MonthEndDate2!=null){ int m2 = MonthEndDate2.getMonth() int y2 = MonthEndDate2.getYear() if((y1 == y2) && (m1==m2)){ check =false; } } } } check ==true
And yes, the code checks for one issue per month and this prevents any issues being duplicated in the same month.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.