You are here:
Update Your Shift Work Topics for Capacity-Based Scheduling
Prepare your org for capacity-based scheduling by updating the shift work topics.
Required Editions
| Available in: Lightning Experience. |
| Available in: Enterprise and Unlimited Editions |
| User Permissions Needed | |
|---|---|
| Run the script: | API Enabled AND View All Data AND Author Apex |
Check your org for any shift work topics that have All Topics Supported check box enabled with these scripts. For such shift work topics, the script deletes them and then creates individual shift work topics for all the work types or work type groups.
-
Depending on the Scheduler for Health Cloud setting in your org, copy one of these
scripts.
- Disabled
global class BatchProcessShiftWorkTerritory implements Database.Batchable<SObject> { global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator( [SELECT Id FROM Shift ] ); } global void execute(Database.BatchableContext BC, List<SObject> scope) { List<String> shiftIds = new List<String>(); for (Shift shift : (List<Shift>) scope) { shiftIds.add(shift.Id); } List<ShiftWorkTopic> shiftWorkTopicsWithAllTopicsSupported = [ SELECT Id, ShiftId, WorkTypeGroupId FROM ShiftWorkTopic WHERE AreAllTopicsSupported = TRUE AND ShiftId IN :shiftIds ]; List<ShiftWorkTopic> shiftWorkTopicsWithAllTopicsNotSupported = [ SELECT WorkTypeGroupId, ShiftId FROM ShiftWorkTopic WHERE AreAllTopicsSupported = FALSE AND ShiftId IN :shiftIds ]; List<ShiftWorkTopic> newShiftWorkTopics = new List<ShiftWorkTopic>(); List<WorkTypeGroup> allWorkTypeGroups = [ SELECT Id FROM WorkTypeGroup ]; Set<Set<String>> allCombinationsOfWorkTypeGroupAndShift = new Set<Set<String>>(); for (ShiftWorkTopic swt : shiftWorkTopicsWithAllTopicsNotSupported) { allCombinationsOfWorkTypeGroupAndShift.add( new Set<String>{ swt.WorkTypeGroupId, swt.ShiftId } ); } // Loop through enabled ShiftWorkTopics and all WorkTypeGroups for ( ShiftWorkTopic enabledTopic : shiftWorkTopicsWithAllTopicsSupported ) { for (WorkTypeGroup workTypeGroup : allWorkTypeGroups) { if ( !allCombinationsOfWorkTypeGroupAndShift.contains( new Set<String>{ workTypeGroup.Id, enabledTopic.ShiftId } ) ) { newShiftWorkTopics.add( new ShiftWorkTopic( WorkTypeGroupId = workTypeGroup.Id, ShiftId = enabledTopic.ShiftId ) ); } } } // Insert the new ShiftWorkTopic records insert newShiftWorkTopics; // Delete the enabled ShiftWorkTopics delete shiftWorkTopicsWithAllTopicsSupported; } global void finish(Database.BatchableContext BC) { // Send notification or log the completion System.debug('Batch job completed successfully.'); } } - Enabled
global class BatchProcessShiftWorkTerritory implements Database.Batchable<SObject> { global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator( [SELECT Id FROM Shift ] ); } global void execute(Database.BatchableContext BC, List<SObject> scope) { List<String> shiftIds = new List<String>(); for (Shift shift : (List<Shift>) scope) { shiftIds.add(shift.Id); } List<ShiftWorkTopic> shiftWorkTopicsWithAllTopicsSupported = [ SELECT Id, ShiftId, WorkTypeId FROM ShiftWorkTopic WHERE AreAllTopicsSupported = TRUE AND ShiftId IN :shiftIds ]; List<ShiftWorkTopic> shiftWorkTopicsWithAllTopicsNotSupported = [ SELECT WorkTypeId, ShiftId FROM ShiftWorkTopic WHERE AreAllTopicsSupported = FALSE AND ShiftId IN :shiftIds ]; List<ShiftWorkTopic> newShiftWorkTopics = new List<ShiftWorkTopic>(); List<WorkType> allWorkTypes = [ SELECT Id FROM WorkType ]; Set<Set<String>> allCombinationsOfWorkTypeAndShift = new Set<Set<String>>(); for (ShiftWorkTopic swt : shiftWorkTopicsWithAllTopicsNotSupported) { allCombinationsOfWorkTypeAndShift.add( new Set<String>{ swt.WorkTypeId, swt.ShiftId } ); } // Loop through enabled ShiftWorkTopics and all WorkTypes for ( ShiftWorkTopic enabledTopic : shiftWorkTopicsWithAllTopicsSupported ) { for (WorkType workType : allWorkTypes) { if ( !allCombinationsOfWorkTypeAndShift.contains( new Set<String>{ workType.Id, enabledTopic.ShiftId } ) ) { newShiftWorkTopics.add( new ShiftWorkTopic( WorkTypeId = workType.Id, ShiftId = enabledTopic.ShiftId ) ); } } } insert newShiftWorkTopics; delete shiftWorkTopicsWithAllTopicsSupported; } global void finish(Database.BatchableContext BC) { // Send notification or log the completion System.debug('Batch job completed successfully.'); } }
- Disabled
- Create an Apex Class called BatchProcessShiftWorkTerritory to trigger approval workflows. See Define Apex Classes.
- From Setup, select Developer Console.
- In the Developer Console, click Debug, then click Open Execute Anonymous Window.
-
Execute this code.
BatchProcessShiftWorkTerritory batchJob = new BatchProcessShiftWorkTerritory(); Integer batchSize = 100; Database.executeBatch(batchJob, batchSize); - To view the progress of the script, in Setup, find and select Apex Jobs.
Did this article solve your issue?
Let us know so we can improve!
