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

Vishali
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.
November 7, 2012

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
Answer accepted
JamieA
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.
November 8, 2012

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.

Vishali
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.
November 8, 2012

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)
JamieA
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.
November 8, 2012

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

Vishali
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.
November 8, 2012

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

JamieA
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.
November 8, 2012

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

JamieA
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.
November 8, 2012

Right. So use a Date.

Vishali
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.
November 8, 2012

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

Vishali
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.
November 8, 2012

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"

Vishali
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.
November 8, 2012

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.)

Vishali
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.
November 8, 2012

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
TAGS
AUG Leaders

Atlassian Community Events