Forums

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

How to pass an array of multiple choice field values ​​in JSON?

Artem Semenov
Contributor
April 12, 2024

Hi everybody!
I have a code that, when the status changes, transmits field values ​​in Json format to another service, the code works fine, but there is a problem with passing an array of field values, now all values ​​are transmitted as a string. How can I pass the values ​​of a multiple choice field in the format ["1param", "2param", "3param"] ?
I will be grateful for any help

 

import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovyx.net.http.HttpResponseException
import groovyx.net.http.HttpResponseDecorator
import java.util.Base64
import java.util.Base64.Encoder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import helper.RestHelper
import groovyx.net.http.ContentType
import groovy.json.JsonBuilder
import javax.ws.rs.core.Response
def client = new RESTClient(url)
//Change to match your username/password
def authBasic = Base64.getEncoder().encodeToString(("login"+":"+"pass").getBytes());
IssueManager im = ComponentAccessor.getIssueManager()
Issue currentIssue = im.getIssueObject(event.getIssue().getKey())
ChangeHistoryManager chm = ComponentAccessor.getChangeHistoryManager()
def lastChange = chm.getChangeHistories(currentIssue)[-1].getChangeItems()
log.warn(lastChange.field[1])
if ((lastChange.field[0].equals("status")) || (lastChange.field[1].equals("status")))  {    
   log.warn("status") 
        String oldStatus = lastChange.oldstring[0]
        String newStatus = lastChange.newstring[0]
    if (!newStatus.equals(oldStatus)) {
        log.warn("test")
        log.warn(currentIssue.getCustomFieldValue(16308))
        log.warn(currentIssue.getCustomFieldValue(19700))
        log.warn(currentIssue.getCustomFieldValue(19701))
        Map jsonObj = [
            "key"         : currentIssue.getKey(),
            "issueType"   : currentIssue.getIssueType().name,
            "status"   : newStatus,
            "Prichina" : currentIssue.getCustomFieldValue(16308).toString(),
            "Shtat" : currentIssue.getCustomFieldValue(19700).toString(),
            "Konkurent" : currentIssue.getCustomFieldValue(19701).toString()
    ]
try{
HttpResponseDecorator response = (HttpResponseDecorator) client.post(path: "",
      contentType: JSON,
      body: jsonObj,
  //headers: [Accept: 'application/json',Authorization:"Basic ${authBasic}"]);
    headers: [Accept: 'application/json']);
    
    log.error("Status: " + response.status)
    log.warn("requestBody: ${jsonObj}")
    if (response.data) {
      log.error("Content Type: " + response.contentType)
      log.error("Headers: " + response.getAllHeaders())
      log.error("Body:\n" + JsonOutput.prettyPrint(JsonOutput.toJson(response.data)))
      //log.error("requestBody: ${jsonObj}")
    }
}
catch(HttpResponseException e){
    log.error e.getResponse().getData()
    //log.error("requestBody: ${jsonObj}")
}
}
}

2 answers

0 votes
Artem Semenov
Contributor
April 17, 2024

I managed to solve this problem using JSON array:

def multiSelectField = customFieldManager.getCustomFieldObject("customfield_19700")
def shtat = currentIssue.getCustomFieldValue(multiSelectField) as List
JSONArray jsonArray_shtat = new JSONArray()
    if (shtat != null) {  
jsonArray_shtat = [
     shtat
     ]
     }
0 votes
Matt Parks
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.
April 15, 2024

I can't tell exactly where you're trying to set multiple values to the same field in your API call, but I found a post that had code that seemed to work for some users:

https://community.atlassian.com/t5/Jira-Software-questions/How-to-save-values-to-a-multiselect-field-using-the-rest-API/qaq-p/1524820

 

Below seems to be the basics of what they included:

{

"fields":

{ "customfield_11201": [{"value": "Disclosure"},{"value": "SecondValue"},{"value": "ThirdValue"}]

   }

}
Artem Semenov
Contributor
April 17, 2024

Thank you, this is not exactly what I needed, I managed to solve the problem using JSONArray.

Suggest an answer

Log in or Sign up to answer