Heads up! On March 5, starting at 4:30 PM Central Time, our community will be undergoing scheduled maintenance for a few hours. During this time, you will find the site temporarily inaccessible. Thanks for your patience. Read more.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

About ScriptRunner Examples

Barış Özer
Contributor
October 9, 2024

Hello,

I want to do the following operations with ScriptRunner, but I always get errors. I asked chatgpt and it gave an error again.

 

Summary (text) field must be at least 10 characters. (Validator)

The Details field is mandatory when the Reason (select list) field is selected in Jira. (Validator)

The End Date (Date Time Picker) field cannot be selected older than the System time. (Validator)

 

I am attaching the scripts given by chatgpt as images.

image.png

I tried the Summary and Reason script in the "Create" transition.

I tried the End Date script in the transition linking to "Resolved".

 

It is very important for me. Can you help please?

 

I used this site https://leap.scriptrunnerhq.com/expression but I didn't understand it. How do I add the output it gives me to the transition section?

4 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
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 9, 2024

Hi, you are confusing Jira Server scripts for Jira Cloud, I leave you the scripts that will help you in Jira Cloud


Validator for "Summary" Field

// Check if the summary length is at least 10 characters
if (issue.fields.summary?.length() >= 10) {
    return true
} else {
    return false
}

Validator for "Reason" and "Details" Fields

def reasonFieldId = "customfield_XXXXX" // Replace with the ID of the "Reason" field
def detailsFieldId = "customfield_YYYYY" // Replace with the ID of the "Details" field

// Retrieve the values of the fields
def reason = issue.fields[reasonFieldId]?.value
def details = issue.fields[detailsFieldId]

// If "Reason" is "Jira" and "Details" is empty, validation fails
if (reason == "Jira" && !details) {
    return false
} else {
    return true
}

 


Validator for "End Date"

def endDateFieldId = "customfield_ZZZZZ" // Replace with the ID of the "End Date" field
def endDate = issue.fields[endDateFieldId]?.isoDate
def now = ZonedDateTime.now()

// Check if the end date is in the future
if (endDate && ZonedDateTime.parse(endDate).isAfter(now)) {
    return true
} else {
    return false
}

If you have any questions or want to do something specific, don't hesitate to ask.

Barış Özer
Contributor
October 9, 2024

Hi @Marcelo Ulloa 

 

