Calculate date field based on due date in a linked issue

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

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

Hi David,

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

Best regards

Dieter

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.

 

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

Best regards

Dieter

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! 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Dec 10, 2018 in Confluence

Organizing your space just got easier - Page Tree Drag & Drop is here

Hi Community! I’m Elaine, Confluence Product Manager. You may have read my earlier post about page tree in space navigation sidebar. I'm excited to share another improvement that helps you organize ...

188 views 4 6
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