# Manage Training with REST and Apex APIs

To manage learning module assignments programmatically, you can use REST or Apex APIs with record objects.

| Required Editions                                                         |
| ------------------------------------------------------------------------- |
| Workforce Engagement is available in Lightning Experience                 |
| Available in: **Enterprise**, **Performance**, and **Unlimited** Editions |

[Person Training](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_persontraining.htm) records represent learning assignments in Workforce Engagement. Assignments are Trailhead learning modules, which are [Learning Content](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_learningcontent.htm) records.

## REST API Examples

These REST API examples list, create, update, and delete learning assignments in PersonTraining records.

**List all learning assignments**

```nolang
/services/data/v54.0/query?q=SELECT+Id+from+PersonTraining
```

**Create a learning assignment**

```nolang
/services/data/v54.0/sobjects/PersonTraining
{
"Name": "Person Training 001",
"TrainingId": "028af5d6-7e23-3cea-66e5-fd8e3bfe7e9c",  
"TrainingType": "T"
}
```

**Update a learning assignment**

In this example, 0hRSG00000000zJ2AQ is the ID of the Person Training record to update.

```nolang
/services/data/v54.0/sobjects/PersonTraining/0hRSG00000000zJ2AQ
{
"Name": "Person Training Module 003"
}
```

**Delete a learning assignment**

In this example, 0hRSG00000000zJ2AQ is the ID of the Person Training record to delete.

```nolang
/services/data/v54.0/sobjects/PersonTraining/0hRSG00000000zJ2AQ
```

## Apex API Examples

If you use Apex classes to look up learning modules programmatically, place a limit clause on the SOQL query, for example:

```nolang
List<LearningContent> aa = [SELECT ExternalId FROM LearningContent
WHERE Title LIKE '%Accessibility%' LIMIT 5];
```

These example Apex classes create, search, update, delete, and route learning assignments.

**Create a learning assignment**

```nolang
/* InsertPersonTraining.apex */
PersonTraining Test001 = new PersonTraining();
Test001.Name = 'Test001';
Test001.TrainingId = '028af5d6-7e23-3cea-66e5-fd8e3bfe7e9c';
Test001.TrainingType = 'T';

insert Test001;
```

**Search for a learning assignment**

```nolang
/* SearchPersonTraining.apex */
PersonTraining  training = 
        [SELECT Name, ID FROM PersonTraining 
         WHERE Name='Test001'
         LIMIT 1];
```

**Update a learning assignment**

```nolang
/* UpdatePersonTraining.apex */
PersonTraining training = 
        [SELECT Name, Id FROM PersonTraining 
         WHERE Name='newTest001'
         LIMIT 1];

// Update the training Name
training.Name = 'newTest001';

update training;

// Verify that the name was updated
PersonTraining training02 = 
    [SELECT Name, Id FROM PersonTraining WHERE Id=:training.Id];
```

**Delete a learning assignment**

```nolang
PersonTraining training = 
        [SELECT Name, Id FROM PersonTraining 
         WHERE Name='newTest001'
         LIMIT 1];

delete training;
```

**Route a learning assignment**

```nolang
/* RoutePersonTraining.apex */
PendingServiceRouting routing = new PendingServiceRouting();
routing.CapacityWeight = 1;

// To route correctly, PreferredUserId must be the same as AssigneeId.
routing.preferredUserId = UserInfo.getUserId(); // Assign to current user for testing.
routing.isPreferredUserRequired = true;
routing.routingPriority = 1;
routing.routingType = 'SkillsBased';

// set service channel ID
routing.serviceChannelId = '0N9SG0000000CHJ'; // Hard-coded the service channel id
routing.isReadyForRouting = true;

// set workItemId to be person training ID
routing.workItemId = '0hRSG000000009h2AA';

insert routing;
```
