Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Updating organization with Script Runner

Berk Öztürk November 15, 2021

Hello everyone! I'm trying to update my organizations custom field which is customfield_10002. I have tried two different bodies, but I have received error from both.


1.  

import groovy.json.JsonSlurper

import groovy.json.JsonBuilder

JsonSlurper slurper = new JsonSlurper()

def issueKey = 'RFD-2'

def organizationValue = []


def result2 = get('https://berkozturk.atlassian.net/rest/servicedeskapi/organization')

.header('Content-Type', 'application/json')

.asObject(Map)

if (result2.status == 200){

def values = result2.body.get("values")

def filteredItem = values.find { it -> it.name == 'ORG2' }

organizationValue.push(filteredItem);

} else {

return "Failed to find issue: Status: ${result2.status} ${result2.body}"

}

// Note: - Atlassian provide examples for the syntax to set other custom field types in the documentation page at https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/#creating-an-issue-examples

def resultSetOrg = put("/rest/api/2/issue/${issueKey}")

// Uncomment the line below if you want to set a field which is not pressent on the screen. Note - If using this you must run the script as the ScriptRunner Add-On User.

    .queryString("overrideScreenSecurity", Boolean.TRUE)

        .header('Content-Type', 'application/json')

        .body([

        fields: [

                customfield_10002: organizationValue

        ]

])

        .asString()

if (resultSetOrg.status == 204) {

    return "The  select list field was successfully updated on the ${issueKey} issue"

} else {

    return "${resultSetOrg.status}: ${resultSetOrg.body} ${organizationValue}"

}
Since organizations field accept array, I stored my filtered data in my organizationValue array. It returned;
400: {\"errorMessages\":[],\"errors\":{\"customfield_10002\":\"The Organization ID must be a number\"}} [[id:2, name:ORG2, _links:[self:https://berkozturk.atlassian.net/rest/servicedeskapi/organization/2]]]

 

2. 

import groovy.json.JsonSlurper
import groovy.json.JsonBuilder




JsonSlurper slurper = new JsonSlurper()
def issueKey = 'RFD-2'
def organizationValue = []





def result2 = get('https://berkozturk.atlassian.net/rest/servicedeskapi/organization')
.header('Content-Type', 'application/json')
.asObject(Map)
if (result2.status == 200){
def values = result2.body.get("values")
def filteredItem = values.find { it -> it.name == 'ORG2' }
organizationValue.push(filteredItem);
} else {
return "Failed to find issue: Status: ${result2.status} ${result2.body}"
}





/
def resultSetOrg = put("/rest/api/2/issue/${issueKey}")
// Uncomment the line below if you want to set a field which is not pressent on the screen. Note - If using this you must run the script as the ScriptRunner Add-On User.
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_10002: [2]
]
])
.asString()

if (resultSetOrg.status == 204) {
return "The select list field was successfully updated on the ${issueKey} issue"
} else {
return "${resultSetOrg.status}: ${resultSetOrg.body} ${organizationValue}"
}

It returned;

500: {\"errorMessages\":[\"Internal server error\"],\"errors\":{}} [[id:2, name:ORG2, _links:[self:https://berkozturk.atlassian.net/rest/servicedeskapi/organization/2]]]

 

P.S:

1)  organizationValue output 

[
  {
    "id": "2",
    "name": "ORG2",
    "_links": {
      "self": "https://berkozturk.atlassian.net/rest/servicedeskapi/organization/2"
    }
  }
]

 

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Support Team [Clearvision]
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.
November 16, 2021

Hi Berk

 

I believe the organization custom field requires an array of numbers (organization id got from 

/rest/servicedeskapi/organization

I ran the code you have and changed one line that added the organization to the field:

organizationValue.push(filteredItem);

 to

organizationValue.push(filteredItem.id as int);
Berk Öztürk November 16, 2021

Thank you for your response! Now it works. 

0 votes
Bastian Stehmann
Community Champion
November 15, 2021

Hi @Berk Öztürk ,

Welcome to the community. 

I think you should try to set the I'd of the organisation and not the whole Organisation Object.

Berk Öztürk November 15, 2021

Hi @Bastian Stehmann

My first case includes the whole object but second one has only id attribute. I got 500 internal server error, when I try to set with Id.

TAGS
AUG Leaders

Atlassian Community Events