Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,301,425
Community Members
 
Community Events
165
Community Groups

Updating organization with Script Runner

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

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);

Thank you for your response! Now it works. 

0 votes

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.

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

📅 Partner Webinar & Events Opportunities: July 2022

Hi community 👋, Here is another monthly roundup of the partner-organized events in July 2022. If there is any event missing please let me know in the comments below 🙏 If you're looking for Atla...

24 views 0 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you