Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

POST Create custom field options (context) via scriptrunner in jiracloud

ALARY Yann
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 13, 2024

Hello,
I'm trying to add options to a customfield using this script:

 

// The issue key

def issueKey = issue.key // or you can try with a real issuekey "PAA-679"

// Obtenir le ticket à partir de la clé de celui-ci

def ticket = get("/rest/api/2/issue/${issueKey}")

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

        .asObject(Map)

        .body

// Obtenir tous les champs

def fields = ticket.fields as Map

final issueSummary = fields.summary

final CodeProjet = issueKey.replace("-",".")

final Projet = (CodeProjet + " " + issueSummary).toString()

def Creation = true

// Obtenir les valeurs existantes

def valeurs = (get("/rest/api/3/field/customfield_10546/context/10810/option")

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

        .asObject(Map)

        .body)

for(value in valeurs.values)

{

    if (valeurs.value == Projet)

    {

        Creation = false

    }

}

if ( Creation == true )

{

def payload = [        

options: [                

disabled: false,

value: Projet

]

]

print payload

post("/rest/api/3/field/customfield_10546/context/10810/option")

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

    .body(payload)

    .asString()

}

but I keep getting this error, can you please help me? :)

 

POST request to /rest/api/3/field/customfield_10546/context/10810/option returned an error code: status: 400 - Bad Request 
body: {"errorMessages":["Invalid request payload. Refer to the REST API documentation and try again."]}

 

I tried several things, replacing the variable value with “test”, putting .asObject(Map) instead of .asString() put the post action in this format:

 

 

 

post("/rest/api/3/field/customfield_10546/context/10810/option")

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

  .body([

      options: [

      disabled : false,

      value : Projet

      ]

  ]).asString() 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Max Lim _Adaptavist_
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.
September 21, 2024

Hi @ALARY Yann, the payload is a list of new options, like:

def payload = [ 
options: [
[
value: 'newOptionA',
],
[
value: 'newOptionB',
],
]
]

You also do not actually need `disabled: false,`.

So, even you are just adding one new option, you still need brackets to enclose the new option:

def payload = [        
options: [
[
disabled: false,
value: Projet,
]
]
]

 Hope this helps.

ALARY Yann
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 23, 2024

Hi @Max Lim _Adaptavist_ ,
It works, thank you 

TAGS
AUG Leaders

Atlassian Community Events