Posts

From Mrityunjoy Chowdhury: Clone Recurring Shift along with Shift Recurrence Pattern:

Preface

In Spring ’22 Salesforce Release – Scheduler Product introduced the Manage Service Resource Availability by Using Shifts feature which gave flexibility for service resources to work flexible hours instead of the Service Territory Operating Hours. This feature was extended in Winter ’23 with the flexibility to create Recurring Shifts – which gave an easy way for
Territory Managers and Service Resources to create multiple shifts with a single record data entry.

This was made possible by capturing the recurring pattern of the shift during data entry.

And Salesforce saves this recurring shift as a single record with Type field as “Recurring”.

Salesforce Scheduler saves the recurrence pattern when the original recurrence shift record is saved. The information is saved in “RecurrencePattern” field on the same Shift record. An example pattern as saved on the record

RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;COUNT=4;

This information corelates to what gets saved as shown in the screenshot – Weekly recurring Event which occurs every Monday, Wednesday and Friday for the next 4 occurrences and repeats every week.

NOTE: Salesforce Scheduler considers the recurrence period in the service territory’s timezone. The period of recurrence is limited to 120 days. For example, you create a recurring shift on July 1, 2022. You can choose an end date that’s either less than or equal to 120 days from July 1, 2022, or the number of occurrences that are repeated within 120 days from July 1, 2022.

Cloning Shift Records

Scheduler product also gave the option to clone shift records. When a user tries to clone a Shift record with the Type field value set to Recurring, the new record creation screen that opens, Salesforce prepopulates the Start and End Times of the Shift, Status, Service Territory, Service Resource and the Time Slot Type fields. You will notice, it does NOT prepopulate the recurring Pattern (at this point of time).

This is a limitation in the current scheduler product, until this feature gets included in future (Roadmap).

In this blog we are going to look at an option to clone the Shift along with its recurring Pattern using Apex

Cloning Shift Records with Recurring Pattern

We want to keep it simple here in this blog. This is just a quick and dirty work around, but feel free to take it to the next level. As you have noticed when using the Clone button on the Shift (with type as recurring) – the new shift screen that opens has all the information from the previous record copied, except for the Recurrence pattern.

The copying of recurrence pattern is handled in the trigger which will copy it from the Original Shift where we are cloning the record from.

Use the sample trigger code below on the Shift Object to copy the recurrence pattern

trigger CloningRecurringShift on Shift (Before insert) {
for(Shift sh : Trigger.New){
if(sh.Type =='Recurring' && sh.IsClone()==true && sh.RecurrencePattern=='RRULE:INTERVAL=1;'){
String sourceId = sh.getCloneSourceId();
Shift orginalShift = [Select RecurrencePattern from shift where ID=:sh.getCloneSourceId()];
System.debug('Orginal Shift recurring patternt ' + orginalShift.RecurrencePattern);
sh.put('RecurrencePattern',orginalShift.RecurrencePattern);

}
}
}

Note: If updating the other fields (not the recurrence type section) that were copied over when you tried to Clone, the saved record will consider the new updated values.

Demo recording

Coming soon

References
https://help.salesforce.com/s/articleView?id=sf.ls_create_shifts.htm&type=5