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.
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.");
}
}
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()
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 -
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.