Unable to update a custom field using post-function groovy.

Problem - Post-function Groovy script is not updating the 'Ship Date' custom field value when an issue is created.

Calculation of Ship Date- Ship date (Custom field) should be three business days prior to Expected start date (Custom field).

Question - Could any of you please let me know what is wrong with the script?

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;
import com.atlassian.jira.issue.fields.CustomFieldImpl;

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

ComponentManager componentManager = ComponentManager.getInstance();
IssueManager issueManager = componentManager.getIssueManager();
CustomFieldManager customField = ComponentManager.getInstance().getCustomFieldManager();

def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
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());

CustomField expectedStartDate = customField.getCustomFieldObject(expectedStartDateID);
def StartDateValue = issue.getCustomFieldValue(expectedStartDate);
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);
shipDay = cal1.get(Calendar.DAY_OF_WEEK);

CustomField shipDateField = customField.getCustomFieldObject(shipDateCustomFieldID);
String DATE_FORMAT = "dd/MMM/yy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String formatedDate = sdf.format(cal1.getTime());

issue.setCustomFieldValue(shipDateField,formatedDate);
//shipDateField.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(shipDateField,formatedDate),changeHolder));
issue.store();
}

Thank you in advance.

1 answer

1 accepted

1 vote
Accepted answer

You should use that updateValue line you have commented out. Also would be good to see some log statements so that you know "formatedDate" is what you expect it to be, and not null.

If that doesn't work can you post a simple script that tries to update the custom field, without all the other stuff. That will help people help you.

Jamie,

I see the following result when I change it to:

//issue.setCustomFieldValue(shipDateField,formatedDate);
shipDateField.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(shipDateField,formatedDate),changeHolder));
issue.store();
}

Result:
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.getCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.CustomFieldImpl, java.lang.String) values: [Ship Date, 21/Nov/12] Possible solutions: getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField), setCustomFieldValue(com.atlassian.jira.issue.fields.CustomField, java.lang.Object)

Also your arguments to new ModifiedValue are wrong too, search google for some sample code or check the javadocs.

Jamie,

With just this script:

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 = "dd/MMM/yy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String formatedDate = sdf.format(cal1.getTime());
log.debug "Ship Date Formatted - ${formatedDate}";
}

Logs:

Ship Date Unformatted - Wed Nov 21 00:00:00 PST 2012
Ship Date Formatted - 21/Nov/12
Ship Date Unformatted - Thu Nov 22 00:00:00 PST 2012
Ship Date Formatted - 22/Nov/12
Ship Date Unformatted - Wed Nov 14 00:00:00 PST 2012
Ship Date Formatted - 14/Nov/12

As it says, you have the usage of getCustomFieldValue wrong. You have it correct in another part of your code.

Right. So use a Date.

I have changed it to:

//issue.setCustomFieldValue(shipDateField,formatedDate);
shipDateField.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(shipDateField),formatedDate),changeHolder);
issue.store();
}

Result: javax.script.ScriptException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date

Jamie,

I don't know Groovy. I was taking help from one of my friend who knows Java.

Can we use it this way?

CustomField shipDateField = customField.getCustomFieldObject(shipDateCustomFieldID);
String DATE_FORMAT = "dd/MMM/yy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String formatedDate = sdf.format(cal1.getTime());

DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd/mm/yy");
date = (Date)formatter.parse(formatedDate);

//issue.setCustomFieldValue(shipDateField,formatedDate);
shipDateField.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(shipDateField),date),changeHolder());
issue.store();
}

Result: javax.script.ScriptException: java.text.ParseException: Unparseable date: "21/Nov/12"

My friend gave me this:

CustomField shipDateField = customField.getCustomFieldObject(shipDateCustomFieldID);
//String DATE_FORMAT = "dd/MMM/yy";

//SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MMM/yy");
String formatedDate = sdfSource .format(cal1.getTime());
log.info("Formatted Date - ${formatedDate}");
//parse the string into Date object
Date date = sdfSource.parse(formatedDate);

//create SimpleDateFormat object with desired date format
SimpleDateFormat sdfDestination = new SimpleDateFormat("MM/dd/yy");

//parse the date into another format
String strDate = sdfDestination.format(date);
Date date2 = sdfDestination.parse(strDate);

shipDateField.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(shipDateField),date2),changeHolder);
issue.store();

}

Result:
javax.script.ScriptException: com.atlassian.jira.exception.DataAccessException: org.ofbiz.core.entity.GenericEntityException: while inserting: [GenericEntity:CustomFieldValue][id,24177][datevalue,Wed Nov 28 00:00:00 PST 2012][issue,21698][parentkey,null][customfield,11435] (Java type java.util.Date not currently supported. Sorry.)

Have also tried this:

CustomField shipDateField = customField.getCustomFieldObject(shipDateCustomFieldID);
//String DATE_FORMAT = "dd/MMM/yy";

//SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MMM/yy");
String formatedDate = sdfSource .format(cal1.getTime());
log.info("Formatted Date - ${formatedDate}");
//parse the string into Date object
Date date = sdfSource.parse(formatedDate);

//create SimpleDateFormat object with desired date format
SimpleDateFormat sdfDestination = new SimpleDateFormat("MM/dd/yy");

//parse the date into another format
String strDate = sdfDestination.format(date);
Date date2 = sdfDestination.parse(strDate);
java.sql.Date sqlDate = new java.sql.Date(date2.getTime());

shipDateField.updateValue(null,issue,new ModifiedValue(issue.getCustomFieldValue(shipDateField),sqlDate),changeHolder);
issue.store();

}

Result: No errors were seen. When pasted as a Script post-function in a workflow after 'Create a issue originally' and created an issue, Ship Date is still empty.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

315 views 0 1
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