Condition - 🔀 Calculate project planned duration based on two dates

Condition - 🔀 Calculate project planned duration based on two dates

This rule calculates the planned duration of a project in calendar days: every time the Start date or the End date of a project changes, the rule counts the days between both dates and writes the result in Planned duration, a Number field.

Start date and End date are Projectrak predefined fields. We suggest using them, but the rule works with any two date fields.

A Duration field looks like the natural choice, but it measures effort, not calendar time. It stores an amount of time and shows it according to the time tracking settings of your Jira site: Working hours per day and working days per week, for example 8 hours and 5 days. The time format: Hours, Days or Pretty. With these settings, a project of 31 calendar days can be shown in very different ways: Stored as 31 days of 24 hours (744 hours), it shows as 744h, 93d or 18 weeks 3 days, because Jira counts 8 hours per day. Stored as 31 days of 8 hours (248 hours), it shows as 248h, 31d or 6 weeks 1 day, because Jira counts 5 days per week. The stored value doesn't change, only the way it is shown. If your Jira administrator changes the time tracking settings, every planned duration changes its display too. A Number field with the suffix days always shows the calendar days as they are: 31 days.

How this rule works

  • The rule starts from a Projectrak outgoing webhook, which tells Automation every time a project field changes. You set it up once in step 2.

  • It only runs when the Start date or the End date changes. The rule detects them by their field ID, so renaming the fields doesn't break it. Changes in other fields are ignored.

  • It counts calendar days, including both the start day and the end day. For example, from 1 March to 31 March the planned duration is 31.

  • If one of the two dates is empty, the rule does nothing and the planned duration keeps its current value.

  • The result is written in a Number field, for example Planned duration.

Before you start

  • You need to be a Jira administrator to import rules and set up Projectrak outgoing webhooks. If you aren't, see the options below.

  • You'll need a Projectrak API Key ID and API Key: How to create a Projectrak API key.

  • Create a Projectrak Number field to store the result, for example Planned duration, with the suffix days.

  • Write down the IDs of your Start date, End date and Planned duration fields. You can find them in the Field manager.

Ask your Jira admin. Copy this message, fill in the parts in [brackets] and send it to your Jira admin: Hi! I use Projectrak and I'd like the planned duration of our projects to be calculated automatically from their start and end dates. Jira spaces that need the rule: [Space name 1] [Space name 2] Projectrak provides a ready-made Automation rule that a Jira administrator can import in a few minutes. Here is the rule and how to set it up. The rule is imported disabled. To set it up you need: A Projectrak API key. This is how to create one. The Start date and End date fields: [e.g. Start date, End date] The Number field to store the result: [e.g. Planned duration] Could you take a look? Thanks! Are you a project administrator? You can't import rules, but you can create this rule in your own project: go to Project settings › Automation, create a new rule and follow the steps in Build the rule manually below. You'll still need a Jira administrator to set up the Projectrak outgoing webhook.

Import the ready-made rule

Download the rule template:

This rule is a template. It is imported disabled and won't work until you enter your own data in the steps below.

There are 5 steps to get the rule working: import it, connect it to Projectrak, then enter your own data in 3 places.

Step 1. Import the rule

  1. Go to Jira settings › System › Global automation.

  2. Click the button with the ••• icon and select Import flows.

  3. Upload the JSON file.

  4. Leave Import flow owners unchecked, so you become the owner of the rule.

  5. Optional: use Select spaces to limit the rule to the projects that need it.

step1-import-flows.png

Step 2. Connect the rule to Projectrak

  1. Open the Incoming webhook trigger and copy the Webhook URL.

  2. Click the button next to Secret to generate one and copy it. Then click Save: the secret is hidden once the rule is saved.

step2-webhook-url-secret.png
  1. Go to Projectrak Administration › Outgoing webhooks and click Create webhook.

  2. Enter a name, paste the Webhook URL in Destination URL and add a header with the key X-Automation-Webhook-Token and the secret as value. Check Secured, click Add and save the webhook.

step2-projectrak-webhook.png

Learn more about Projectrak outgoing webhooks.

Step 3. Enter your Projectrak API key

  1. Open the first Send web request step (POST …/api-keys/jwt).

  2. In Custom data, replace YOUR_API_KEY_ID and YOUR_API_KEY with your own values. Keep the quotes.

Don't have them yet? How to create a Projectrak API key

step3-api-key.png