First of all, thank you very much. (:

I tried, but I got an error. I share the details below.

 

image.png

 

Expression Log

{

    "timestamp": "2024-10-09T17:22:03.323Z",

    "webhookEvent": "jira_expression_evaluation_failed",

    "expression": "def reasonFieldId = \"customfield_10071\"\ndef detailsFieldId = \"customfield_10073\"\n\ndef reason = issue.fields[reasonFieldId]?.value\ndef details = issue.fields[detailsFieldId]\n\n\nif (reason == \"Jira\" && !details) {\n    return false\n} else {\n    return true\n}",

    "errorMessages": [

        "Jira expression failed to parse: line 1, column 5:\n=, =>, ., ?., [, ?.[, (, *, /, %, +, -, <, <=, >, >=, ==, !=, &&, ||, ??, ?, ; or EOF expected, reasonFieldId encountered."

    ],

    "context": {

        "issue": {

            "fields": {

                "summary": "1231231231233213",

                "issuetype": {

                    "self": "https://hbtestcase.atlassian.net/rest/api/2/issuetype/10013",

                    "id": "10013",

                    "description": "",

                    "iconUrl": "https://hbtestcase.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10309?size=medium",

                    "name": "Support",

                    "subtask": false,

                    "avatarId": 10309,

                    "hierarchyLevel": 0

                },

                "assignee": null,

                "priority": {

                    "self": "https://hbtestcase.atlassian.net/rest/api/2/priority/3",

                    "iconUrl": "https://hbtestcase.atlassian.net/images/icons/priorities/medium.svg",

                    "name": "Medium",

                    "id": "3"

                },

                "status": null

            }

        },

        "project": {

            "self": "https://hbtestcase.atlassian.net/rest/api/2/project/10001",

            "id": "10001",

            "key": "HBHC",

            "assigneeType": "PROJECT_LEAD",

            "name": "HB Help Center",

            "roles": {},

            "avatarUrls": {

                "48x48": "https://hbtestcase.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10419",

                "24x24": "https://hbtestcase.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10419?size=small",

                "16x16": "https://hbtestcase.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10419?size=xsmall",

                "32x32": "https://hbtestcase.atlassian.net/rest/api/2/universal_avatar/view/type/project/avatar/10419?size=medium"

            },

            "projectTypeKey": "service_desk",

            "simplified": false,

            "style": "classic",

            "properties": {}

        },

        "serviceDesk": {

            "id": "1",

            "projectId": "10001",

            "projectName": "HB Help Center",

            "projectKey": "HBHC",

            "_links": {

                "self": "https://hbtestcase.atlassian.net/rest/servicedeskapi/servicedesk/1"

            }

        },

        "user": {

            "self": "https://hbtestcase.atlassian.net/rest/api/2/user?accountId=712020:9ea6ee70-d7ba-4468-b817-4d14e40860a7",

            "accountId": "712020:9ea6ee70-d7ba-4468-b817-4d14e40860a7",

            "avatarUrls": {

                "48x48": "https://secure.gravatar.com/avatar/68cececb9158e29c1c1f1038580ddcd0?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Fdefault-avatar-2.png",

                "24x24": "https://secure.gravatar.com/avatar/68cececb9158e29c1c1f1038580ddcd0?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Fdefault-avatar-2.png",

                "16x16": "https://secure.gravatar.com/avatar/68cececb9158e29c1c1f1038580ddcd0?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Fdefault-avatar-2.png",

                "32x32": "https://secure.gravatar.com/avatar/68cececb9158e29c1c1f1038580ddcd0?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Fdefault-avatar-2.png"

            },

            "displayName": "Barış Özer",

            "active": true

        },

        "config": {

            "expression": "def reasonFieldId = \"customfield_10071\"\ndef detailsFieldId = \"customfield_10073\"\n\ndef reason = issue.fields[reasonFieldId]?.value\ndef details = issue.fields[detailsFieldId]\n\n\nif (reason == \"Jira\" && !details) {\n    return false\n} else {\n    return true\n}",

            "errorMessage": "If the Reason field is selected as \"Jira\", the \"Details\" field must be filled.",

            "description": "Reason and Details field Validator",

            "uuid": "68562683-c183-45dd-9c44-c0ed9a0ca61e",

            "enabled": true

        },

        "transition": {

            "name": "Create",

            "from": null,

            "id": 1,

            "to": {

                "self": "https://hbtestcase.atlassian.net/rest/api/2/status/1",

                "description": "",

                "iconUrl": "https://hbtestcase.atlassian.net/images/icons/statuses/open.png",

                "name": "Open",

                "id": "1",

                "statusCategory": {

                    "self": "https://hbtestcase.atlassian.net/rest/api/2/statuscategory/2",

                    "id": 2,

                    "key": "new",

                    "colorName": "blue-gray",

                    "name": "New"

                }

            }

        }

    },

    "validator.id": "2c752a0e-ea13-4ae7-9cf7-fb7300a8ef8c",

    "workflow.name": "HBHC: Jira Service Management IT Support Workflow",

    "moduleKey": "script-workflow-validators"

}

 

After selecting the "Reason" field "Jira" on the Create Screen, I left the "Details" blank. After clicking Create it gave this error.

image.png

 

Kind regards,

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 9, 2024

@Barış Özer  I apologize, I made them with a listener in mind, so I'll leave them here so they can be in a validator.

 

Use the fields you had on hand, but it would just be changing them.

 

Validator for "Summary" Field

image.png

 

Validator for "Reason" and "Details" Fields

 

issue.customfield_11361?.value == "Contributor" && (!issue.customfield_11359 || issue.customfield_11359.trim() == "")

image.png

 

Validator for "End Date"

 

image.png

0 votes
Answer accepted
Evgenii
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 9, 2024

Hi, @Barış Özer 

ChatGPT examples are mostly correct, but they are for on-premise solution.
If I understood tags, that you added, right, you are using Jira Cloud.

You need to use validators based on expressions. You can find examples here:
https://docs.adaptavist.com/sr4jc/latest/features/workflow-extensions/jira-expression-examples

First example is exactly what you're looking for - summary, larger than 10 symbols.
Also there are examples for other field types.

Barış Özer
Contributor
October 9, 2024

Hi @Evgenii 

It gives an error.

Where am I doing wrong?

image.png

Evgenii
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 9, 2024

It's an example. You need to know basics of programming.

For summary checking you can use:

if (issue.summary.length >= 10 ) {
return true
} else {
return false
}
Barış Özer
Contributor
October 9, 2024

Hi @Evgenii 

I created an issue.

Summary I wrote more than 10 characters, but I was still able to create it.

Where am I doing wrong?

I added "validator" to the create transition.

image.png

image.png

I think if I learn this, I can do it for the rest.

Evgenii
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 9, 2024

I don't understand what you are trying to make them.
That was your question:
Summary (text) field must be at least 10 characters. (Validator)

"at least" means, that it must be 10 symbols or longer.

If you want summary exactly 10 symbols long - it's "issue.summary.length == 10" condition.
If you want summary no longer than 10 symbols - it's "issue.summary.length <= 10"

 



Barış Özer
Contributor
October 9, 2024

Hi @Evgenii 

I'm really, really sorry.

I've been trying for 2 days and I can't see it :'(

 

The Details field is mandatory when the Reason (select list) field is selected in Jira. (Validator)

The End Date (Date Time Picker) field cannot be selected older than the System time. (Validator)

 

How can I do it for the above 2 items? Can you give a recommendation or website?

Evgenii
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 9, 2024

I don't know customfiled IDs in your system. You have to find it by yourself.

The End Date (Date Time Picker) field cannot be selected older than the System time. (Validator)

 

issue.customfield_11111 ? 
    new CalendarDate(issue.customfield_11111) <= new CalendarDate() :
    false

 
> The Details field is mandatory when the Reason (select list) field is selected in Jira. (Validator)

if (issue.customfield_22222) {
   if(issue.customfield_33333) {
      return true
   } else {
return false
} }

 Instead of 11111, 22222, 33333 use customfield ids from your system

Barış Özer
Contributor
October 9, 2024

Hi @Evgenii 

I got an error like below.

customfield value is correct. I checked.

I added a resolve transition, is that correct?

After the Resolve status, you can see on the right "End Date" field will be written manually. (Cannot be selected older than system time)

Where and what am I doing wrong? :(

image.png

image.png

image.png

 

> The Details field is mandatory when the Reason (select list) field is selected in Jira. (Validator)

if (issue.customfield_22222) {

   if(issue.customfield_33333) {

      return true

   } else {

      return false

   }

}

This didn't work either :(  

Thank you very much for the help..

0 votes
Barış Özer
Contributor
October 9, 2024

Hi @Marcelo Ulloa 

I tried the last one you sent, but it still didn't work :(

I don't know what to do. I've run out of ideas.

Is it the right time to give up?

I don't know where is wrong...

0 votes
Barış Özer
Contributor
October 9, 2024

Hi @Evgenii 

The End Date (Date Time Picker) field cannot be selected older than the System time. (Validator)

I did it, it works.

 

But I still couldn't find the one below...

The Details field is mandatory when the Reason (select list) field is selected in Jira. (Validator)

Evgenii
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 9, 2024
if (issue.customfield_22222) {
    return issue.customfield_33333 != null
} else {
    return true
}

Try this

Barış Özer
Contributor
October 9, 2024

Hi @Evgenii 

Sorry, it gave an error :(

I selected Reason field Jira and left Details blank. I pressed Create and it gave the following error. It acts as if the Details field is empty.

 

image.png

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events