We have custom field having about 1000 options. We need to sort all these options alphabetically using REST API call. I am using below REST API code from “https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issue-custom-field-options/#api-rest-api-2-field-fieldid-context-contextid-option-move-put” site and passing all 1000 option Ids but this call is not sorting my options propeprly. Could you please provide help here.
// This sample uses Atlassian Forge
// https://developer.atlassian.com/platform/forge/
import api, { route } from "@forge/api";
var bodyData = `{
"customFieldOptionIds": [
"10001",
"10002"
],
"position": "First"
}`;
const response = await api.asUser().requestJira(route`/rest/api/2/field/{fieldId}/context/{contextId}/option/move`, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: bodyData
});
console.log(`Response: ${response.status} ${response.statusText}`);
console.log(await response.json());
Hello @ArunaKumbhar
That code you’re provided is just one of the examples from the documentation page.
You will need to provide your code that shows how you are “passing all 1000 option Ids” and using that endpoint to put them into alphabetic order.
Below is the actual code:
function Set-JIRAClientListSort {
<#
.SYNOPSIS
Re-sorts custom field 10081 ClientList sort order (ascending by client name
.NOTES
DK 9/28/2017
#>
[CmdletBinding()]
param(
[parameter(Mandatory=$False,ValueFromPipeline=$True)][string] $baseURI= "https://arroweye-jira-cloud.atlassian.net/rest/api/2/field/customfield_10081/context/10176/option"
)
#[String] $body = '{"locale": "en-EN","strength": "PRIMARY","order": "ASCENDING"}';
$response = Invoke-JIRARequest -URI $baseURI -method GET
$optionIds = ""
$startAtValue = 0
for($x=0; $x -lt $response.total; $x=$x+100)
{
$URI = $baseURI + "?startAt="+ $startAtValue
$response = Invoke-JIRARequest -URI $URI -method GET
foreach($value in $response.values)
{
if($optionIds -eq "")
{
$optionIds = '"'+$value.id+'"'
$counter = $counter + 1
}
else
{
$optionIds = $optionIds + "," +'"'+$value.id+'"'
$counter = $counter + 1
}
}
$startAtValue = $startAtValue + 100
$URI = ""
}
$sortURI = $baseURI + "/move"
[String] $body = '{"customFieldOptionIds": ['+ $optionIds +'], "position": "First"}';
$result= Invoke-JIRARequest -URI $sortURI -method PUT -body $body
return $result
}
I’m not quite sure how this is supposed to cause all the options in the fields to become alphabetic. From what I see, this happens:
- You GET all the options from the field customfield_10081, in batches of 100
- You put all the ids of those options into a string array called
$optionIds
, in the sequence given to you in the response
- You then PUT all those option ids back into the field customfield_10081 in the exact same order as you got them, but declare they must ALL be
first
, which just puts the associated options all back exactly where and in in the order that you got them
If so, there seems to be no intermediate sorting of the $optionIds
array to make the contents ‘alphabetic’ that I can see.
- Your code looks like PowerShell. Is that correct?
- What is
Invoke-JIRARequest
? Is it some sort of command / function provided by an external library / module? If so, which one?
- Are you just assuming that the first GET call to the Get custom field options endpoint using
Invoke-JIRARequest
will return the results in alphabetic order because you declared “order”: “ASCENDING”
in the body of the first request?
- Did you check the results of the first GET request to see if the results returned was actually in alphabetic order?
- When you tested all your methods with a test tool like Postman first, to interact directly with the REST API endpoints, did you get the same results that the
Invoke-JIRARequest
function gave?
Thanks for your prompt reply. Appreciate it. Answers to your questions:
- Yes, it is powershell code
- Invoke-JIRARequest method just gives call to REST API by passing proper authorization and returns response from API
- “order”: “ASCENDING” is commented out. This code was used when the client was using on-prem JIRA. But now they have migrated to cloud JIRA
- GET request do not return results in alphabetical order. It just returns all values in the order as they are on JIRA portal. After getting all options, I am using REST API call (https://arroweye-jira-cloud.atlassian.net/rest/api/2/field/customfield_10081/context/10176/option/move) to reorder the list which I returned from GET method
- We are not using POSTMAN but using powershell function calls to test our API calls
Please let me know if you need any other details from me.
There are some problems.
- There is no native PowerShell command
Invoke-JIRARequest
. You must be using some sort of third party module. Or, somewhere else in your code, a native command like Invoke-RestMethod
has been turned into a new function.
- The Reorder custom field options endpoint does not do any form of sorting for you. YOU MUST TELL IT WHAT ORDER YOU WANT.
- Only once you have done the sorting first, and you have a new array of option ids in the correct order, then you PUT that array back into the custom field using the endpoint
You may want to take the time to read the documentation a bit more thoroughly.
The other way to achieve what you want is to totally erase and re-build the custom field’s options:
-
GET all the existing options from the custom field and sort them alphabetically in your code
-
DELETE all the existing options in the custom field
-
POST in the whole new set of options, in their alphabetic order
NOTE. this method causes all new IDs to be generated, so they will not have the same relationship to the option values as they previously did.
Also, trying doing some basic testing with a REST API test tool first. It will save lots of wasted time.
Good luck
2 Likes
I got it now. Thank you so much for your detailed reply.
1 Like
No problem. If you could mark that solution as correct, it would be appreciated.