hello,
I am writing a script using scriptrunner for Jira to add restriction on a particular date field. i am creating listener to check action but while doing so system is not able to import any of libraries and due to which i am not able work script.
My query is what will be better option a listener or behaviour? If listener then how to import library?
Hello!
Both have different use cases. A listener is suitable when you want to react to events like issue updates, while a behaviour is better for dynamically modifying fields on the creation or edit screen.
In this case, using a listener would be more appropriate for applying the restriction you mentioned. If you need help, feel free to share your script, and I’ll be happy to review it and see what’s missing.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is this for jira cloud or server? since the tag specifies cloud but the script is server script, if you confirm me
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If it is for the cloud, the whole approach changes since it is different. I'll leave you something as an example:
import groovy.json.JsonSlurper
def issueKey = "TEST-3"
def issueResponse = get("/rest/api/3/issue/${issueKey}?fields=customfield_XXXXX") // Replace XXXXX with custom field ID
.header('Content-Type', 'application/json')
.asString()
// Parse the response
def issueDetails = new JsonSlurper().parseText(issueResponse.body)
def customFieldValue = issueDetails.fields.customfield_XXXXX // Replace XXXXX with the custom field ID
// Check if the custom field value exists and is a valid date
if (customFieldValue) {
def customDate = Date.parse("yyyy-MM-dd", customFieldValue)
def currentDate = new Date()
// Compare the custom field date with the current date
if (customDate.before(currentDate)) {
logger.warn("The date is in the past.")
throw new Exception("The date cannot be in the past.")
}
} else {
logger.warn("The custom field does not contain a valid date.")
}
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.
With the script I left you, you can now compare the dates. The import error was given because the script you were using is from "Jira Server" which is totally different. I'm leaving you the official ScriptRunner page with some examples so you can review how they are created:
https://docs.adaptavist.com/sr4jc/latest/features/script-listeners/script-listener-examples
If you have any other questions about the script, feel free to ask.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Arti Patil Welcome to Atlassian Community!
Use SR Behaviour or SR Validator for applying restriction on a Date field while creating a new issue.
What type of restriction are you trying to apply ?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.