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)}"
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Moving the post-function further down below all the post-functions worked.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Moving the post-function further down below all the post-functions worked.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.