Scriptrunner script to set Organizations after creation

Destri Weir February 26, 2024

I want to be able to add an Organization to an issue after creation even if the reporter chose not to share it with anyone. I currently have this working via automation, but we have so many tickets, it's maxing out our allowed automation runs, and we don't currently have another reason to use premium. 

I can find older scripts to populate the Organization field based on the reporter, but I can't find any that work with the current version of Jira Cloud. 

Does anyone have a sample script to do this?

1 answer

0 votes
Kristian Walker _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.
February 26, 2024

Hi Destri,

The example library script here shows how to add users to an organisation when an issue is created, and this may be a useful reference guide to help you to create the script that you require.

I hope this helps.

Regards,

Kristian

Destri Weir February 27, 2024


Thank you! I already have the users in organizations, but I want to add the organization associated with the user to the ticket automatically. 

Kristian Walker _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.
February 28, 2024

Hi Destri,

I this case you will need to add a script to call the edit issue API and to set the body for the organisations field.

The best way to see what structure the organisations field uses is to run the example script on the script console called Get Issue Fields to return an issue which has this field set.

This will show what structure it uses to help create the correct body structure to set this field.

If you still need further assistance with this script, can I please ask you to raise this issue as a support ticket here so that our support team can help investigate this and help you with writing the script.

I hope this helps.

Regards,

Kristian

 

Destri Weir March 1, 2024

I entered an issue with Adaptavist (Help writing script - ScriptRunner for Jira Cloud - Service project (adaptavist.com) and attempted to write a script using the help they provided, but I'm currently getting a startup failed error (and others, too). 

Any idea what I'm doing wrong?

// Step 1: Get the current issue's reporter's accountID
def issueKey = issue.key
def result = get("/rest/api/2/issue/${issueKey}")
.header("Content-Type", "application/json")
.asObject(Map)
.body

def reporterAccountId = result?.fields?.reporter?.accountId

if (reporterAccountId) {
// Step 2: Get all existing organizations
result = get("/rest/servicedeskapi/organization")
.header("Content-Type", "application/json")
.asObject(Map)
.body

def organizations = result?.values

// Step 3: Find the organization of the reporter
def reporterOrganizationId = null
organizations?.each { organization ->
result = get("/rest/servicedeskapi/organization/${organization.id}/user")
.header("Content-Type", "application/json")
.asObject(Map)
.body

def members = result?.values

if (members?.any { it?.accountId == reporterAccountId }) {
reporterOrganizationId = organization.id
return
}
}

if (reporterOrganizationId) {
// Step 4: Update the issue's organization field
result = put("/rest/api/2/issue/${issueKey}")
.header("Content-Type", "application/json")
.body([
fields: [
organization: [
id: reporterOrganizationId
]
]
])
.asString()

}


Errors are:

2024-03-01 20:40:20.983 ERROR - startup failed:
Script1.groovy: 47: Unexpected input: '{\r\n    // Step 2: Get all existing organizations\r\n    result = get("/rest/servicedeskapi/organization")\r\n        .header("Content-Type", "application/json")\r\n        .asObject(Map)\r\n        .body\r\n\r\n    def organizations = result?.values\r\n\r\n    // Step 3: Find the organization of the reporter\r\n    def reporterOrganizationId = null\r\n    organizations?.each { organization ->\r\n        result = get("/rest/servicedeskapi/organization/${organization.id}/user")\r\n            .header("Content-Type", "application/json")\r\n            .asObject(Map)\r\n            .body\r\n\r\n        def members = result?.values\r\n\r\n        if (members?.any { it?.accountId == reporterAccountId }) {\r\n            reporterOrganizationId = organization.id\r\n            return\r\n        }\r\n    }\r\n\r\n    if (reporterOrganizationId) {\r\n        // Step 4: Update the issue's organization field\r\n        result = put("/rest/api/2/issue/${issueKey}")\r\n            .header("Content-Type", "application/json")\r\n            .body([\r\n                fields: [\r\n                    organization: [\r\n                        id: reporterOrganizationId\r\n                    ]\r\n                ]\r\n            ])\r\n            .asString()\r\n                    }' @ line 47, column 22.
                       }
                        ^

1 error

2024-03-01 20:40:20.983 ERROR - Class: com.adaptavist.sr.cloud.workflow.RunScript, Config: [className:com.adaptavist.sr.cloud.workflow.RunScript, uuid:de5a0a77-789c-4a5d-92bb-6928da7ab7f8, enabled:true, executionUser:INITIATING_USER, condition:// Step 1: Get the current issue's reporter's accountID

 

Kristian Walker _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.
March 4, 2024

It looks like you may be missing a closing } at the end of your script.

Destri Weir March 7, 2024

@Kristian Walker _Adaptavist_ 

Thank you so much for your help so far. I think I'm close, but I just can't get it to work. 

 

The following script runs without error, but the GET /rest/servicedeskapi/organization/851/user asObject does not evaluate to true. Any idea what I'm doing wrong here?

// Step 1: Get the current issue's reporter's accountID
def issueKey = issue.key
def result = get("/rest/api/2/issue/${issueKey}")
.header("Content-Type", "application/json")
.asObject(Map)

if (result.status == 200) {
def reporterAccountId = result.body.fields.reporter.accountId

// Step 2: Get all existing organizations
result = get("/rest/servicedeskapi/organization")
.header("Content-Type", "application/json")
.asObject(Map)

if (result.status == 200) {
def organizations = result.body.values

// Step 3: Find the organization of the reporter
def reporterOrganizationId = null
organizations.each { organization ->
result = get("/rest/servicedeskapi/organization/${organization.id}/user")
.header("Content-Type", "application/json")
.asObject(Map)

def members = result.body.values

if (members.any { it.accountId == reporterAccountId }) {
reporterOrganizationId = organization.id
return
}
}

if (reporterOrganizationId) {
// Step 4: Update the issue's organization field
result = put("/rest/api/2/issue/${issueKey}")
.header("Content-Type", "application/json")
.body([
fields: [
"Organization": [
id: reporterOrganizationId
]
]
])
.asString()

if (result.status == 204) {
log.info("Organization field updated for issue: ${issueKey}")
} else {
log.warn("Failed to update the organization field. Response: ${result.status} - ${result.body}")
}
} else {
log.warn("Reporter not found in any organization.")
}
} else {
log.warn("Failed to retrieve organizations. Status: ${result.status}, Response: ${result.body}")
}
} else {
log.warn("Failed to find issue: Status: ${result.status} ${result.body}")
}

 

Kristian Walker _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.
March 7, 2024

Hi Destri,

Does the organisation have any users, as I have tested the rest call and it is the correct syntax.

To get help with debugging this script further, I would advise you to raise a support ticket here for our support team to help debug this further.

Regards,

Kristian

Destri Weir March 7, 2024

I am a user in the organization. I have this script running as a post function on the Create transition, I expect it to add the organization to a new request when I create one. 

Screenshot 2024-03-07 114636.png

Kristian Walker _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.
March 8, 2024

Hi Destri,

Please can you raise a support ticket for this, as requested, so that our support team can diagnose the issue further for yourself?

Regards,

Kristian

Like Destri Weir likes this

Suggest an answer

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

Atlassian Community Events