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 invocable action.
-
From the Setup menu, select Developer Console.
-
From the File menu, select New | Apex Class.
-
Enter a name for the Apex Class.
-
Click OK.
-
In the Developer Console, replace the code that you see with this Apex class, then save your work.
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()); } } } } } } -
To implement this schedulable class, execute this Apex code from Developer Console.
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. To help accommodate these limits, this example shows a bulkified request.
See Also