---
schema_version: "1"
id: 057e925091b307cc1fe1ee64e3ca565cd72c50fc5ae288d6eff82a9981998390
title: Assign Users to Programs with a Schedulable Apex Class
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/sales-cloud/enablement/guides/enablement-assign-automate-apex.md
canonical_url: https://developer.salesforce.com/docs/sales/enablement/guide/enablement-assign-automate-apex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 14c2f093fe68d755b4bc88b90bf31799b0696cb02a65b56bfcf31ddd954c7f35
products:
  - Sales Programs and Partner Tracks with Enablement
  - Sales Cloud
---
# Assign Users to Programs with a Schedulable Apex Class

Create a schedulable Apex class that bulk-assigns users to Enablement programs.

| User Permissions Needed         |                                                                                       |
| ------------------------------- | ------------------------------------------------------------------------------------- |
| To create Apex classes:         | Author Apex                                                                           |
| To assign or unassign programs: | Design and Deliver Enablement Programs OR Design and Deliver Enablement Lite Programs |

**note:** You can’t add Apex classes directly in production orgs. Only Developer Edition, trial, and sandbox orgs support direct Apex editing in Setup. To deploy Apex code in a production org, add it to a sandbox first, test it, and then promote it to production.

The Apex class calls the [assignEnablementProgram](https://developer.salesforce.com/docs/atlas.en-us.api_action.meta/api_action/actions_obj_assign_enablement_program.htm) invocable action.

1. From the Setup menu, select **Developer Console**.
2. From the File menu, select **New | Apex Class**.
3. Enter a name for the Apex Class.
4. Click **OK**.
5. In the Developer Console, replace the code that you see with this Apex class, then save your work.

   ```apex
   global class ScheduleAssignments implements Schedulable {
    global void execute(SchedulableContext SC) {
      String enablementAdminToNotify = [SELECT AssigneeId FROM PermissionSetAssignment WHERE PermissionSet.Name = 'EnablementAdmin' LIMIT 1].AssigneeId;
      String programId = [SELECT Id FROM EnablementProgram WHERE Name = 'Test Program' LIMIT 1].Id;

      List<PermissionSetAssignment> allAssignees = [SELECT AssigneeId FROM PermissionSetAssignment WHERE PermissionSet.Name = 'EnablementUser'];

      Invocable.Action action = null;

      for (PermissionSetAssignment assignee : allAssignees) {
          if (action == null) {
              action = Invocable.Action.createStandardAction('assignEnablementProgram');
          } else {
              action.addInvocation();
          }

          action.setInvocationParameter('assigneeId', String.valueOf(assignee.AssigneeId));
          action.setInvocationParameter('startDate', Date.today() + 1);
          action.setInvocationParameter('notificationUserId', enablementAdminToNotify);
          action.setInvocationParameter('programId', programId);
      }

      if (action != null) {
          List<Invocable.Action.Result> results = action.invoke();

          for (Invocable.Action.Result result : results) {
              if (!result.isSuccess()) {
                  for (Invocable.Action.Error error : result.getErrors()) {
                      System.debug('Error: ' + error.getMessage());
                  }
              }
          }
      }
    }
   }
   ```

6. To implement this schedulable class, execute this Apex code from Developer Console.

   ```apex
   ScheduleAssignments assignerJob = new ScheduleAssignments();
   // Schedules for 08:30:20 AM on Feb 10th
   String scheduledTime = '20 30 8 10 2 ?';
   String jobID = System.schedule('Enablement Assign Job', scheduledTime, assignerJob);
   ```

The `assignEnablementProgram` invocable action counts against your org’s [Bulk API 2.0 limits](https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_bulkapi.htm). To help accommodate these limits, this example shows a bulkified request.

**See Also**

- [_Salesforce Help_: Considerations and Limitations for Enrolling Users in Enablement Programs](https://help.salesforce.com/s/articleView?id=sales.enablement_assignments_guidelines.htm&type=5)
- [_Salesforce Developers_: Apex Scheduler](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm)
- [_Trailhead_: Asynchronous Apex](https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex)
