ScriptRunner/Automation for Calculating End Date based on estimates and Updating Epic Estimations
Asked by Maggie Tokarzewska:

I'm encountering issues with implementing automation using ScriptRunner in Jira to **calculate the end date based on custom estimations and to update epic estimations based on child issues.** Below are the details and the scripts I have tried:

### Requirements: {#toc-hId-2145505329}

* **Working Days**: 5 working days a week (Monday-Friday)
* **Working Hours**: Each day has 7 working hours
* **Total Working Days** : Calculated as `(Dev Estimations + QA Estimations + Tech Art Estimations) / 7`, rounded up.
* **End Date Calculation**: Start Date + Total Working Days, excluding weekends.
* **Epic Estimations** : Update if the total of child issues' estimations exceeds the current epic estimations.  

  ![Zrzut ekranu 2024-07-11 o 17.20.16.png](/t5/image/serverpage/image-id/334996i1E75E90060CFBFA1/image-size/large?v=v2&px=999 "Zrzut ekranu 2024-07-11 o 17.20.16.png")![Zrzut ekranu 2024-07-11 o 17.20.23.png](/t5/image/serverpage/image-id/334995i373712E47DB53915/image-size/large?v=v2&px=999 "Zrzut ekranu 2024-07-11 o 17.20.23.png")  

  <br />

**Attempted Approach without ScriptRunner (Using Jira Automation):**

1. **Trigger** : Field value changed
   * Fields: **Start Date** , **Dev Estimations** , **QA Estimations** , **Tech Art Estimations**
2. **Conditions** :
   * **Issue fields condition** :
     * Field: **Start Date**
     * Condition: **is not empty**
3. **Actions** :
   * **Create variable** to calculate totalHours:
     * Variable name: totalHours
     * Value:textCopy code  

       {{#=}}issue.fields\["Dev Estimations"\].toNumber() + issue.fields\["QA Estimations"\].toNumber() + issue.fields\["Tech Art Estimations"\].toNumber(){{/}}  

   * **Create variable** to convert totalHours to totalWorkingDays:
     * Variable name: totalWorkingDays
     * Value:textCopy code  

       {{#=}}ceil(totalHours / 7){{/}}  

   * **Create variable** to calculate interimEndDate:
     * Variable name: interimEndDate
     * Value:textCopy code  

       {{#=}}{{issue.fields\["Start Date"\].plusDays(totalWorkingDays)}}{{/}}  

4. **Conditions and Actions to Adjust for Weekends** :
   * **Condition** : If interimEndDate is Saturday
     * **Advanced compare condition** :
       * First value: {{interimEndDate.toDate("e")}}
       * Condition: **equals**
       * Second value: 6
     * **Action** : Edit issue fields to set **End Date** to {{interimEndDate.plusDays(2)}}
   * **Condition** : If interimEndDate is Sunday
     * **Advanced compare condition** :
       * First value: {{interimEndDate.toDate("e")}}
       * Condition: **equals**
       * Second value: 7
     * **Action** : Edit issue fields to set **End Date** to {{interimEndDate.plusDays(1)}}
   * **Default Action** : If interimEndDate is not a weekend
     * **Edit issue fields** :
       * Field: **End Date**
       * Value: {{interimEndDate}}

Any help to resolve these issues or to identify where the scripts might be failing would be greatly appreciated!

Thank you!


Answers:
author: Bill Sheboy
body: Hi [@Maggie Tokarzewska](/t5/user/viewprofilepage/user-id/5541678) -- Welcome to the Atlassian Community!

*First thing, I am not currently using ScriptRunner and so my suggestions will focus on your Jira Automation rule shown.*

You note there are challenges with your solutions, but you do not describe what they are. **What is not working as you expect with your rule?**

Until we know those challenges...some observations for your rule are:

The {{#=}} ... {{/}} syntax is a math operation, and it is not used for date incrementing. To increment a date value, just use the plus / minus functions, such as:

```
{{issue.myDateField.plusDays(someValue)}}
```

When *someValue* is a variable, that needs to be converted to a number for use in the functions:

```
{{issue.myDateField.plusDays(someValue.asNumber)}}
```

I have observed using *fields* to be problematic in rule smart values. Instead I recommend using either the exact smart value for the field, or its custom field id. To correctly identify those, please use this how-to article: <https://support.atlassian.com/cloud-automation/docs/find-the-smart-value-for-a-field/>

The basic steps are:

* identify an example issue with your fields
* using a browser tab, perform a REST API lookup of the issue

```
yourJiraURL/rest/api/2/issue/yourExampleIssueKey?expand=names
```

* Now search for your field, by name, on the page to find the correct smart value and / or custom field id to use. Note that smart values are name, spacing, and case-sensitive, and so using an incorrect one is substituted with null, often failing to work as expected.

The rule adjustments for weekends may not work as you expect, as they only account for the end point falling on a weekend day and not any *multiple weeks that could be spanned* by the estimates.

An alternative approach is to explicitly [increment by business days](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/#Date-plus-unit---) instead:

```
{{issue.Start date.plusBusinessDays(totalWorkingDays.asNumber)}}
```

Even with this approach, this will not handle any non-working holidays. Those will need either a much more complex rule or another solution approach.

Kind regards,  
Bill


Tags: automation

cloud

enddate

estimations

jira-cloud

scriptrunner