Groovy - Detect custom single select list value of current issue

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

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 vote
Henning Tietgens Community Champion Jan 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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 6 hours ago in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

86 views 4 5
Read article

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