Set the custom field value based on custom field date Range
Asked by Venkatesh Pettem:

Hi Team,

There are two custom fields Start date and Star quarter.

Start Quarter values are Q1 24, Q2 24, Q3 24, Q4 24.

whenever user select a date in Start date and if it falls under first quarter of the year (Consider January to March) then Start quarter field value to be set to Q1 24.

if it falls under second quarter of the year (Consider April to June) then Start quarter field value to be set to Q2 24.

Similarly for July to Sep then Q3 24, Oct to Dec then Q4 24

Please help with Behavior script

Thanks,

Venkatesh


Answers:
author: Venkatesh Pettem
body: document.addEventListener("DOMContentLoaded", function() {  
const dateInput = document.getElementById('date');  
const selectList = document.getElementById('selectList');

dateInput.addEventListener('change', function() {  
const selectedDate = new Date(dateInput.value);

// Define your date range conditions  
const startDate1 = new Date('2024-07-01');  
const endDate1 = new Date('2024-07-10');

const startDate2 = new Date('2024-07-11');  
const endDate2 = new Date('2024-07-20');

const startDate3 = new Date('2024-07-21');  
const endDate3 = new Date('2024-07-31');

// Example condition: if selected date is within the range  
if (selectedDate \>= startDate1 \&\& selectedDate \<= endDate1) {  
selectList.value = "option1"; // Set your desired option value for this range  
} else if (selectedDate \>= startDate2 \&\& selectedDate \<= endDate2) {  
selectList.value = "option2"; // Set your desired option value for this range  
} else if (selectedDate \>= startDate3 \&\& selectedDate \<= endDate3) {  
selectList.value = "option3"; // Set your desired option value for this range  
} else {  
selectList.value = "default"; // Default option value if date is outside all ranges  
}  
});  
});


