Hi I have a problem setting new value to a custom field date time picker. The requiremement is to add X working days to a specific custom date field. Can anyone help me on this? Any sugestion would be of great help.
Below is my post function code that supposed to work but it does not work as I expected:
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import java.sql.Timestamp;
import com.atlassian.jira.issue.util.IssueChangeHolder
// initializing the priority
def Urgent = 5;
def High = 20;
def Medium = 30;
def None = 0;
private GregorianCalendar getDate(double roll)
{
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
int numNonBusinessDays = 0;
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
numNonBusinessDays++;
}
cal.add(Calendar.DATE, 1);
}
if(numNonBusinessDays > 0) {
cal.add(Calendar.DATE, numNonBusinessDays);
}
return cal;
}
key = issue.getKey();
def priority = mutableIssue.getPriority().getString("name");
IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
MutableIssue issue = issueManager.getIssueObject(key);
customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
GregorianCalendar cal;
if(priority.equals("1-Urgent")) {
cal = getDate(Urgent);
}
else if(priority.equals("2-High")) {
cal = getDate(High);
}
else if(priority.equals("3-Medium")) {
cal = getDate(Medium);
}
// get the Date value from Date Full Packed Receive custom field
dateFPR = customFieldManager.getCustomFieldObjectByName("Date Full Pack Received");
dateFPRValue = issue.getCustomFieldValue(dateFPR);
// get the Date value from SLA Date custom field
dateSLA = customFieldManager.getCustomFieldObjectByName("SLA Date");
dateSLAValue = issue.getCustomFieldValue(dateSLA);
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
Timestamp dueDate = new Timestamp(cal.getTimeInMillis());
dateSLA.updateValue(null, issue, new ModifiedValue(dateSLAValue, dueDate), changeHolder);
import java.util.Date;
import java.sql.Timestamp; import java.util.Calendar; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import com.atlassian.jira.ComponentManager; import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.issue.IssueManager; import org.apache.log4j.Category; import org.apache.log4j.Logger; import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.util.IssueChangeHolder; import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem; import com.atlassian.jira.issue.ModifiedValue; import com.atlassian.jira.crowd.embedded.ofbiz.db.OfBizHelper; myissue = "CMF-280"; IssueManager issueManager = ComponentManager.getInstance().getIssueManager(); MutableIssue mutableIssue = issueManager.getIssueObject(myissue); customFieldManager = ComponentManager.getInstance().getCustomFieldManager(); // initializing the priority def Urgent = 5; def High = 20; def Medium = 30; def None = 0; def priority = mutableIssue.getPriority().getString("name"); def dateFPR = ""; def dateFPRValue = ""; private GregorianCalendar getDate(double roll, String date_fpr) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S"); Date origdate = df.parse(date_fpr); Calendar cal = Calendar.getInstance(); cal.setTime(origdate); //cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.HOUR_OF_DAY,0); cal.set(Calendar.MINUTE,0); cal.set(Calendar.SECOND,0); cal.set(Calendar.MILLISECOND,0); for (int x=0;x<roll;x++) { cal.add(Calendar.DATE, 1); while(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { cal.add(Calendar.DATE, 1); } } return cal; } GregorianCalendar cal; // get the Date value from Date Full Packed Receive custom field dateFPR = customFieldManager.getCustomFieldObjectByName("Date Full Pack Received"); dateFPRValue = mutableIssue.getCustomFieldValue(dateFPR); //return dateFPRValue; if(priority.equals("1-Urgent")) { cal = getDate(Urgent, dateFPRValue.toString()); } else if(priority.equals("2-High")) { cal = getDate(High, dateFPRValue.toString()); } else if(priority.equals("3-Medium")) { cal = getDate(Medium, dateFPRValue.toString()); } else if(priority.equals("None")) { cal = getDate(None, dateFPRValue.toString()); } // get the Date value from SLA Date custom field dateSLA = customFieldManager.getCustomFieldObjectByName("SLA Date"); FieldLayoutItem fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(mutableIssue).getFieldLayoutItem(dateFPR); IssueChangeHolder changeHolder = new DefaultIssueChangeHolder(); Timestamp dueDate = new Timestamp(cal.getTimeInMillis()); dateSLA.updateValue(fieldLayoutItem, mutableIssue, new ModifiedValue(dateSLA, dueDate), changeHolder); //mutableIssue.setCustomFieldValue(dateSLA , dueDate); dateSLAValue = mutableIssue.getCustomFieldValue(dateSLA); return dateSLAValue; log.info("value from field SLA:"+dateSLAValue.getValueFromIssue(mutableIssue));
I have found the answer on my problem. The suitable code which works for me is posted above. Hope this will help others with similar problems in custom fields.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The script might not work for Jira 6.2 as Component manager is deprecated . Use
import com.atlassian.jira.component.ComponentAccessor
Try to add logging to your script , use below statement
log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Mizan, I will try your suggestion. Do you have idea where I can locate the logfile so that I can check waht the log contains?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried using the ComponentAccessor and import com.atlassian.jira.component.ComponentAccessor but still cannot acheive what I wanted. I just want to update the dateSLA custom field but don't know where I am having trouble. I used the updateValue based from the forums I have read.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Finally, I have made a way to solve my own problem. I was now able to update a date picker custom field base on another date picker custom field.
Below is my complete updated working code:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You refer to a finished working code but it is not posted could you post, i also have use for such a requirement, thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am not sure if it is throwing errors. It just not update the expected custom field which is supposed to have value. When I crate an issue this post function does not work as I expect.
I am using Jira version 6.2.
Do you have an idea on this matter? Any help would be greatly appreciated.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How is your code not working as expected? Is it throwing errors, or just not updating the field to the correct value?
Also, which version of Jira are you using?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online 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.