Scriptrunner: Check through other tickets and comparing a custom field. Edited

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

1 answer

1 accepted

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.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,111 views 13 19
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot