# Queue generation with coded workflows and Orchestrator APIs

> This tutorial shows you how to build an automation that generates random queues using the Orchestrator APIs, accessed from Swagger. The automation creates a new queue, generates queue items with random data, and adds them to the queue.

This tutorial shows you how to build an automation that generates random queues using the Orchestrator APIs, accessed from Swagger. The automation creates a new queue, generates queue items with random data, and adds them to the queue.

## Prerequisites
* For this example, you need to use a **Library** or a **Test Automation**. But you can use coded workflows for any type of RPA process.
* [System.Activities](https://docs.uipath.com/activities/other/latest/workflow/system-apis) 23.10
* [Testing.Activities](https://docs.uipath.com/activities/other/latest/workflow/testing-apis) 23.10
* Add a new service (Orchestrator) in the **Services** section of your Studio project.
  + Add the Orchestrator API Swagger definition of the instance that you want to use under **File or Link,** then click **Load**. To get the swagger definition link, visit [API References](https://docs.uipath.com/orchestrator/automation-cloud/latest/api-guide/api-references).
  + Deselect all endpoints, except **QueueDefinitions**, then click **Save**.

1. Create a Coded workflow by selecting **New**, and then **Coded Workflow** from the **File** group.
2. Inside the **Execute** method, create an instance of the **HttpClient** object, by calling the **`BuildClient(String, Boolean)`** method. This method builds and HTTP Client with a specified scope.

The method takes two parameters, that have the following default values:
   1. `scope "Orchestrator"` - The OAuth 2.0 scope for which to get an access token.
   2. `force True` - Generates a new access token.
   ```
   var client = BuildClient();
   ```
3. Create an instance of the **QueueDefinitionsClient**, pass the client instance as a parameter, and assign it to a variable named **queueClient**. This client variable is used to interact with the Orchestrator's **Queue Definitions** APIs.
   ```
   var queueClient = new QueueDefinitionsClient(client);
   ```

![docs image](https://dev-assets.cms.uipath.com/assets/images/studio/studio-docs-image-268760-781cea0f.webp)

4. Generate a queue name and create a new queue.
   ```
   var queueName = "SampleQueue" + Guid.NewGuid().ToString("N");
   var queue = queueClient.PostAsync(new QueueDefinitionDto() { Name = queueName }, null).Result;
   ```

![docs image](https://dev-assets.cms.uipath.com/assets/images/studio/studio-docs-image-271434-e0a384cf.webp)

5. Create a **Parallel For Each** loop to iterate over a range of numbers between 0 and 100. Use a dictionary to store the values for Address, FirstName, and LastName. Generate random values for these items, using the [Address](https://docs.uipath.com/activities/other/latest/user-guide/address), [GivenName](https://docs.uipath.com/activities/other/latest/user-guide/given-name), and [LastName](https://docs.uipath.com/activities/other/latest/user-guide/last-name) coded automation APIs.

In this scenario, a **Parallel For Each** is used instead of a simple **For Each**, with the purpose of improving the performance of the automation.

   ```
   Parallel.ForEach(Enumerable.Range(0, 100), i =>
         {
           var data = new Dictionary<string, object>()
           {
             { "Address", testing.Address("Romania", "Bucharest")["City"].ToString() },
             { "FirstName", testing.GivenName() },
             { "LastName", testing.LastName() }
           };
   ```

![docs image](https://dev-assets.cms.uipath.com/assets/images/studio/studio-docs-image-271438-7ae1250b.webp)

6. Add the random data to a queue, using the [AddQueueItem](https://docs.uipath.com/activities/other/latest/user-guide/add-queue-item) coded automation API.
   ```
   system.AddQueueItem(queueName, null, DateTime.UtcNow, data, DateTime.UtcNow, QueueItemPriority.Normal, i.ToString(), 100);
   ```

## Sample project

To follow the steps, download the following sample project: [Queue generation with coded workflows and Orchestrator APIs](https://documentationexamplerepo.blob.core.windows.net/examples/CodedAutomations/QueueGenerationWithCodedWorkflowsAndOrchestratorApis.zip).