author: Venkatesh Pettem
body: document.addEventListener("DOMContentLoaded", function() { const dateInput = document.getElementById('date'); const selectList = document.getElementById('selectList'); dateInput.addEventListener('change', function() { const selectedDate = new Date(dateInput.value); // Define your date range conditions const startDate1 = new Date('2024-07-01'); const endDate1 = new Date('2024-07-10'); const startDate2 = new Date('2024-07-11'); const endDate2 = new Date('2024-07-20'); const startDate3 = new Date('2024-07-21'); const endDate3 = new Date('2024-07-31'); // Example condition: if selected date is within the range if (selectedDate \>= startDate1 \&\& selectedDate \<= endDate1) { selectList.value = "option1"; // Set your desired option value for this range } else if (selectedDate \>= startDate2 \&\& selectedDate \<= endDate2) { selectList.value = "option2"; // Set your desired option value for this range } else if (selectedDate \>= startDate3 \&\& selectedDate \<= endDate3) { selectList.value = "option3"; // Set your desired option value for this range } else { selectList.value = "default"; // Default option value if date is outside all ranges } }); });


author: Jeroen Poismans
body: Hi and welcome to the Community!

If you want to make this flexible, try using a lookup table in automation. What the rule will do:

* Extract the month form a date field
* Lookup the month's corresponding Quarter
* Combine the quarter with the date

The rule ( i have used manual trigger and due date field, but you can change to fit your needs):

![Screenshot 2024-07-15 at 10.22.44.png](https://community.atlassian.com/t5/image/serverpage/image-id/335586iFA5D01AD46036038/image-dimensions/350x257?v=v2 "Screenshot 2024-07-15 at 10.22.44.png")

The lookup table in the first action:

![Screenshot 2024-07-15 at 10.22.57.png](https://community.atlassian.com/t5/image/serverpage/image-id/335587iD9A4EA33499822A4/image-dimensions/264x353?v=v2 "Screenshot 2024-07-15 at 10.22.57.png")

The extract the quarter and put in a variable together with the year:

![Screenshot 2024-07-15 at 10.26.01.png](https://community.atlassian.com/t5/image/serverpage/image-id/335589iDC97B5B811A2649F/image-dimensions/391x311?v=v2 "Screenshot 2024-07-15 at 10.26.01.png")

Several things happening here:

* We create a variable **quarter**
* First part is extracting the quarter form your **quarters** table using the month (M) of the date field.
* Second part is extracting the year (YY) from the date and append it to the first part.

Now all you have to do is add an action step, editing the issue and setting the select field with the **quarter** variable.

Good luck!

comments: author: Venkatesh Pettem
body: HI [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) , I have tried to create lookup table in automation, but the suggested lookup table is not available in Automation. we are using 9.10.1 Jira version. Please help.

Regards,

Venkatesh


author: Jeroen Poismans
body: Ok, so I modified the rule so you it should work in your version:\]

![Screenshot 2024-07-16 at 09.25.59.png](https://community.atlassian.com/t5/image/serverpage/image-id/335808i8B0B91378A43A0C3/image-size/large?v=v2&px=999 "Screenshot 2024-07-16 at 09.25.59.png")

* First I create a variable that extracts the month  

  ```
  {{issue.duedate.format("M")}}
  ```

* Then I create IF-ELSE blocks using the **Advanced compare condition**   

  ![Screenshot 2024-07-16 at 09.31.16.png](https://community.atlassian.com/t5/image/serverpage/image-id/335810iDC1A226A45272328/image-dimensions/303x229?v=v2 "Screenshot 2024-07-16 at 09.31.16.png")  

  You can see I use a regex compare there, stating in the first IF that the **month** variable should be 1,2 or 3  

* Then I log the value that you want in the select list

  ```
  Q1 {{issue.duedate.format("YY")}}
  ```

  You should change this to the **Edit issue** action, to edit and set your select list.  

* Repeat for the other possible ranges, quarters (see screenshot).

Jeroen


author: Venkatesh Pettem
body: Hi [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) , There is no "Create variable" Action in my version 9.10.1.

Please suggest an alternative way.

Thanks


author: Venkatesh Pettem
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

Could you please help on this ticket.

Regards,

Venkatesh


author: Venkatesh Pettem
body: Below is the behavior script

<br />

<br />

import com.atlassian.jira.event.type.EventDispatchOption  
import com.atlassian.jira.issue.Issue  
import java.sql.Timestamp  
import com.atlassian.jira.component.ComponentAccessor

<br />

def dateFieldId = "customfield_18303"  
def selectListFieldId = "customfield_19813"

def issueManager = ComponentAccessor.issueManager  
def customFieldManager = ComponentAccessor.customFieldManager

// def issue = issueManager.getIssueByCurrentKey("MBD-3828")

def issue = getUnderlyingIssue()

<br />

<br />

def selectedDateValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(dateFieldId)) as Timestamp

def dateRanges = \[

\[startDate: Timestamp.valueOf("2024-01-01 00:00:00"), endDate: Timestamp.valueOf("2024-03-31 23:59:59"), optionValue: "2024 Q1"\],  
\[startDate: Timestamp.valueOf("2024-04-01 00:00:00"), endDate: Timestamp.valueOf("2024-06-30 23:59:59"), optionValue: "2024 Q2"\],  
\[startDate: Timestamp.valueOf("2024-07-01 00:00:00"), endDate: Timestamp.valueOf("2024-09-30 23:59:59"), optionValue: "2024 Q3"\],  
\[startDate: Timestamp.valueOf("2024-10-01 00:00:00"), endDate: Timestamp.valueOf("2024-12-31 23:59:59"), optionValue: "2024 Q4"\],

<br />

\]

def selectedOptionValue

dateRanges.each { range -\>

if(selectedDateValue \>= range.startDate \&\& selectedDateValue \<= range.endDate){  
selectedOptionValue = range.optionValue  
}  
}

issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(selectListFieldId), selectedOptionValue)

<br />

issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)

<br />

<br />

<br />

<br />

<br />


author: Jeroen Poismans
body: Are you sure about that **Create variable** option? I am on 9.4.X and it is there:

![Screenshot 2024-07-18 at 13.11.37.png](https://community.atlassian.com/t5/image/serverpage/image-id/336322i4E75C88F347940C7/image-dimensions/379x322?v=v2 "Screenshot 2024-07-18 at 13.11.37.png")

Regards!


author: Venkatesh Pettem
body: Hi [@Jeroen Poismans](/t5/user/viewprofilepage/user-id/1708479) , Yes it is there in Prod but not in UAT even though both the environments are running on same version.

Could you please tell me what the month variable is holding smart value?

Thanks,

Venkatesh


author: Jeroen Poismans
body: Hi,

I use this expression:

```
{{issue.duedate.format("M")}}
```

It extracts the month number.

Jeroen


Tags: data-center

jira-data-center