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

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.

Note

The Apex class calls the assignEnablementProgram 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.

    1global class ScheduleAssignments implements Schedulable {
    2 global void execute(SchedulableContext SC) {
    3   String enablementAdminToNotify = [SELECT AssigneeId FROM PermissionSetAssignment WHERE PermissionSet.Name = 'EnablementAdmin' LIMIT 1].AssigneeId;
    4   String programId = [SELECT Id FROM EnablementProgram WHERE Name = 'Test Program' LIMIT 1].Id;
    5
    6   List<PermissionSetAssignment> allAssignees = [SELECT AssigneeId FROM PermissionSetAssignment WHERE PermissionSet.Name = 'EnablementUser'];
    7
    8   Invocable.Action action = null;
    9
    10   for (PermissionSetAssignment assignee : allAssignees) {
    11       if (action == null) {
    12           action = Invocable.Action.createStandardAction('assignEnablementProgram');
    13       } else {
    14           action.addInvocation();
    15       }
    16
    17       action.setInvocationParameter('assigneeId', String.valueOf(assignee.AssigneeId));
    18       action.setInvocationParameter('startDate', Date.today() + 1);
    19       action.setInvocationParameter('notificationUserId', enablementAdminToNotify);
    20       action.setInvocationParameter('programId', programId);
    21   }
    22
    23   if (action != null) {
    24       List<Invocable.Action.Result> results = action.invoke();
    25
    26       for (Invocable.Action.Result result : results) {
    27           if (!result.isSuccess()) {
    28               for (Invocable.Action.Error error : result.getErrors()) {
    29                   System.debug('Error: ' + error.getMessage());
    30               }
    31           }
    32       }
    33   }
    34 }
    35}
  6. To implement this schedulable class, execute this Apex code from Developer Console.

    1ScheduleAssignments assignerJob = new ScheduleAssignments();
    2// Schedules for 08:30:20 AM on Feb 10th
    3String scheduledTime = '20 30 8 10 2 ?';
    4String jobID = System.schedule('Enablement Assign Job', scheduledTime, assignerJob);

The assignEnablementProgram invocable action counts against your org’s Bulk API 2.0 limits. To help accommodate these limits, this example shows a bulkified request.

See Also