Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set the custom field value based on custom field date Range

Venkatesh Pettem July 15, 2024

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

3 answers

0 votes
Venkatesh Pettem July 17, 2024

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
}
});
});

0 votes
Venkatesh Pettem July 17, 2024

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 } }); });

0 votes
Jeroen Poismans
Community Champion
July 15, 2024

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

The lookup table in the first action:

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

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!

Venkatesh Pettem July 15, 2024

HI @Jeroen Poismans , 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

Jeroen Poismans
Community Champion
July 16, 2024

Ok, so I modified the rule so you it should work in your version:]

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

    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

Venkatesh Pettem July 16, 2024

Hi @Jeroen Poismans , There is no "Create variable" Action in my version 9.10.1.

Please suggest an alternative way.

Thanks

Venkatesh Pettem July 17, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Could you please help on this ticket.

Regards,

Venkatesh

Venkatesh Pettem July 17, 2024

Below is the behavior script

 



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


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()



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"],


]

def selectedOptionValue

dateRanges.each { range ->

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

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


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






Jeroen Poismans
Community Champion
July 18, 2024

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

Regards!

Venkatesh Pettem July 21, 2024

Hi @Jeroen Poismans , 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

Jeroen Poismans
Community Champion
July 22, 2024

Hi,

I use this expression:

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

It extracts the month number.

Jeroen

Suggest an answer

Log in or Sign up to answer