Groovy Post function on Create transition can't set Date custom field value

Based on the references online, I have come up with this Groovy Script. I have a custom field called 'Expected Start Date' which is a required field filled in by the Jira user. Based on this Start Date, I have written a post function Groovy script that is used to update another custom field in Jira 'Ship Date' by calculating 3 business days prior to Expected Start Date. When I run the Groovy script from Script Runner, I see the following error. Appreciate your help.

ERROR: javax.script.ScriptException: java.lang.ClassCastException: com.atlassian.jira.issue.ModifiedValue cannot be cast to java.util.Date

SCRIPT:

import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.query.Query;
import com.atlassian.jira.bc.JiraServiceContext;
import com.atlassian.jira.bc.JiraServiceContextImpl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.bc.filter.SearchRequestService;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.issue.search.SearchRequest;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.web.bean.PagerFilter;

String currentUser = "rballa";
String projectId = 10001;
Integer filterId = 11113;
Integer expectedStartDateID = 10049;
Integer shipDateCustomFieldID = 11435;

// Get all of our different managers
ComponentManager componentManager = ComponentManager.getInstance();
IssueManager issueManager = componentManager.getIssueManager();
CustomFieldManager customField = ComponentManager.getInstance().getCustomFieldManager();

// Logger
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");

// Set the logging level to DEBUG
log.setLevel(org.apache.log4j.Level.DEBUG);

JiraAuthenticationContext authenticationContext = componentManager.getJiraAuthenticationContext();
SearchRequestService searchRequestService = componentManager.getSearchRequestService();
JiraServiceContext ctx = new JiraServiceContextImpl(authenticationContext.getUser());
SearchProvider searchProvider = componentManager.getSearchProvider();
SearchRequest sr = searchRequestService.getFilter(ctx, filterId);
results = searchProvider.search(sr.getQuery(),authenticationContext.getUser() , PagerFilter.getUnlimitedFilter());

results.getIssues().each {
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
Issue issue = componentManager.getIssueManager().getIssueObject(it.getKey());

// Get the custom field value
CustomField expectedStartDate = customField.getCustomFieldObject(expectedStartDateID);
def StartDateValue = issue.getCustomFieldValue(expectedStartDate);
log.debug "Start Date is - ${StartDateValue}";

//get Day_of_Week
//cal1 = Calendar.getInstance();
def cal1 = Calendar.getInstance();
cal1.setTime(StartDateValue);
def dayOfweek = cal1.get(Calendar.DAY_OF_WEEK);
def shipDay;
if((dayOfweek == 2) || (dayOfweek == 3) || (dayOfweek == 4))
cal1.add(Calendar.DATE,-5);
else
cal1.add(Calendar.DATE,-3);

log.debug "Ship Date Unformatted - ${cal1.getTime()}";
shipDay = cal1.get(Calendar.DAY_OF_WEEK);

CustomField shipDateField = customField.getCustomFieldObject(shipDateCustomFieldID);
String DATE_FORMAT = "MM/dd/yy";
SimpleDateFormat sdf =
new SimpleDateFormat(DATE_FORMAT);

String formatedDate = sdf.format(cal1.getTime());
// Date shipDate = sdf.parse(formatedDate);
log.debug "Ship Date Formatted - ${formatedDate}";
issue.setCustomFieldValue(shipDateField,formatedDate);
shipDateField.updateValue(null,issue,new ModifiedValue(issue.setCustomFieldValue(shipDateField,formatedDate),changeHolder)
issue.store();
log.debug "New Custom Field Value -- ${shipDateField.getValue(issue)}"
}

3 answers

1 accepted

2 votes
Accepted answer

Jira date fields typically accept java.util.date or java.sql.timestamp objects or calendar.

The format of date being displayed on UI is configured in administaration settings.

I suggest to try throw either of the three to the field and see what sticks :)

Edit:

I actually tried myself as I encountered same question in my development

Custom fields of types Date and Datetime accept java.util.date objects/

Here is the code that I tested in Scripted field that works :

import java.util.*
import java.sql.Timestamp
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager

def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def dt = new Date() //now
def date = new Date(0) //1970
def ts = new Timestamp(dt.getTime()) //this will work for built-in field DueDate!
def cf_date = customFieldManager.getCustomFieldObjectByName("cf_date") //alright! I know it's a stupid name for CF :)
def cf_datetime = customFieldManager.getCustomFieldObjectByName("cf_datetime")
issue.setCustomFieldValue(cf_date, dt)
issue.setCustomFieldValue(cf_datetime, date)
issue.setDueDate(ts)
return issue.getCustomFieldValue(cf_datetime)

Thank you, Alexey for offering help. Thanks to Jamie, who helped me in fixing my script. My script works fine when I manually run it from scriptrunner but not when I insert it into the post-function at 'Create' in the workflow.

Moving the post-function further down below all the post-functions worked.

Please refer this post , there is a simliar script available

Moving the post-function further down below all the post-functions worked.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,408 views 0 8
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