Groovy - Detect custom single select list value of current issue

Rob van Bommel January 13, 2014

Hi, due to my very limited groovy / java(?) / scripting knowledge I'm unable to figure out how to do this. I have a (POST) script that sets the due date of an issue based on the detected status, which works fine so far. Now I would like to make the due date depend on the combination of the status and the selected option of a custom single select field called "Classification". Is this possible, and if so, could someone point me in the right direction? This is what I have so far (based on the status only):

import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.config.ConstantsManager
import org.apache.log4j.Category
import java.sql.Timestamp;

// Init log
log = Category.getInstance("com.onresolve.jira.groovy.Belden-Set-DueDate")

// Init issue
MutableIssue myIssue = issue

// Define processing time based on status value
def statusVar = myIssue.getStatusObject();
def days = 0;
switch ( statusVar.name ) {
	case 'Incomplete':
		days = 1
		break
	case "New request":
		days = 2
		break
	case "Request in progress":
	// From now the days should be set depending on classification (A/B/C/E)
		days = 2
		break
}

// Turn amount of days into working days and set due date accordingly
if (days > 0) {
	Calendar wdcal_duedate = Calendar.getInstance();
	wdcal_duedate.setTime(wdcal_duedate.getTime());
	def workingdays = 0;
	while (days > 0) {
		wdcal_duedate.add(Calendar.DATE, 1);
		workingdays++;
		if (wdcal_duedate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && wdcal_duedate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
			days--;
		}
	}
	
	Calendar cal_duedate = Calendar.getInstance();
	Timestamp date_duedate = new Timestamp(cal_duedate.getTimeInMillis()+ workingdays*1000*24*60*60);
	myIssue.setDueDate(date_duedate);
}

I hope someone can help me out. Thanks in advance!

2 answers

1 accepted

0 votes
Answer accepted
Rob van Bommel January 14, 2014

Thanks for the quick reply, but somehow that didn't work for me. Now I've got it to work in a slightly different manner, see script below (for now the script updates the summary field with the detected value of the Classification field, setting the due date depending on the Classification will be simple now).

import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.config.ConstantsManager
import org.apache.log4j.Category
import java.sql.Timestamp;

// Init log
log = Category.getInstance("com.onresolve.jira.groovy.Belden-Set-DueDate")

// Init issue
MutableIssue myIssue = issue

// Define processing time based on status value
def statusVar = myIssue.getStatusObject();
def days = 0;
switch ( statusVar.name ) {
	case 'Incomplete':
		days = 1
		break
	case "New request":
		days = 2
		break
	case "Request in progress":
		// From now the days should be set depending on classification (A/B/C/E)
		ComponentManager componentManager = ComponentManager.getInstance()
		CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
		CustomField cf = customFieldManager.getCustomFieldObject("customfield_10300") 

		def classification = myIssue.getCustomFieldValue(cf)
		myIssue.summary = classification
		
		days = 2
		break
}

// Turn amount of days into working days and set due date accordingly
if (days > 0) {
	Calendar wdcal_duedate = Calendar.getInstance();
	wdcal_duedate.setTime(wdcal_duedate.getTime());
	def workingdays = 0;
	while (days > 0) {
		wdcal_duedate.add(Calendar.DATE, 1);
		workingdays++;
		if (wdcal_duedate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && wdcal_duedate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
			days--;
		}
	}
	
	Calendar cal_duedate = Calendar.getInstance();
	Timestamp date_duedate = new Timestamp(cal_duedate.getTimeInMillis()+ workingdays*1000*24*60*60);
	myIssue.setDueDate(date_duedate);
}

0 votes
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 13, 2014

You can get the value of the select field like this

cfValues['Classification']?.getValue()

Suggest an answer

Log in or Sign up to answer