Calculate date field based on due date in a linked issue

Dieter Schade December 11, 2016

Hi all,

I would like to create a calculated date field with the misc custom field addon which does the following:

I have 1 Main Issue with a couple of subtasks. The calculated date fields are only relevant in the subtasks.

In the subtasks it is possible to set a dependency to another subtask, this is done via an issue link, link type: 'depends on' (e.g. subtask 1 can only be started after subtask 2 is finished). The calculated date should now calculate the date based on the due date of the linked subtasks + x days (the x days is the value which I have in another custom field).

Is that possible?

2 answers

0 votes
David _old account_
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.
December 12, 2016

You can try with a formula derived from the following:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.user.ApplicationUser;

IssueLinkManager ilm = ComponentAccessor.getIssueLinkManager();
ApplicationUser curUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
linkedIssues = ilm.getLinkCollection(issue, user, true).getOutwardIssues("Dependency");
if (linkedIssues.size()>0) {
    Issue dependsOn = linkedIssues.iterator().next();
    if (dependsOn.getDueDate()==null)
        return null;
    Long delta = issue.get("my field"); //assuming this is a Number field. Otherwise, needs to be converted to a Long
    Calendar dueDate = Calendar.getInstance();
    dueDate.setTime(dependsOn.getDueDate());
    if (delta)
        dueDate.add(Calendar.DAY_OF_YEAR, delta);
    return dueDate.getTime();
} else
    return null;

You'll need to adapt it to your needs:

  • replace the link type name
  • possibly replace "getOutwardIssues" with "getInwardIssues" depending on the link direction you're interested in (just experiment until it works)
  • replace the field name that contains the number of days to add

For debugging, don't forget to watch atlassian-jira.log for errors. Also, you can use log.error("some text"); to write data to the logs for debugging purposes.

 

Dieter Schade December 12, 2016

Thank you for the detailed answer! I will try that asap and will update you here if that worked for me.

Best regards

Dieter

Dieter Schade December 29, 2016

I found the time to test the script but even after some adjustments I could not get it to work. Im trying a different script now in a scripted field from the scriptrunner plugin. Nevertheless thank you for your help! 

0 votes
David _old account_
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.
December 11, 2016

Hi Dieter,

this is technically possible but the required script is a little complex. However, you should know something: the value of a calculated field is not stored in the database, but it is still stored in the JIRA index, which is used for searching as well as for the "List view". The index for an issue is updated only when that issue is transitioned or edited.  As a consequence, if you modify a linked subtask's due date, this will not update the primary subtask, so search won't work as expected.

However, when you display the main subtask, its calculated fields will be re-computed (although the results won't be stored in the index) so the calculated field will be accurate then.

So, before anyone tries to help you write the formula, you need to decide whether this limitation is acceptable for your use case.

David

Dieter Schade December 11, 2016

Hi David,

thanks for the quick Response and the explanation. This limitation is acceptable for my use case.

Best regards

Dieter

Suggest an answer

Log in or Sign up to answer