how to import scriptrunner library for Jira Cloud

Arti Patil October 2, 2024

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?

2 answers

1 accepted

0 votes
Answer accepted
Marcelo Ulloa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 3, 2024

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.

Arti Patil October 4, 2024
this script i am using for listner but i am not able to import library, so all functions like "def" are giving error.
i am trying to put validation on date- that date should never be in past on In Progress Status.

//define custom field name
def customFieldName = "dateReview'


//get the custom object
def customField = customFieldManager.getCustomFieldObjectByName(customFieldName)
if (!customField){
    log.error("Custom Field '${customFieldName}' Not Found")
    return
}
//get issue from event
def issue = Event.issue
def customFieldValue = issue.getCustomFieldValue(customField)
//check custome field value a date
if(getCustomFieldValue instanceof java.util.Date){
    def currentDate = new Date()
}
//compare dates
if(customFieldValue.before(currentDate))
{
    //log warning
    log.warn( "the date is in past")
    // throe error for another action
    throw new Exception("the date cannot be past")
}
else
log.warn (" custom feild does not contain valide date")
Marcelo Ulloa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 2024

Is this for jira cloud or server? since the tag specifies cloud but the script is server script, if you confirm me

Marcelo Ulloa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 2024

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.")
}
Arti Patil October 4, 2024

i am using jira cloud

Marcelo Ulloa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 4, 2024

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.

Like Walter Buggenhout likes this
0 votes
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2024

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 ? 

 

Arti Patil October 4, 2024

validation is date should never be in past.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events