Based on the field value need to restrict the dates

king jira
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 25, 2024

Hi All,

Good day!

 On A project we have field called change type, it's a single select field, on that custom field having multiple values, but if the values are Emergency/Expedite, we need to restrict two date fields (planed start date and planed end date) Back dates should be allowed for last 3 days. for remaining change type values, they can enter any dates. but if the values are Emergency/Expedite we need restrict dates (planed start date, planed end date) should be allowed for last 3 days. We have script runner plugin; I believe this requirement we can achieve through behaviors. Can you please create script?   below are the custom field ids.

Planned end date :10950
Planned start date: 10949
Change type: 11100

Please take this ask as high priority.






2 answers

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 27, 2024

I am afraid you have been talking to an AI that has no understanding of your question, Atlassian software, or the code you would need to write here.

That is apparent from the fact that the code you have been given is not valid for a behaviour or a workflow condition or validator, partly because it attempts to use both, and also tries to define objects that are not valid. 

A behaviour to check a date would be written more like:

const dateField = getFieldById("customfield_12136");

// Get the current value of the date field
const dateValue = dateField.getValue();

if (typeof dateValue === 'string' && dateValue) {
    const currentDate = new Date();
    const selectedDate = new Date(dateValue);
    
    // Clear any previous error by using the description to temporarily show errors
    const descriptionField = getFieldById("description");
    descriptionField.setDescription('');

    // Check if the selected date is in the past by comparing only the date parts
    if (selectedDate.setHours(0, 0, 0, 0) < currentDate.setHours(0, 0, 0, 0)) {
        descriptionField.setDescription("**Error**: The date must be in the present or future.");
    }
}

 

0 votes
Prem May 27, 2024

Hi @king jira 

I hope below script will help -


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.onresolve.jira.groovy.user.legacy.DefaultFieldLayoutManager
import groovy.time.TimeCategory
import java.text.SimpleDateFormat

--- Define the custom field IDs
def changeTypeFieldId = "customfield_11100"
def plannedStartDateFieldId = "customfield_10949"
def plannedEndDateFieldId = "customfield_10950"

--- Get the current field objects
FormField changeTypeField = getFieldById(changeTypeFieldId)
FormField plannedStartDateField = getFieldById(plannedStartDateFieldId)
FormField plannedEndDateField = getFieldById(plannedEndDateFieldId)

--- Get the current values of the fields
String changeTypeValue = changeTypeField.getValue() as String
String plannedStartDateValue = plannedStartDateField.getValue() as String
String plannedEndDateValue = plannedEndDateField.getValue() as String

--- Define the date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")

--- Get the current date and the date three days ago
Date currentDate = new Date()
Date threeDaysAgo = use(TimeCategory) { currentDate - 3.days }

--- Convert string dates to Date objects
Date plannedStartDate = plannedStartDateValue ? sdf.parse(plannedStartDateValue) : null
Date plannedEndDate = plannedEndDateValue ? sdf.parse(plannedEndDateValue) : null

--- Check if the change type is Emergency or Expedite
if (changeTypeValue == "Emergency" || changeTypeValue == "Expedite") {
--- Validate the Planned Start Date
if (plannedStartDate && (plannedStartDate.before(threeDaysAgo) || plannedStartDate.after(currentDate))) {
plannedStartDateField.setError("Planned Start Date must be within the last 3 days for Emergency or Expedite change types.")
} else {
plannedStartDateField.clearError()
}

--- Validate the Planned End Date
if (plannedEndDate && (plannedEndDate.before(threeDaysAgo) || plannedEndDate.after(currentDate))) {
plannedEndDateField.setError("Planned End Date must be within the last 3 days for Emergency or Expedite change types.")
} else {
plannedEndDateField.clearError()
}
} else {
--- Clear errors if the change type is not Emergency or Expedite
plannedStartDateField.clearError()
plannedEndDateField.clearError()
}

 

 

Mustafa May 27, 2024

Hi Prem,

We run the script on console, for testing purpose but we got few errors, please find the below erros.

