How to set a custom field date picker based on another date field?

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

6 answers

1 accepted

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

1 vote
Mizan Community Champion Apr 20, 2014

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

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?

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.

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?

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.

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.

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:

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,308 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot