Very easy!

  1. Create the required TimeStamps custom fields to fill with a Listener or a PostFunction with Groovy Scriptrunner with a CODE like this
  2. Then create a Groovy Scripted Field in JIRA to calculate the SLA (ANS in spanish). You can follow this instructions to create the HTML field
  3. Put a code like THIS:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.customfields.*
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IndexException
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.fugue.Option
import org.apache.log4j.Category
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.plugin.PluginAccessor
import com.atlassian.fugue.Option
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;

def issueMgr = ComponentManager.getInstance().getIssueManager()
ComponentManager componentManager=ComponentManager.getInstance()
def issueFactory = ComponentManager.getInstance().getIssueFactory()
def projectMgr = ComponentManager.getInstance().getProjectManager()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

def cf = customFieldManager.getCustomFieldObjectByName('Date Start to Calculate SLA');

//uncomment to test the options

//def fecha = '2016-06-30';

//Date now = new Date().parse('yyyy-MM-dd', fecha);
Date now = new Date();

long time = 0;
boolean res = false;

//to calculate .... working_days = calculate_working_days ( , );

if (issue.getCustomFieldValue(cf) != null && issue.getCustomFieldValue(cf) != "") {
if ( issue.resolutionDate ) {
time= (((((issue.resolutionDate.getTime() - issue.getCustomFieldValue(cf).getTime())/1000)/60)/60)/24);

if ( time > 5 ) // 5 days of SLA
return "<font color='red'><b>"+time+" days</b></font></a>";
else
return "<font color='green'><b>"+time+" days</b></font></a>";
} else {
time= (((((now.getTime() - issue.getCustomFieldValue(cf).getTime())/1000)/60)/60)/24);
if ( time > 5 )
return "<font color='red'><b>"+time+" days</b></font></a>";
else
return "<font color='green'><b>"+time+" days</b></font></a>";
}
} else {
return "<font color='blue'><b>Time is not counted in this Status</b></font></a>";
}
int calculate_working_days (date_start, date_end ) {
def date_limit = date_end.format('yyyy-MM-dd');
def date_aux = date_start.format('yyyy-MM-dd');
def date = date_start;
def i = 0;
def days = 0;
def weekday = null;
while ( date_aux != date_limit ) {
weekday = Date.parse('yyyy-MM-dd', date_aux)[Calendar.DAY_OF_WEEK];
if (!( weekday == Calendar.SUNDAY || weekday == Calendar.SATURDAY )) {
days++;
}
date = date + 1;
date_aux = date.format('yyyy-MM-dd');
i++;
}
return days;
}

That’s all! very easy!!

Advertisements
Posted by:.

2 replies on “Calculate the SLA with Scripted Field in ScriptRunner for JIRA

  1. Hi, I was trying to use your script but im getting this error:

    “jira unable to resolve class com.atlassian.jira.ManagerFactory”

    any sugestions? Thanks!

    Like

    1. Hi, the code is for Jira v.6 , for Jira 7 you can use this version of the code:

      //JIRA other classes
      import com.atlassian.jira.component.ComponentAccessor
      import com.atlassian.crowd.embedded.api.User
      import com.atlassian.jira.ComponentManager
      import com.atlassian.jira.issue.IssueFactory
      import com.atlassian.jira.project.ProjectManager
      import com.atlassian.jira.project.Project
      import com.atlassian.jira.user.util.UserUtil
      import com.atlassian.jira.issue.customfields.CustomFieldType
      import com.atlassian.jira.issue.customfields.*
      import com.atlassian.jira.issue.CustomFieldManager
      import com.atlassian.jira.issue.IssueManager
      import com.atlassian.jira.issue.index.IndexException
      import com.atlassian.jira.ManagerFactory
      import com.atlassian.jira.user.util.UserUtil
      import com.atlassian.jira.bc.issue.search.SearchService
      import com.atlassian.jira.issue.MutableIssue
      import com.atlassian.jira.issue.Issue;
      import com.atlassian.jira.web.bean.PagerFilter
      import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
      import com.atlassian.jira.issue.CustomFieldManager
      import com.atlassian.jira.issue.ModifiedValue
      import com.atlassian.fugue.Option
      import org.apache.log4j.Category
      import com.atlassian.jira.issue.issuetype.IssueType
      import com.atlassian.jira.issue.link.IssueLinkType
      import com.atlassian.jira.issue.IssueManager
      import com.atlassian.jira.issue.link.IssueLinkTypeManager
      import com.atlassian.crowd.embedded.api.User
      import com.atlassian.jira.component.ComponentAccessor
      import com.atlassian.jira.ComponentManager
      import com.atlassian.jira.issue.Issue
      import com.atlassian.jira.issue.MutableIssue
      import com.atlassian.plugin.PluginAccessor
      import com.atlassian.fugue.Option
      import com.atlassian.jira.issue.ModifiedValue
      import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
      import com.atlassian.jira.issue.util.IssueChangeHolder
      import com.opensymphony.workflow.WorkflowContext
      import com.atlassian.jira.workflow.WorkflowTransitionUtil;
      import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
      import com.atlassian.jira.util.JiraUtils;
      
      
      def issueMgr = ComponentAccessor.getIssueManager()
      def issueFactory = ComponentAccessor.getIssueFactory()
      def projectMgr = ComponentAccessor.getProjectManager()
      CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
          
      def cf = customFieldManager.getCustomFieldObjectByName('XXX Credentials Date');
      def cf2 = customFieldManager.getCustomFieldObjectByName('XXX Creation Date');
      
      //def fecha = '2016-06-30';
      //Date now = new Date().parse('yyyy-MM-dd', fecha);
      Date now = new Date();
      
      long time = 0;
      boolean res = false;
      def Not_working_days = 0;
      
      
      if (issue.getCustomFieldValue(cf) != null && issue.getCustomFieldValue(cf) != "") {
      
       Not_working_days = calculate_not_working_days( (Date) issue.getCustomFieldValue(cf2),(Date) issue.getCustomFieldValue(cf));    
       time= (long) (((((((Date) issue.getCustomFieldValue(cf2)).getTime() - ((Date) issue.getCustomFieldValue(cf)).getTime())/1000)/60)/60)/24) - Not_working_days;
       
       if ( time >= 1 )
       	return "<font color='red'><b>"+time+" days</b></font></a>";
       else
       	return "<font color='green'><b>"+time+" days</b></font></a>";
      } else {
       Not_working_days = calculate_not_working_days((Date) issue.getCustomFieldValue(cf2), (Date) now);    
       time=(long) (((((now.getTime() - ((Date) issue.getCustomFieldValue(cf2)).getTime())/1000)/60)/60)/24) - Not_working_days;
       return time
       if ( time >= 1 )
       	return "<font color='red'><b>"+time+" days</b></font></a>";
       else
       	return "<font color='green'><b>"+time+" days</b></font></a>";
      }
      
      
      
      int calculate_not_working_days (Date date_start, Date date_end ) {
      	def date_limit = date_end.format('yyyy-MM-dd');
      	def date_aux = date_start.format('yyyy-MM-dd');
          def date = date_start;
      	def i = 0;
          def days = 0;
          def weekday = null;
      	while ( date_aux != date_limit ) {
              weekday = Date.parse('yyyy-MM-dd', date_aux)[Calendar.DAY_OF_WEEK];
              if (( weekday == Calendar.SUNDAY || weekday == Calendar.SATURDAY )) {
              	days++;
              }
          	date = date + 1;
              date_aux = date.format('yyyy-MM-dd');
              i++;
          }
          return days;
      }
      
      
      

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s