The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script54.groovy: 37: Unexpected input: 'the' @ line 37, column 14. --- Check if the change type is Emergency or Expedite ^ 1 error </pre>.



we have created the behaviour and we have selected the planed start date and  planed end date   and we pasted the above script. 


Can you please modify the script?

Best,
Mustafa 

Prem May 27, 2024

Hi @Mustafa ,

 

I guess you should have shared your version of script in order to rectify the error.
Anyways, Please give a shot to below -

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.onresolve.jira.groovy.user.legacy.DefaultFieldLayoutManager
import groovy.time.TimeCategory
import java.text.SimpleDateFormat

// Define the custom field IDs
def changeTypeFieldId = "customfield_11100"
def plannedStartDateFieldId = "customfield_10949"
def plannedEndDateFieldId = "customfield_10950"

// Get the current field objects
FormField changeTypeField = getFieldById(changeTypeFieldId)
FormField plannedStartDateField = getFieldById(plannedStartDateFieldId)
FormField plannedEndDateField = getFieldById(plannedEndDateFieldId)

// Get the current values of the fields
String changeTypeValue = changeTypeField.getValue() as String
String plannedStartDateValue = plannedStartDateField.getValue() as String
String plannedEndDateValue = plannedEndDateField.getValue() as String

// Define the date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")

// Get the current date and the date three days ago
Date currentDate = new Date()
Date threeDaysAgo

// Calculate the date three days ago using TimeCategory
use(TimeCategory) {
    threeDaysAgo = currentDate - 3.days
}

// Convert string dates to Date objects
Date plannedStartDate = plannedStartDateValue ? sdf.parse(plannedStartDateValue) : null
Date plannedEndDate = plannedEndDateValue ? sdf.parse(plannedEndDateValue) : null

// Check if the change type is Emergency or Expedite
if (changeTypeValue == "Emergency" || changeTypeValue == "Expedite") {
    // Validate the Planned Start Date
    if (plannedStartDate && (plannedStartDate.before(threeDaysAgo) || plannedStartDate.after(currentDate))) {
        plannedStartDateField.setError("Planned Start Date must be within the last 3 days for Emergency or Expedite change types.")
    } else {
        plannedStartDateField.clearError()
    }

    // Validate the Planned End Date
    if (plannedEndDate && (plannedEndDate.before(threeDaysAgo) || plannedEndDate.after(currentDate))) {
        plannedEndDateField.setError("Planned End Date must be within the last 3 days for Emergency or Expedite change types.")
    } else {
        plannedEndDateField.clearError()
    }
} else {
    // Clear errors if the change type is not Emergency or Expedite
    plannedStartDateField.clearError()
    plannedEndDateField.clearError()
}
Mustafa May 27, 2024

hi Prem,

 

Getting this error.

The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script58.groovy: 7: unable to resolve class com.onresolve.jira.groovy.user.legacy.DefaultFieldLayoutManager @ line 7, column 1. import com.onresolve.jira.groovy.user.legacy.DefaultFieldLayoutManager ^ 1 error </pre>.

Best,
Musthafha

Prem May 27, 2024

Hi @Mustafa 

 

Replace all the imports of the script with the below - 
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.MutableIssue

import com.onresolve.jira.groovy.user.FieldBehaviours

import com.onresolve.jira.groovy.user.FormField

import groovy.time.TimeCategory

import java.text.SimpleDateFormat

Mustafa May 27, 2024

Hi Prem,

 

Can we connect for quick call? 

 

 

Best,

mustafa

Prem May 27, 2024

Hi @Mustafa 

 

I would rather suggest you to share your version of script and outline the error's you are getting during compilation. I will try to rectify those. If the call would be required then I will surely approach you for the same.

Mustafa May 27, 2024

Hi Prem,

Version: 8.2.0

and error 
The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script60.groovy: 37: Unexpected input: 'the' @ line 37, column 14. --- Check if the change type is Emergency or Expedite ^ 1 error </pre>.


Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.12.6
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events