Step 4. Enter the IDs of your Start date and End date fields

  1. Open the first Compare two values condition.

  2. In Regular expression, replace YOUR_START_DATE_FIELD_ID and YOUR_END_DATE_FIELD_ID with the IDs of your fields. Keep the rest of the expression as it is.

step4-condition.png
  1. Open the Send web request step GET …/fields/YOUR_START_DATE_FIELD_ID and, in Web request URL, replace YOUR_START_DATE_FIELD_ID with the ID of your Start date field.

step4-start-date.png
  1. Open the Send web request step GET …/fields/YOUR_END_DATE_FIELD_ID and, in Web request URL, replace YOUR_END_DATE_FIELD_ID with the ID of your End date field.

step4-end-date.png

Step 5. Enter the ID of your Planned duration field

  1. Open the last Send web request step (POST …/values/projects/{{projectId}}).

  2. In Custom data, replace YOUR_PLANNED_DURATION_FIELD_ID with the ID of your Number field. It's a number, so don't add quotes.

step5-planned-duration.png

Save and enable the rule

Click Save after your changes, then turn the rule on with the Enabled toggle.

Rules that apply to all projects use more of your monthly Automation executions. Limit the rule's scope to the projects that need it.

Build the rule manually

The data of the Projectrak outgoing webhook is available in the {{webhookData}} smart value. Step 1. Configure trigger Create a new trigger → Incoming webhook Work item criteria: No work items from the webhook Save the rule, then copy the Webhook URL and the Secret into a Projectrak outgoing webhook as described in Step 2. Connect the rule to Projectrak above. Step 2. Store the webhook data in variables Add component → New action → Create variable, once for each variable: Variable name Smart value projectId {{webhookData.project.id}} changedFieldIds |{{#webhookData.fields}}{{id}}|{{/}} Step 3. Check that a date has changed Add component → New condition → {{smart values}} condition First value: {{changedFieldIds}} Condition: exactly matches regular expression Regular expression: .*\|(YOUR_START_DATE_FIELD_ID|YOUR_END_DATE_FIELD_ID)\|.* Step 4. Get the authorization token Add the steps "Get the Projectrak JWT" and then "Store the JWT in a variable", described at this link: Important: If you don't have already created a Projectrak API token, you have to do so as described in the first steps of the above link. If you have already got it, you can reuse it. Step 5. Get the Start date of the project Add component → New action → Send web request Web request URL: https://profields-cloud.deiser.com/values/projects/{{projectId}}/fields/YOUR_START_DATE_FIELD_ID Headers: Content-Type: application/json Authorization: Bearer {{projectrakJwt}} → Defined in step 4 HTTP method: GET Web request body: Empty Check: Delay execution of subsequent flow actions until we've received a response for this web request Then add component → New action → Create variable: Variable name: startDate Smart value: {{webResponse.body.value.value}} Step 6. Get the End date of the project Add component → New action → Send web request Web request URL: https://profields-cloud.deiser.com/values/projects/{{projectId}}/fields/YOUR_END_DATE_FIELD_ID Headers: Content-Type: application/json Authorization: Bearer {{projectrakJwt}} → Defined in step 4 HTTP method: GET Web request body: Empty Check: Delay execution of subsequent flow actions until we've received a response for this web request Then add component → New action → Create variable: Variable name: endDate Smart value: {{webResponse.body.value.value}} Step 7. Check that both dates have a value Add component → New condition → {{smart values}} condition First value: {{startDate}}|{{endDate}} Condition: exactly matches regular expression Regular expression: \d{4}-\d{2}-\d{2}\|\d{4}-\d{2}-\d{2} If one of the dates is empty, the rule stops here. Step 8. Calculate the planned duration Add component → New action → Create variable Variable name: plannedDays Smart value: {{#=}}{{startDate.toDate("yyyy-MM-dd").diff(endDate.toDate("yyyy-MM-dd")).days}} + 1{{/}} The + 1 includes the end day in the count. Step 9. Write the result in the Planned duration field Add component → New action → Send web request Web request URL: https://profields-cloud.deiser.com/values/projects/{{projectId}} Headers: Content-Type: application/json Authorization: Bearer {{projectrakJwt}} → Defined in step 4 HTTP method: POST Web request body: Custom data Custom data: [ { "fieldId": YOUR_PLANNED_DURATION_FIELD_ID, "value": {{plannedDays}} } ] This example uses an incoming webhook trigger and two date fields to calculate the period but more options can be used. I.e. You can calculate the remaining time of a project with a scheduled trigger that executes every day and the end date of the project and the current date (see Jira smart values - date and time )