How to select the least date amongst the list of dates selected in a multi-select field?
Asked by Aman Jain:

I have a multi-select custom field with several dates as option. I want to select the least date amongst the selected options and put it in a different custom date field.


Answers:
author: Jovin
body: Hey [@Aman Jain](/t5/user/viewprofilepage/user-id/5532902)

This is a good question! One that's quite challenging to work through, but I believe you could achieve this with automation. I've not tested this specific solution, but let me know how you go.

Right - let's jump in!

1. **TRIGGER: Field value changed**
   1. Field to monitor: Your custom field
   2. Change type: All changes
   3. For: all issue operations
2. **Add component: THEN: Add an action: Create variable**
   1. Variable name: smallestDate
   2. Smart value: 2099-01-01
      1. By setting it so far in the future all dates you offer will be less than this
3. **FOR EACH: Add a branch: Advanced branching**
   1. Smart value: {{issue.custom field name}}
   2. Variable name: {{currentDate}}
   3. **Add to branch: IF: Add a condition: {{smart values}} condition**
      1. First value: {{currentDate.jqlDate}}
      2. Condition: less than
      3. Second value: {{smallestDate.jqlDate}}
   4. **Add to branch: THEN: Add an action: Create variable**
      1. Variable name: smallestDate
      2. Smart value: {{currentDate.jqlDate}}
4. (Not in the branch now) **Add component: IF: Add a condition: {{smart values condition}}**
   1. First value: {{smallestDate}}
   2. Condition: does not equal
   3. Second value: 2099-01-01
5. **Add component: THEN: Add an action: Edit issue**   
   1. Fields: Due date(or whatever date field you are using)
   2. Value: {{smallestDate.jqlDate}}

Let's go over what this does in plain english!

1. When the customField value changes (at all) we want to check we still have the smallest date
   1. You could change this to be only on creation or on transition etc.
2. We'll create a dummy variable to be able to compare dates again, by making this be January 1st 2099 all dates will be less than this.
3. Now, let's compare every date the person has selected and make sure it's the smallest one by comparing it to our current smallest one. If it's smaller, let's update our current smallest one.
4. Lastly, let's check that the value isn't the default value (e.g. they removed all values), and if we have a valid one, update our issue's Due date (or whatever date field you're using).

Lastly - a screenshot to help!

![smallest date.png](https://community.atlassian.com/t5/image/serverpage/image-id/332727iEE34778E314E8848/image-size/large?v=v2&px=999 "smallest date.png")

comments: author: Aman Jain
body: Thanks Jovin for the answer. However, when I am running this automation, log shows that the issue did not match the condition.

Please note that the values in the multi-select field are not dates exactly. I am extracting out the dates from the names of the field values.

For an instance, "ABC - 2024-01-12" is the exact value in the multi-select field and I am extracting it using "{{issue.Test Field.value.right(10)}}" where "Test Field" is the custom field name.

Let me know if I need to do something else.

Here is the screenshot:

![Screenshot 2024-07-01 at 6.24.52?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/332755iDDB4BFB2FDB51C23/image-size/large?v=v2&px=999 "Screenshot 2024-07-01 at 6.24.52?PM.png")

![Screenshot 2024-07-01 at 6.39.11?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/332757iFEB277DA4EEC55E4/image-size/large?v=v2&px=999 "Screenshot 2024-07-01 at 6.39.11?PM.png")![Screenshot 2024-07-01 at 6.39.51?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/332758i0BDE4EC8B62E29FE/image-size/large?v=v2&px=999 "Screenshot 2024-07-01 at 6.39.51?PM.png")


Tags: automation

cloud

jira-service-management

jira-service-management-cloud