Create an Enum Custom Field and Add It to a Project
Use the REST API to create an enum custom field with a set of values, then add this field to a specific project.
In the REST API, a CustomField entity defines the field itself. To make this field available in a project, add it to the project as a ProjectCustomField entity. For more information about custom field entities, see Custom Fields in REST API.
Summary
When you have the required data, create an enum bundle, create the custom field that uses this bundle by default, and add the custom field to the project.
Step-by-Step
Get the ID of the project where you want to add the enum custom field. For instructions, see Get a Project ID. This example uses the project
Sample Projectwith the entity ID0-0.Create an enum bundle that stores the values for the custom field. See Enum Bundles.
curl -X POST 'https://example.youtrack.cloud/api/admin/customFieldSettings/bundles/enum?fields=id,name,values(id,name)' \ -H 'Accept: application/json' \ -H 'Authorization: Bearer <YouTrack_token>' \ -H 'Content-Type: application/json' \ -d '{ "name": "Customer Impact", "values": [ {"name": "Low", "$type": "EnumBundleElement"}, {"name": "Medium", "$type": "EnumBundleElement"}, {"name": "High", "$type": "EnumBundleElement"} ], "$type": "EnumBundle" }'If the request is successful, the response contains the created enum bundle.
{ "values": [ { "name": "Low", "id": "67-12", "$type": "EnumBundleElement" }, { "name": "Medium", "id": "67-13", "$type": "EnumBundleElement" }, { "name": "High", "id": "67-14", "$type": "EnumBundleElement" } ], "name": "Customer Impact", "id": "86-10", "$type": "EnumBundle" }In the response, locate the entity ID of the created bundle. This example uses
86-10.Create the enum custom field. In the request body, set the
fieldTypetoenum[1]and set the default bundle to the enum bundle you created in the previous step. See Create CustomField.curl -X POST 'https://example.youtrack.cloud/api/admin/customFieldSettings/customFields?fields=id,name,fieldType(id),fieldDefaults(bundle(id,name),$type)' \ -H 'Accept: application/json' \ -H 'Authorization: Bearer <YouTrack_token>' \ -H 'Content-Type: application/json' \ -d '{ "name": "Customer Impact", "fieldType": {"id": "enum[1]"}, "fieldDefaults": { "bundle": {"id": "86-10", "$type": "EnumBundle"}, "$type": "EnumBundleCustomFieldDefaults" } }'If the request is successful, the response contains the created custom field.
{ "name": "Customer Impact", "fieldType": { "id": "enum[1]", "$type": "FieldType" }, "fieldDefaults": { "bundle": { "name": "Customer Impact", "id": "86-10", "$type": "EnumBundle" }, "$type": "EnumBundleCustomFieldDefaults" }, "id": "58-80", "$type": "CustomField" }In the response, locate the entity ID of the created custom field. This example uses
58-80.Add the custom field to the project. See Add a New ProjectCustomField.
curl -X POST 'https://example.youtrack.cloud/api/admin/projects/0-0/customFields?fields=id,field(id,name),bundle(id,name,values(id,name),$type),canBeEmpty,isPublic' \ -H 'Accept: application/json' \ -H 'Authorization: Bearer <YouTrack_token>' \ -H 'Content-Type: application/json' \ -d '{ "field": {"id": "58-80"}, "bundle": {"id": "86-10", "$type": "EnumBundle"}, "canBeEmpty": true, "isPublic": true, "$type": "EnumProjectCustomField" }'If the request is successful, the response contains the project custom field.
{ "field": { "name": "Customer Impact", "id": "58-80", "$type": "CustomField" }, "bundle": { "values": [ { "name": "Low", "id": "67-12", "$type": "EnumBundleElement" }, { "name": "Medium", "id": "67-13", "$type": "EnumBundleElement" }, { "name": "High", "id": "67-14", "$type": "EnumBundleElement" } ], "name": "Customer Impact", "id": "86-10", "$type": "EnumBundle" }, "canBeEmpty": true, "isPublic": true, "id": "105-80", "$type": "EnumProjectCustomField" }
As a result, the enum custom field is available in the target project with the values from the enum bundle.