Posts

From Shubham Singh Rawat & Mrityunjoy Chowdhury: Reverse Map Service Appointment to the appropriate shift

Shift is one of the major additions made in Salesforce Scheduler, It provides the user with enormous flexibility and makes the entire process of appointment scheduling less cumbersome. The diagram below shows the interaction between Shift and various other entities involved in scheduling a Service Appointment.

Blank diagram2.jpg

A Service Territory Member can have multiple Shifts associated with it and as a result the time slot selected for the Service Appointment can fall between more than one overlapping shifts (considering both the required and primary Service Resources).

In the following sections we will figure out a way to reverse map all the associated Shifts with corresponding Service Appointment.

Creating a custom object

For completing the purpose of reverse mapping Shift with Service Appointment a custom object(Service appointment shift) is used as a junction object. It comprises of the following custom fields:-

  • ServiceAppointnmentId – Referring to a Service Appointment.
  • ShiftId – Referring to a Shift whose startTime and endTime completely encloses the Service Appointment’s time slot.
  • Modified – A boolean flag to help backtrack all the updates made to Service Appointment.Only the records with the flag value false should be considered as reliable entries.
  • Canceled – A boolean flag which is set to true only when the associated Service Appointment is canceled.
Blank diagram.jpeg

As evident from the design proposed whenever a Service Appointment is scheduled we need to obtain all the Shifts associated with the Service Appointment.For each associated Shift, one record comprising of ServiceAppointmentId,ShiftId

and Modified(initially false) must be made.

For achieving our goal of reverse mapping we will be writing triggers on two entities:

  • Service Appointment
  • Assigned Resource

Creating a Service Appointment

We must do the reverse mapping the moment a Service Appointment is created.Whenever a Service Appointment is created necessary inserts are made in Assigned Resource and this will invoke the trigger that we will be writing for Assigned Resource .

Pseudocode

Let us first discuss the pseudocode for triggers when new Service Appointments are created.

  • Create a trigger which runs after inserts are made in Assigned Resource.
  • Fetch all the linked Service Appointments using the just inserted Assigned Resource records.
  • Check for any previous entries for obtained Service Appointments in the custom object
    and for all those records set the Modified as true.
  • Design a map of Service Appointment and all the required resources for that appointment.
  • Loop through all the Service Appointments in the map and obtain the associated Shifts of the Service Resource.
  • Do the inserts for Service Appointment and Shift mapping records.

Updating a Service Appointment

Whenever a Service Appointment is modified we can make changes in the time slot and the required resources of the Service Appointment.Once a Service Appointment is updated we must set the Modified field for all the records of this Service Appointment in the custom object to true and obtain new associated Shifts. Let’s have a look at the possible scenarios and the way we are handling it.

  • Deletion of required SR from Appointment(will invoke an after delete trigger on Assigned Resource)
  • Addition of required SR to Appointment(will invoke an after insert trigger on Assigned Resource)
  • Moving of SR from required to optional resource(will invoke an after update trigger on Assigned Resource)
  • Moving of SR from optional to required resource(will invoke an after update trigger on Assigned Resource)
  • Change in Appointment start time and/or end time
  • Change in Appointment WTG/WT (Appointment duration)
  • Changing the time slot at the same time altering the Assigned Resources.

For these cases we will be using one update and a delete trigger on Assigned Resource wherein we implement a similar logic as mentioned above.

Disclaimer: The following code is meant to be verbose and easily understandable from a Salesforce Developer perspective. Given a choice between performance vs readability I have strived for the latter. It is a proof of concept to demonstrate the feature and should be modified and tested thoroughly as per different data shapes and existing code in the org.

trigger getassociatedshifts on AssignedResource (after insert,after update,after delete) {

   /* fetching all the linked service appointments from the trigger */

   List<ServiceAppointment> AllAppointments = new List<ServiceAppointment>();

   if(Trigger.isDelete) {

       List<String> AffectedAppointments = new List<String>();

       For(AssignedResource a:Trigger.old) {

           AffectedAppointments.add(a.ServiceAppointmentId); 

       }

       AllAppointments = [Select Id,ServiceTerritoryId,SchedStartTime,SchedEndTime,WorkTypeId from ServiceAppointment where Id In :AffectedAppointments];

   } else {

       AllAppointments = [Select Id,ServiceTerritoryId,SchedStartTime,SchedEndTime,WorkTypeId from ServiceAppointment where Id In (Select ServiceAppointmentId from AssignedResource where Id In :Trigger.New)];

   }

   /*

     check for any previous entries for obtained service appointments in the custom object

     and for all those records set the modified flag as true

  */

   List<Service_appointment_shift__c> ServiceAppointmentShiftToUpdate =new List<Service_appointment_shift__c>();

   For(Service_appointment_shift__c all:[Select id,modified__c from Service_appointment_shift__c where Service_Appointment__c In :AllAppointments]) {

       all.Modified__c = true;

       ServiceAppointmentShiftToUpdate.add(all);

   }

   update ServiceAppointmentShiftToUpdate;

   /* Making call to helper class for necessary inserts */

   ServiceAppointmentShiftMapping.InsertNewRecords(AllAppointments);

}

Helper class

public class ServiceAppointmentShiftMapping {

   public static void InsertNewRecords(List<ServiceAppointment> AllAppointments) {

       /*creating a map of ServiceAppointmentId and List<ServiceResourceId> */

       Map<Id,List<Id> > ServiceAppointmentToAllServiceResources = new Map<Id,List<Id> >();

       /*for creating the map we need list of required resource and service appointment */

       List<AssignedResource> ServiceResourcetoServiceAppointment =new List<AssignedResource>();

       ServiceResourceToServiceAppointment = [Select ServiceResourceId,ServiceAppointmentId from AssignedResource where ServiceAppointmentId in:AllAppointments

                AND  IsRequiredResource = True];

       for(AssignedResource temp:ServiceResourceToServiceAppointment) {

           /* check if the key already exists or not*/

           if(ServiceAppointmentToAllServiceResources.containsKey(temp.ServiceAppointmentId)) {

               List<Id> ResourceList = ServiceAppointmentToAllServiceResources.get(temp.ServiceAppointmentId);

               ResourceList.add(temp.ServiceResourceId);

               /*insert the key value pair*/

               ServiceAppointmentToAllServiceResources.put(temp.ServiceAppointmentId,ResourceList);

           } else {

               ServiceAppointmentToAllServiceResources.put(temp.ServiceAppointmentId,new List<Id>{temp.ServiceResourceId});

           }

       }

       /*All the new records for custom object will be stored in this*/ 

       List<Service_appointment_shift__c> AllNewEntries =new List<Service_appointment_shift__c>(); 

       For(ServiceAppointment current:AllAppointments) {

           List<Id> ResourceId = new List<Id>();

           /*obtain the list of resources from the map */

           ResourceId = ServiceAppointmentToAllServiceResources.get(current.Id);

           //fetch associated shits with wtg and without wtg

           List<Shift> probableShifts= new List<Shift>();

           probableShifts =[Select Id from Shift where ServiceResourceId IN :ResourceId

                            AND ServiceTerritoryId = :current.ServiceTerritoryId

                            AND StartTime <= :current.SchedStartTime

                            AND EndTime >= :current.SchedEndTime

                            AND Status =’Confirmed’

                            AND WorkTypeGroupId IN (Select WorkTypeGroupId from WorkTypeGroupMember where WorkTypeId =:current.WorkTypeId)

                          ];

           List<Shift> probableShiftsNoWtg= new List<Shift>();

           probableShiftsNoWtg =[Select Id from Shift where ServiceResourceId IN :ResourceId

                                 AND ServiceTerritoryId = :current.ServiceTerritoryId

                                 AND StartTime <= :current.SchedStartTime

                                 AND EndTime >= :current.SchedEndTime

                                 AND Status =’Confirmed’

                                 AND WorkTypeGroupId =”

                            ]; 

           /* add all the records to be inserted in the custom object in a list*/

           for(Shift a:probableShifts) {

               Service_appointment_shift__c next = new Service_appointment_shift__c(Service_Appointment__c = current.Id,Shift__c = a.Id,Modified__c = false);

               AllNewEntries.add(next);

           }

           for(Shift a:probableShiftsNoWtg) {

               Service_appointment_shift__c next = new Service_appointment_shift__c(Service_Appointment__c = current.Id,Shift__c = a.Id,Modified__c = false);

               AllNewEntries.add(next);

           }

        }

        Insert AllNewEntries;

   }

}

  • Service Appointment is canceled

trigger cancelServiceAppointment on ServiceAppointment (after update)

{

   /*

     This trigger will be called whenever a service appointment is update(time slot is

     changed,assigned resources are changed,status is changed etc)

     but the only purpose of this trigger is to handle the case when the service appointment

     is canceled

   */

   /*

      All the Service Appointments that are canceled.

   */

   List<ServiceAppointment> changed = new List<ServiceAppointment>();

   For(Integer i=0;i<Trigger.New.size();++i) {

       ServiceAppointment justUpdated = Trigger.New[i];

       /*

          checking the status of updated Service Appointment

       */

       if(justUpdated.Status == ‘Canceled’) {

           changed.add(justUpdated);

       }

   }

   /*

       Setting the canceled flag and modified flag of the obtained service Appointment in custom object

   */

   List<Service_appointment_shift__c> ServiceAppointmentShiftToUpdate =new List<Service_appointment_shift__c>();

   For(Service_appointment_shift__c all:[Select id,modified__c from Service_appointment_shift__c where Service_Appointment__c In :changed]) {

       all.Canceled__c =true;

       all.Modified__c = true;

       ServiceAppointmentShiftToUpdate.add(all);

   }

   /* updating the records */

   update ServiceAppointmentShiftToUpdate;

}

From Apeksh Dave: Create Zoom Meetings from Salesforce (with Scheduler)

Introduction

A customer of Salesforce is looking to integrate with Zoom and the Online Meeting capabilities from Salesforce using Portal / Internal screens. The use case is fairly straightforward, as a customer of a bank, I want to schedule an online meeting with my advisor and discuss my accounts. The technology the bank wants to use is Zoom.

The technology usage behind this particular demo is to simply create a meeting in Zoom via an API which returns a URL that parties can click on to launch Zoom( either browser or desktop ) and have a meeting. There is a lot more that can be done, but this integration is to show the possibilities with the connection

Technology Utilized

Listened below are the main technologies and configurations used in the demo preparation.

Main SaaS Tech Stacks

Demo Setup Technology

  • Zoom Development Account
  • Salesforce Org
  • Postman – To easily test the setup and API’s
  • <optional> Visual Code – Used to write and deploy code
  • Apex Classes
    • Invocable Methods setup to make available in Flows
    • Test Classes
  • Auth Provider Setup in Salesforce
  • Named Credentials
  • Salesforce Flows – To provide the screen navigation, call Zoom to get the meeting, and update the appropriate records.

Demo Flow High Level

This section describes the general flow of the demo from start to finish when showing to the customer. This is a specific flow for the customer this demo was prepared for. You can tailor or make your own flow(s) for your customer.

  • Customer goes to the portal to schedule a meeting, using the Salesforce Scheduler.
  • This demo has an authenticated flow, there is a person account associated with the demo.
  • Enter the required information from the screen
    • Click by previously scheduled service appointments or search for a banker.
    • Click Next
    • Click on a Work Type Group → Select the type => Click Next
    • Select Video Call
    • Enter a address → San Francisco → Select a branch
    • Choose a date in scheduler
    • The call to Zoom is done at this point and a URL is returned to the “Additional Information” Section. Note, this could be put anywhere but for the purpose of the meeting we chose here.
    • Click on next and a new Service Appointment is created with the Meeting URL in a custom field that can be accessed to launch a Zoom meeting.

The integration behind the scene created a Zoom meeting valid link that can be clicked on to start a Zoom meeting. This utilized the Zoom API, OAuth/Open ID authentication/authorization, and Apex Callout to create and return the meeting.

Demo Details

This section will describe the setup needed to execute the demo. What is described is the particular flow used to validate and test the integration. The entire section for Postman is completely optional, but it is suggested to do this to validate and debug. In our case, it was invaluable in determining the exact headers to place into the API. In the initial development, Postman was first used outside of Salesforce to ensure the API format was correct and the authentication/auth was setup correctly in Zoom.

Zoom Setup

The first piece that is needed is a Zoom Developer Account.  Please note you cannot use a Salesforce Zoom account as they have disabled permission to use the API.  Best bet create Personal Zoom account with personal ( no salesforce account) or working with your client account that allows to create meeting via API

image.png
  • Click on Create under OAuth.
image.png
  • Name it, choose an account level app and toggle off the publish button like this.
image.png
  • Next screen
    • Screen will show the client key, client secret. Copy & store in a secure place. You will need these in the Salesforce setup.
    • Populate the redirect url and add the allow list.
image.png
  • Enter developer name.
  • Scope is meeting:write:admin
  • Press install.

Setup Salesforce/Postman Application in Zoom

Salesforce Setup

This section describes the Salesforce Setup needed to access Zoom API meetings. It is strongly advised to setup Postman first to validate and test the API as well as get familiar with the API before jumping into Apex coding setup. This section will consist of Authentication setup, the Named Credential, and the Apex code needed to create a Zoom meeting and retrieve a URL. Additionally, this will provide the callback referenced in the Zoom section which will be needed to complete the Authentication section in Zoom.

Auth. Provider Setup

The first step is to create the Authorization Provider configuration to connect to Zoom. You will need the Secret Key(Value) from the Certificate setup in Zoom and the application id.

  1. Enter Salesforce Setup → Auth Providers → New.
  2. Choose Open ID Connect from the Provider Type drop-down.
  3. Name the Auth Provider.
  4. URL Suffix → Can make the same as the name.
  5. Consumer Key → This is the client key in Zoom.
  6. Consumer Secret → This is the client secret in Zoom.
image.png
  1. Authorization Endpoint URL – https://zoom.us/oauth/authorize see screen above.
  2. Token Endpoint URL – https://zoom.us/oauth/token see screen above.
  3. Default Scopes <optional> – meeting:write:admin
  4. Make sure this option is checked: Send client credentials in header.
  5. Use the defaults for the remainder.
  6. Save.
  7. Click on the newly defined Auth Provider to open it up.
  8. Copy the Callback URL that is shown in the Salesforce Configuration. If you have Experience Cloud setup and you are using those domains, then add those callback URLs as well to the Zoom Dev configuration.
  9. Take the callback URL you copied above and paste back in the Zoom redirect url & allow list. (Use the exact same word to word, no space.)
image.png

Named Credential

Creating a named credential will perform the authentication to Zoom and do the OAuth validation. Here is where the Zoom Authenticated App is used to validate the OAuth connection. (I set mine to auto approve.) It will prompt you to login to your instance of Zoom. The login will be the Zoom Developer login/id created during the setup of the developer instance.

  1. In Setup → Named Credentials, click New.
  2. Enter the label and the URL of the API Endpoint you wish to access. In this case, the full API is used for the onLineMeeting for Delegated access. The application access has a different signature
  3. Identify Type will be Named Principal.
  4. Auth Protocol will be OAuth 2.0.
  5. Select the Auth Provider created in the previous step.
  6. Scope: very important! The scope here will match the scope you created in Zoom for the application. It is space delimited.
    • meeting:write:admin
  7. Click on Generate Authorization Header.
  8. Check on Start Auth Flow on Save.
  9. Generate Auth Header is checked.
  10. Click Save. This will trigger the actual connection to Zoom and validate the Authentication.
  11. When you Save, the OAuth flow will initiate.
image.png
  • Zoom Login Screen will popup.
  • Login.
  1. If successful, you will see Authenticated in the Authentication Status in Salesforce. If it fails, you will get a failure screen from Zoom.  Common Issues:
  • Used the wrong secret key or id. Double check the values in the Auth. The Providers are correct.
  • URL Endpoint is invalid. Check your URL Endpoint. (Using Postman first helps avoid this issue.)

Salesforce Application Code

This section will review the Apex application code needed to make the Rest API call to setup a Zoom meeting and retrieve the meeting URL from the Zoom Server. The code is not production quality but instead is a sample to prove the concepts. The current iteration does not accommodate error handling in a meaningful way nor does it do much more than create a meeting. The goal of this good is to demonstrate the basic connection for the customer and proof of concept the meeting invites. The sample code will be in two parts, the first part is a test class that can be used to validate the connection and result. Once that is working, then the 2nd class uses @InvocableMethod so that it can be used in flows. Obviously this can be tailored however desired.

This section does not instruct how to setup command line tools, deploy the source, or other development tasks. It is assumed the reader understands how to deploy, run the developer tools/debugger in Salesforce.

Test Apex Class – Simple Class to Create the Meeting utilizing Named Credentials

public class TestZoomApi {

   /* Test Method for Unit Testing Connection */

   public static String getMeetingUrl()

   {

       HttpRequest req = new HttpRequest();

       Http http = new Http();

       //Setup the Endpoint and append the name of the file

       req.setEndpoint(‘callout:SalesforceZoomPOCNC’);

       req.setMethod(‘POST’);

       req.setHeader(‘Content-Type’,’application/json’);

       req.setHeader(‘Accept’,’*/*’);

       req.setHeader(‘Accept-Encoding’,’gzip, deflate, br’);

       //Setup the JSON Body – in the test just set a subject, can add more through Postman or other tests

       req.setBody(‘{“topic”: “test create meeting”,”type”: “1”}’);       

       //req.setBody(‘{}’);

       System.debug(‘Body: ‘ + req.getBody());

       System.debug(‘Endpoint Value: ‘+ req.getEndpoint());

       System.debug(‘Request: ‘ + req);

       HTTPResponse res = http.send(req);

       System.debug(‘Response Body: ‘+res.getBody());

       /* Parse Response */

       JSONParser parser = JSON.createParser(res.getBody());

       String webLink;

       webLink = ‘ZoomNotSetup’;

       while (parser.nextToken() != null) {

       if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&

       (parser.getText() == ‘join_url’)) {

           parser.nextToken();

           webLink = parser.getText();

           System.debug(‘join_url= ‘ + webLink);

           }

       }

   return webLink;

   }

}

Common Issues/Errors

  1. Zoom will return a 201 in the HTTP Response. This is a success. It is documented in the API.
  2. HTTP Error 500 – Internal Server Error
    1. Notice the headers that are set. The Zoom documentation does not state it explicitly, but in our testing we found that the 2 accept lines need to be in the header when sending the request. You need to add the gzip, etc and the “*/*” accept lines.
    2. 401 – The 401 unauthorized generally means the scope is incorrect in the Named Credential, or in the Named Credential and/or the Zoom setup. Make sure the permissions are correct and the scope is space delimited.
    3. 403 – Forbidden – This occurs when the secret keys are incorrect. You should have fixed this when you saved the name credential so it shouldn’t show up.
  3. Note: When using the Apex Debugger – Look for the debug line to joinWebURL populated with a long string for the meeting. It will start with something like this: 12:40:52:477 USER_DEBUG [37]|DEBUG|joinWebUrl= https://us04web.zoom.us/j/xxxxxxxxxx?pwd=xxxxxxxxxxxxxxxxx

Salesforce InvocableMethod Class Utilizing the Apex Code and Callout

This is the actual code used in the demonstration. This code is accessible in the Flow Builder inside of Salesforce as an Apex Action.

global class GetZoomMeetingURLwithInput {

   @InvocableMethod(label=’Get Zoom Meeting URL with Input’ description=’Returns a meeting URL For Zoom’)

   global static List<String> makeApiCalloutwithInput(List<List<String>> inputZoomsParms)

   {

       HttpRequest req = new HttpRequest();

       Http http = new Http();

       //Setup the Endpoint and append the name of the file

       req.setEndpoint(‘callout:SalesforceZoomPOCNC’);

       req.setMethod(‘POST’);

       req.setHeader(‘Content-Type’,’application/json’);

       req.setHeader(‘Accept’,’*/*’);

       req.setHeader(‘Accept-Encoding’,’gzip, deflate, br’);

        /* Setup the Parameters for Meetings, subject, etc. */

       // Note: The initial demo only utilized the title, further development can use other inputs.

       system.debug(‘Array size  =’ + inputZoomsParms.get(0).size()); 

       String inTitle = ‘”‘ + inputZoomsParms.get(0).get(0) + ‘”‘;

       system.debug(‘inTitle =’ + inTitle);   

       String inAgenda = ‘”‘ + inputZoomsParms.get(0).get(0) + ‘”‘;

       system.debug(‘inAgenda =’ + inAgenda);              

       String inPwd = ‘”‘ + inputZoomsParms.get(0).get(1) + ‘”‘;

       system.debug(‘inPwd =’ + inPwd);               

       String inStart = ‘”‘ + inputZoomsParms.get(0).get(2) + ‘”‘;

       system.debug(‘inStart =’ + inStart);               

       String inEnd = ‘”‘ + inputZoomsParms.get(0).get(3) + ‘”‘;

       system.debug(‘inEnd =’ + inEnd);

       //Setup the JSON Body – in the test just set a subject, can add more through Postman or other tests

       // req.setBody(‘{“topic”:’ + inTitle + ,”type”: “1”}’);       

       req.setBody(‘{“topic”: “test create meeting”,”type”: “1”}’);       

       //req.setBody(‘{}’);

       System.debug(‘Body: ‘ + req.getBody());

       System.debug(‘Endpoint Value: ‘+ req.getEndpoint());

       System.debug(‘Request: ‘ + req);

       HTTPResponse res = http.send(req);

       System.debug(‘Response Body: ‘+res.getBody());

       /* Parse Response */

       JSONParser parser = JSON.createParser(res.getBody());

       String webLink;

       webLink = ‘ZoomNotSetup’;

       while (parser.nextToken() != null) {

       if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&

       (parser.getText() == ‘join_url’)) {

           parser.nextToken();

           webLink = parser.getText();

           System.debug(‘join_url= ‘ + webLink);

           }

       }

   // Apex Actions Return. The method signature for an invocable method requires a List of Strings to be returned.

   return new List<String>{webLink};

   }

}

Salesforce User Experience Setup

This section is optional but describes how the above code can be accessed in the low-code building tools inside of Salesforce.

This section in a flow shows how this action is called to retrieve a URL for the Zoom Meeting and then assigns it to a Record (such as the Service Appointment, and/ or its associated Event) in Salesforce. A larger flow will then do further processing as part of a full scheduling flow. The takeaway is that the Apex Code above is an option to drag and drop into the low code builder and use it when running a flow. It could also be embedded within a Lightning Web Component(LWC) and used in other places. Alternatively, it can be accessed from another Apex Class to get the required information.

image.png

Postman Setup

This section describes how to setup Postman to access Zoom APIs and test the API integration outside of Salesforce in a developer-centric manner. This is completely optional, but oftentimes if it works in Postman and not in another application or Salesforce, you can see what is different in Postman versus the other applications. This tutorial assumes the reader is familiar with Postman and has downloaded the application or is using the web version. This document will use the desktop installation for reference. This section does not require knowledge of the Salesforce Setup. We recommend starting with Postman before the setup in Salesforce.

Postman Collection – use version 2 link below. (Do NOT use v1.)

Download the Zoom Postman Collection

To make life easier, download the Postman Collection already created for Zoom as plenty of examples. To do so:

Setup Postman Authentication

Setting up Authentication is straightforward and will require the client id and secret key from the Zoom setup done previously. Additionally, you will need to add the callback URL to the Authentication section in Zoom. You also need to setup Environment variables as part of this step.

Setup Zoom Dev account with Postman OAuth App

image.png
  • Click on Create under OAuth.
image.png
  • Name it, choose an account level app and toggle off the publish button like this.
image.png
  1. ClientID – will be the Application ID of the App you created in the Zoom dev account.
  2. Client Secret – will be the Client Secret in Zoom dev account.
  3. Make sure the Redirect url and Allow List are exactly this https://app.getpostman.com/oauth2/callback
image.png
  1. Enter developer name.
  2. Scope is
    • meeting:write:admin
  3. Press install.

Setup Postman authentication

  • Setup Zoom environment like this, and:
  • Edit api_key with Client ID from Zoom dev account.
  • Edit api_secret with client secret from Zoom dev account.
image.png
  • Go to Zoom Api. Right click. Edit, and:
    • Edit setup variable with baseurl.
image.png
  • Edit Authorization with appropriate info as shown below.
    • Make sure the callback url is exactly the same in the Zoom Dev account.
image.png
image.png
  • Get a new Access Token.
  • You will have to login in the Zoom account.

Get/Create and Run the Zoom Online Meeting API

image.png

{
“topic”: “test create5 meeting”,
“type”: “2”,
“start_time”: “2022-02-15T10:00:00”,
“duration”: “30”,
“registrants_email_notification”: “true”
}

  • Authentication – inherited from the parent.
  • Set this:
image.png

From Ankit Srivastava: Report available Appointment Slots by Resource

Salesforce Scheduler gives tools needed to simplify appointment scheduling in Salesforce. We can create a personalized experience by scheduling customer appointments in person, or by phone or video; with the right person at the right place and time. One of the strong feature of the product is its ability to manage availability of multiple Service Resources for various Work Types across multiple Service Territories.

Now the biggest question is if there is an out of the box Salesforce Report that can provide a list of service appointment slots available for all or a specific set of service resources. The Answer is NO.

So, how do we solve for it?

Salesforce Scheduler calculates the availability of a service resource at runtime on demand (when a user is scheduling an appointment). Salesforce Scheduler uses multiple entities along with data from external systems to calculate availability data. This information is NOT stored anywhere to report on.

To solve for the reporting Question – we need some complex level of customization. This can be achieved using the power of the Salesforce Scheduler and Salesforce platform to generate reports on Service Resource’s availability information.

Approach we will use in this blog post

Salesforce Scheduler provides the LxScheduler namespace which offers the apex method getAppointmentCandidates which provides the availability information. We will use this method to

  • retrieve the information using Apex
  • dump the retrieved information into a custom object that we will create and
  • report on top of the custom object

Create Custom Object – Appointment Slots

Let us create a custom object, “Appointment Slots” with these custom fields to store availability information

  • Service Resource (lookup)
  • Service Territory (lookup)
  • Work Type Group (lookup)
  • Start Date (Date/Time)
  • End Date (Date/Time)
  • Remaining Appointments (Integer, default value = 1)

Enable reporting for this custom object.

Query and Dump retrieved information into the Custom Object

We can use following batch apex to populate availability information in our custom object.
P.S. Apex governor limits will apply on this code (https://developer.salesforce.com/docs/atlas.en-us.234.0.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm).

Slot.apex

class Slot {
Datetime startTime;
Datetime endTime;
List<String> resources;
String territoryId;
}

ReportDump.apex

public without sharing class ReportDump implements Database.Batchable<sObject>{
public Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id FROM ServiceResource where isActive = true';
return Database.getQueryLocator(query);
}

public void execute(Database.BatchableContext BC, List<ServiceResource> srList){
Integer DUMP_DURATION = 30;
String ACCOUNT_ID = '001xx000003GYQRAA4';
String SCHEDULING_POLICY = '0Vrxx0000004C92';
//Hardcoding WTG for now
List<String> wtgs = new List<String>();
wtgs.add('0VSxx0000004C92GAE');

List<Appointment_Slots__c> slots = new List<Appointment_Slots__c>();

for (ServiceResource sr : srList){
//Find all territories he works in
List<ServiceTerritoryMember> stms = [Select Id, ServiceTerritoryId
from ServiceTerritoryMember
where ServiceResourceId = :sr.Id];
List<String> territories = new List<String>();
for (ServiceTerritoryMember stm : stms){
territories.add(stm.ServiceTerritoryId);
}

for (String wtg : wtgs){
lxscheduler.GetAppointmentCandidatesInput input = new lxscheduler.GetAppointmentCandidatesInputBuilder().setWorkTypeGroupId(wtg).setTerritoryIds(territories).setStartTime(System.now().format('yyyy-MM-dd\'T\'HH:mm:ssZ', 'America/New_York')).setEndTime(System.now().addDays(DUMP_DURATION).format('yyyy-MM-dd\'T\'HH:mm:ssZ', 'America/New_York')).setAccountId(ACCOUNT_ID).setSchedulingPolicyId(SCHEDULING_POLICY).setApiVersion(Double.valueOf('54.0')).build();
String response = lxscheduler.SchedulerResources.getAppointmentCandidates(input);

List<Slot> objs = (List<Slot>)JSON.deserialize(response, List<Slot>.class);
for (Slot obj : objs){
Appointment_Slots__c slot = new Appointment_Slots__c();
slot.Start_Date__c = obj.startTime;
slot.End_Date__c = obj.endTime;
slot.Service_Territory__c = obj.territoryId;
slot.Service_Resource__c = sr.Id;
slot.Work_Type_Group__c = wtg;
slots.add(slot);
}
}
}
insert slots;
}

public void finish(Database.BatchableContext BC){
}

}

Create a Report using the Custom Object

Create a Salesforce report using this custom object. Refer – https://help.salesforce.com/s/articleView?id=sf.rd_reports_overview.htm&type=5 on how to create reports. Here is a sample matrix report we created which reports a Service resources total available hours at a Service location for an appointment topic / template – and this is how it looks.

Truncating records in custom Object

The example quoted above requires the Appointment Slots object to be empty in order to calculate the Service Resources availability. Ensure you truncate the data in the custom object before running the above batch job. Please refer this help article on Truncating Salesforce Obejcts https://help.salesforce.com/s/articleView?id=sf.dev_object_trunc.htm&type=5.

You can also use apex to delete all records from the object as shown at https://developer.salesforce.com/forums/?id=906F0000000BPDTIA4

From Ankit Srivastava: Restraining Shifts within the Service Territory’s Operating Hours

In the Spring ‘22 release, Scheduler introduced Shift Rostering Management which can be used to setup working hours for resources.

Shifts is a paradigm changing update to Salesforce Scheduler product. It eases pain of setting adhoc or non-recurring time slots for Service Resources to work. It also gives flexibility to work outside Service Territory’s operating hours.

One of the advantages of using Shifts is the ability to use Custom reports to report on working hours of a resource but that goes away if we allow resources to setup shifts beyond the working hours of a Territory. So here one would want to restrict the Service Resource to only create Shifts within the working hours of a Service Territory. This way, you could use create a custom report on the Shift record to understand the working hours of previous weeks or months.

Another scenario where this could be a need is when we have two types of Service Resources:

  1. Service Resources who work within the constraints of their Service Territory’s Operating Hour
  2. Service Resources who can go beyond their Service Territory’s Operating Hours. For eg: contractors (independent wealth managers) who can even take important appointments after working hours of the Service Territory (Branch)

Selectively restraining working hours of resources

First we need to define which resources can work beyond their branches working hours.

For this we are going to simply add a custom field to Service Resource of type boolean. Let’s call this field OvertimeEnabled for now.

If OvertimeEnabled is True then the Service Resource can work extra hours else they cannot.

Data Model

Next we need to set our Scheduling Policy such that we don’t restrict Shifts as per Service Territory’s Operating Hour by default. For this we will se up policy to use Shifts but uncheck the box ‘Use service territory’s operating hours with shifts’

Now we can add a trigger on Shift entity which runs before insert and update on the entity. In this trigger we will check validate that shift timings are within Branch’s Operating Hours only for resources who have OvertimeEnabled flag set to false.

Sample Code

https://github.com/anardana/salesforce-scheduler-shifts-validation

trigger ShiftValidation on Shift (before insert, before update) {
List<Shift> shifts = Trigger.new;

Set<String> sr = new Set<String>();
for(Shift shift: shifts) {
sr.add(shift.ServiceResourceId);
}

List<ServiceResource> extendedSr = [SELECT Id FROM ServiceResource WHERE OvertimeEnabled__c = False AND Id IN :sr];

Set<String> srIds = new Set<String>();
for(ServiceResource sr1: extendedSr) {
srIds.add(sr1.Id);
}

Set<Shift> confirmedShifts = new Set<Shift>();
Set<String> territoryIds = new Set<String>();
//We will only run this validation for Shifts with Status Category = "Confirmed" AND Service Resource can work extended hours
for(Shift shift: shifts) {
if(shift.StatusCategory == 'Confirmed' && srIds.contains(shift.ServiceResourceId)) {
confirmedShifts.add(shift);
territoryIds.add(shift.ServiceTerritoryId);
}
}

if(confirmedShifts.size() > 0) {
//Get all Service Territory IDs along with their OperatingHourId
ServiceTerritory[] territoryIdsWithOperatingHours = [SELECT Id, OperatingHoursId FROM ServiceTerritory WHERE Id IN :territoryIds];

Set<String> operatingHourIds = new Set<String>();
for(ServiceTerritory st: territoryIdsWithOperatingHours) {
//Service Territory may not have Operating Hour defined
if(st.OperatingHoursId != null) {
operatingHourIds.add(st.OperatingHoursId);
}
}

//Get Timeslot information for all Operating Hours got in previous step. We disregard all STM level concurrent timeslots (Shifts with MaxAppointments set to 1)
TimeSlot[] timeSlots = [SELECT Id, DayOfWeek, StartTime, EndTime, OperatingHoursId FROM TimeSlot WHERE MaxAppointments = 1 AND OperatingHoursId IN :operatingHourIds];

//Complex data structure to store Working hours for all Service territories. We will store empty inner map in case Service Territory does not have Operating Hour defined
Map<String, Map<String, TimeSlot>> serviceTerritoryWithTimeSlotsPerDay = new Map<String, Map<String, TimeSlot>>();

for(ServiceTerritory territory: territoryIdsWithOperatingHours) {
Map<String, TimeSlot> timeSlotsPerDay = new Map<String, TimeSlot>();

for(TimeSlot timeSlot: timeSlots) {
if(timeslot.OperatingHoursId == territory.OperatingHoursId) {
timeSlotsPerDay.put(TimeSlot.DayOfWeek.substring(0,3), TimeSlot);
}
}
serviceTerritoryWithTimeSlotsPerDay.put(territory.Id, timeSlotsPerDay);
}

//Main validation logic for all confirmed shifts
for(Shift s: confirmedShifts) {
String dayOfShiftStart = ((Datetime) s.StartTime).format('E').substring(0,3);
String dayOfShiftEnd = ((Datetime) s.EndTime).format('E').substring(0,3);

Map<String, TimeSlot> slots = serviceTerritoryWithTimeSlotsPerDay.get(s.ServiceTerritoryId);

if(dayOfShiftStart != dayOfShiftEnd) {
s.addError('Shift should be within Service Territory\'s Operating hours ');
} else if(slots.get(dayOfShiftStart) == null || slots.get(dayOfShiftEnd) == null) {
s.addError('Operating Hours for Service Territory not set up correctly');
}else if(s.StartTime.time() < slots.get(dayOfShiftStart).StartTime || s.EndTime.time() > slots.get(dayOfShiftEnd).EndTime) {
//Eureka
s.addError('Shift should be within Service Territory\'s Operating hours ');
}
}
}
}

P.S. For this code we have considered that Service Territory’s OH are in same timezone as shifts being created.

From Faith Kindle, Mrityunjoy Chowdhury & Sunil Nandipati: Accessing Inbound Guest Appointment Scheduler Flow from External Websites by Un-authenticated users

Context & Preface

Businesses and Organizations which provide services have to provide a way for their customers to request appointments with the provider’s service resources. These experiences are expected to be easy to access without any kind of account creation and no authentication involved. Examples can be booking an appointment with a hair stylist at your favorite Salon, looking up for a dentist for a regular cleaning, reaching out to a financial advisor at a bank near your home.

To implement un-authenticated user experiences, the best way in Salesforce is to use the Guest user profile that comes with any Experience Sites (Earlier Communities). Experience sites can be built with public pages where end-users can access the application built using these guest user access. Refer this knowledge article for more information around Guest users – https://help.salesforce.com/s/articleView?id=000327969&type=1

From a high level, there are three design options to allow un-authenticated Guest users to access Scheduler features and functionality. These design patterns are as follows and illustrated below.

  • API level: this pattern offers the most flexibility but requires the most investment in time as everything is custom built. You will host and build your interface entirely on your (external to Salesforce) web site, and create a fully custom-developed user interface with custom code to accesses Scheduler on the back end via Scheduler REST APIs.
    If you are wondering about how all of this works in the context an UNAUTHENTICATED (or guest) user experience — that’s a great question! The Scheduler API can be accessed from an unauthenticated type perspective using the oAuth SAML bearer assertion flow, which uses connected app to request/grant an access token for that “guest user” (unauthenticated) type experience. You will use a connected app associated with a particular profile to grant that profile permissions to access what is needed. This is similar to the way Salesforce Experience Cloud Guest User leverages the Guest User Profile & Guest User to allow unauthenticated type access to Salesforce objects and the Scheduler Inbound flow.
  • Component Level: this is the option that we will be going over in this blog. Using this pattern you will leverage Salesforce Experience Cloud/ public sites along with out-of-the-box or custom cloned versions of pre-built Scheduler flows. Scheduler flows are flexible and can be customized in a number of ways, including extending to the Scheduler APIs from within the flow. With this design pattern you will access Scheduler flows from your (external to Salesforce) business web site and remain on your web site’s URL.
  • Page Level: this level of access offers a relatively quick time to market and can be achieved in a number of ways”:
    • Leveraging the end to end Salesforce footprint, offering Scheduler capability as part of an Experience Cloud community.
    • Hosting your Scheduler flow on a public-facing Salesforce Site, and pointing to that site from your site’s menu. You can register a customized domain within Salesforce to use with your site to make the user flow as seamless and branded as possible.

Most business prefer NOT to have too many websites / links and prefer to have everything hosted on their business websites. In such scenarios the appointment scheduling experience needs to be embedded into the business’s website. To implement such user experiences, components build in Salesforce application need to be used outside Salesforce. Salesforce offers a mechanism called LightningOut to provide such experiences.

Configuration steps to be executed

  1. First and foremost thing is to have a Guest user Profile. Create a Digital Experience Site and make the Site available for public access. This can be enabled by navigating to the Site → Builder settings → General and Turning this feature ON.
  1. Navigating to the ‘Guest User Profile’ by clicking on the Profile link and exploring the Object settings, you will notice that the System ONLY allows users to either CREATE or READ on most of the objects. Configure this ‘Guest User Profile’ as mentioned in the help article – https://help.salesforce.com/s/articleView?id=sf.ls_set_up_guest_users.htm&type=5
  2. Create a Flow using the ‘Scheduler Scheduler Flow Template – Inbound New Guest Appointment’. Customize the flow as per your needs or use the template as is. OOTB template will create a lead and assign the service appointment against the lead.
  1. Most of all, for everything to work perfectly, configuring the Scheduler application using the guided setup. Navigate to the hamburger icon to see all applications, look for ‘Salesforce Scheduler Setup Assistant’ and complete the configuration steps

Understanding the Setup

The appointment time slots are determined based on your Org Setup followed by Salesforce Scheduler configurations and data setup. Ensure you have validated all these and setup configurations and data correctly

  1. Organization Setup-
  • Verify the Organization Wide Defaults for external access
    • For the below set of objects either set them to Public Read Only
      • Work Type
      • Work Type Group
      • Service Resource
      • Service Territory
      • User
    • Or extend access to the records by Creating sharing rules for guest users (Determine which records to expose to your Community’s guests and decide on a right sharing method – group based, role based or criteria-based record-sharing rules to match your business processes)
    • NOTE: Without defining sharing rules for guests, unauthenticated users can’t access the records required for the Inbound New Guest Appointment flow with Embedded Services hosted in a community or external site.
  1. Scheduler Application Setup – Ensure you have setup data for these objects
  • What services are offered by the Organization
    • WorkTypeGroup
    • WorkType
    • WorkTypeGroupMember
  • What locations and what times are these services Offered
    • ServiceTerritory
    • ServiceTerritoryWorkType
    • OperatingHours
    • TimeSlot
    • AppointmentTopicTimeSlot
  • What skills are required to provide these services
    • Skill
    • SkillRequirement
  • Who is providing these services and at what times they are NOT available
    • ServiceResource (A user or an Asset)
    • ServiceResourceSkill
    • ServiceTerritoryMember
    • ResourceAbsence
  1. Guest User Setup
  • Make sure you set the default timezone for the guest user profile for the Scheduler
    • Note: Additional customization is required to support the timezone mapping for guest appointments which can be passed via APIs if out of the box flow templates do not support use cases.
  • To allow a guest to schedule appointments, update the Guest User Profile to allow guests users to
    • Run Flows
    • Edit Events
  1. Optionally, configure field level security for the Asset field on the Service Resource object
  • The Asset field becomes available only when your org has an Asset Scheduling license enabled and provisioned in the instance
  • Grant Edit permission to profiles that will book appointments, including guest profiles
  • Add the Asset field to the page layout for the Service Resource object

External Website – Guest User Flow

As mentioned in the preface, to expose lightning components out of Salesforce, we will explore the LightningOut feature. External websites can be your business websites, and adding an experience for customers to request and schedule appointments we need to add a Lightning component to the external website.

Steps involved here

  1. Generate web-based html code that will include the details of the Experience site and the LightningOut component which launches the Guest user Flow. Here is a sample code and refer the demo video to know what changes to make to this sample code so that it works for you.
    1. <head></head>
      <body>
      <script src=”https://sandbox-234ss-cs1.cs1.stmfa.stm.force.com/234CommunityLogin/lightning/lightning.out.js”></script&gt;
      <script>
      $Lightning.use(“runtime_appointmentbooking:lightningOutGuest”, // name of the Lightning app
      function() {
      $Lightning.createComponent(
      “lightning:flow”, // top-level component of your app
      { }, // attributes to set on the component when created
      “lightningLocator”, // the DOM location to insert the component
      function(cmp) {
      console.log(‘Hi from callback’);// callback when component is created and active on the page
      cmp.startFlow(‘runtime_appointmentbooking__Guest_Flow‘);
      }
      );
      }, ‘https://sandbox-234ss-cs1.cs1.stmfa.stm.force.com/234CommunityLogin&#8217; // Community endpoint
      );
      </script>

      <div id=”lightningLocator”>
      <p>Lightning Component mentioned in the Script is invoked from here</p>
      </div>
      </body>
  2. Work with your company’s external website administrators to embed the above component into your company’s external website where you plan to have it.
  3. Get the details of the page like URL where this is hosted, this will be required to let the external traffic into salesforce in the next step
  4. Setup CORS
    1. Typically this to whitelist the Origin URLs, so that Salesforce can allow these web browsers to communicate with Salesforce

Case Study

Demo Video on Github Pages – https://drive.google.com/file/d/1ihDFnOaloYxMSJhp19qSFelTpvXuBTkC/view?usp=sharing
Demo Video on W3Schools – https://partners.salesforce.com/0684V00000FsyJF

From Shantinath Patil: Appointments with Dynamic Duration

In Salesforce Scheduler, Work Type is an entity where we store information about the skeleton of an invite. It’s just a template we can use to define meeting duration, block time before/after, timeframe start/end.

In the provided flow templates, work type selection happens with the help of a work type group. You first select a work type group, then a territory, and based on these two combinations; a work type is chosen automatically. Well, not automatically! The internal logic determines work type based on linking these three objects, as shown below ERD diagram.

Since the Scheduler data model only allows to map one work type to one work type group, there can always be only one work type for the selection of work type group and service territory.

This one-to-one mapping poses a limitation for a requirement where we want to define different duration based on customers needs. The good news is, there is a way to overcome this. This blog will discuss one of the approaches to set up Scheduler data to achieve dynamic duration. The basic premise is to duplicate the work type group and work type records based on durations.

Consider a scenario where you have a premier customer and a regular customer with whom you want to set up a meeting about ‘Wealth Management’. In this scenario, let’s consider that an exclusive customer always needs an appointment with a longer duration, say 90 minutes. Other customers may need a meeting with the same topic for 30/60 minutes. We can store this information on the work type group record for now. The data setup may look something like this:

Once the data is mapped correctly via junction objects in the above format, we can then carve out the following logic:

Following is the working flow with different duration:

VIDEO LINK: https://youtu.be/fwdVoxrrpgY

As you can see based on work type group selection, the flow selects the corresponding work type and duration gets adjusted accordingly.

You may ask this may confuse the end-user! Yes, looking at the same topic titles 3 times may confuse some end users! For this, you can either build your own component to display only the appointment topic and duration to choose. This approach may sound cleaner, but there is one more solution coming up in Spring 22 release. That feature allows you to filter work type groups shown on the screen by passing record ids to the component. We will cover more on that in another blog!

From: Shantinath Patil: Review Your Resource

In Spring 22, the Salesforce scheduler has given a new feature: enabling rendering HTML tags in Service Resource cards. This new feature has opened up many possibilities to show additional information about the resource.

This help guide lets you add fields to the Service Resource cards: https://help.salesforce.com/s/articleView?id=sf.ls_change_the_fields_that_appear_on_the_service_resource_cards.htm&type=5.

Peer reviews and ratings are common decision making guides for customers today to help them pick among

Imagine you were to add the capabilities of a feedback management software (eg: Salesforce Feedback management) to Salesforce Scheduler, you could ask your customers to provide ratings for their appointments and then use those ratings to guide future customers.

With Feedback Management you can not only use survey invitation rules to distribute surveys when an appointment is closed but also use the data mapper feature to easily map the rating from the survey to any object in Salesforce, in this case to the Service Appointment object.

Refer to this link for more details on the datamapper and survey invitation rules

Once you have captured the rating data for your resources, there are several possibilities

  • Help the consumer decide between resources
  • Help Territory Mgr evaluate resources & branch
  • Give preferential treatment to premium customers by only showing them higher rated resources
  • Use Salesforce Einstein’s Machine Learning capabilities to predict who would be the best resource for this prospect based on past ratings giving by similar prospects eg: Someone from the South might prefer a resource from the south, or someone from an age group would prefer certain Resources.

For this blog, we’re going to focus on the Salesforce Scheduler changes you need to make to get the above capabilities going.

let’s take an example of adding a rating to the service resource. Please follow the below steps to show the shining rating stars on the resource card:

1. Fields on Service Resource

To show the rating, we need to create a formula field on Service Resource to render rating stars in cards. We will add this field to the compact layout to show up in the out of the box component. However, to populate value in this field, we need two more fields on Service Resource: to capture the average rating for each of the appointments he served and the number of appointments.

Field LabelField API NameData TypeComments
RatingRating__cNumber(1,0)Number field to store rating between 1-5
Review CountReview_Count__cNumber(18,0)Number field to store number of reviews this service resource have received
Star RatingStar_Rating__cFormula(Text)Formula field which will show ratings based on above 2 fields

The sample formula will turn out like this:

IMAGE(
IF(TEXT( CEILING(Rating__c) ) = '0', "/img/samples/stars_000.gif",
IF(TEXT( CEILING(Rating__c) ) = '1', "/img/samples/stars_100.gif",
IF(TEXT( CEILING(Rating__c) ) = '2', "/img/samples/stars_200.gif",
IF(TEXT( CEILING(Rating__c) ) = '3', "/img/samples/stars_300.gif",
IF(TEXT( CEILING(Rating__c) ) = '4', "/img/samples/stars_400.gif",
IF(TEXT( CEILING(Rating__c) ) = '5', "/img/samples/stars_500.gif",
"/img/samples/stars_000.gif"
)
)
)
)
)
),
'Rating Level'
) + ' ('+ TEXT( Review_Count__c ) + ' Reviews)'

2. Field on Service Appointment

We will have to create a field on Service Appointment to capture the sentiment of a customer based on how the appointment went. We can populate this value from a Survey, which will be sent out once an appointment is complete.

Field LabelField API NameData TypeComments
RatingRating__cNumber(1,0)Number field to store rating between 1-5

3. Propagate value from Service Appointment to Service Resource

Now that we have fields on Service Resource and Service Appointment, we will have to process the aggregate reviews. As shown in the below data model diagram, we can traverse through Assigned Resource and populate this value to Service Resource.

The review aggregator formula that we can use is:

 ((Current Average Rating * Total Reviews) + New Rating) / (Total Reviews+ 1)

Using this formula, we can write a trigger on Service Appointment. As soon as we get a rating from Service Appointment, we will aggregate the review and update its corresponding Service Resource. A sample trigger code is given below:

trigger UpdateSurveyRating on ServiceAppointment (after insert, after update) {

Set<Id> vSetServiceAppointment = new Set<Id>();

for(ServiceAppointment sapp : Trigger.new){
if(Trigger.isInsert && sapp.Rating__c != null){
vSetServiceAppointment.add(sapp.Id);
}else if(Trigger.isUpdate && Trigger.oldMap.get(sapp.Id).Rating__c != sapp.Rating__c){
vSetServiceAppointment.add(sapp.Id);
}
}

if(!vSetServiceAppointment.isEmpty()){

Map<Id, List<ServiceResource>> vMapServiceAppToResources = new Map<Id, List<ServiceResource>>();
List<AssignedResource> vListAssResource = [SELECT Id, ServiceResourceId, ServiceAppointmentId FROM AssignedResource WHERE ServiceAppointmentId IN: vSetServiceAppointment];

if(vListAssResource != NULL && !vListAssResource.isEmpty()){

Set<Id> vSetServiceResource = new Set<Id>();

for(AssignedResource vAssRes : vListAssResource){
vSetServiceResource.add(vAssRes.ServiceResourceId);
}

Map<Id, ServiceResource> vMapServiceResource = new Map<Id, ServiceResource>([SELECT Id, Rating__c, Review_Count__c FROM ServiceResource WHERE Id IN: vSetServiceResource]);
for(AssignedResource vAssRes : vListAssResource){
if(!vMapServiceAppToResources.keySet().isEmpty() && vMapServiceAppToResources.containsKey(vAssRes.ServiceAppointmentId)){
List<ServiceResource> vTempResources = vMapServiceAppToResources.get(vAssRes.ServiceAppointmentId);
vTempResources.add(vMapServiceResource.get(vAssRes.ServiceResourceId));
vMapServiceAppToResources.put(vAssRes.ServiceAppointmentId, vTempResources);
}else{
List<ServiceResource> vTempResources = new List<ServiceResource>();
vTempResources.add(vMapServiceResource.get(vAssRes.ServiceResourceId));
vMapServiceAppToResources.put(vAssRes.ServiceAppointmentId, vTempResources);
}
}

//((Current Average Rating * Total Reviews) + New Rating) / (Total Reviews+ 1)
Map<Id, ServiceResource> vMapServiceResourceToUpdate = new Map<Id, ServiceResource>();
for(ServiceAppointment sapp : Trigger.new){
if(vMapServiceAppToResources.containsKey(sapp.Id)){
for(ServiceResource vResource : vMapServiceAppToResources.get(sapp.Id)){
if(vMapServiceResourceToUpdate != NULL && vMapServiceResourceToUpdate.containsKey(vResource.Id)){
ServiceResource vTempResource = vMapServiceResourceToUpdate.get(vResource.Id);
vTempResource.Rating__c = (((vTempResource.Rating__c * vTempResource.Review_Count__c) + sapp.Rating__c) / vTempResource.Review_Count__c+1);
vTempResource.Review_Count__c++;
vMapServiceResourceToUpdate.put(vTempResource.Id, vTempResource);
}else{
Decimal rating = vResource.Rating__c != NULL ? vResource.Rating__c : 0;
Decimal reviews = vResource.Review_Count__c != NULL ? vResource.Review_Count__c : 0;
vResource.Rating__c = (((rating * reviews) + sapp.Rating__c) / (reviews+1));
vResource.Review_Count__c = ++reviews;
vMapServiceResourceToUpdate.put(vResource.Id, vResource);
}
}
}
}

UPDATE vMapServiceResourceToUpdate.values();
}
}
}

Note: This is just a concept illustrator built for a simple create Guest appointment use case using the out of the box flow template.

Possibilities

From Ruchi Sharma: Setup a Topic Hierarchy

Salesforce Scheduler provides a component to display and select a work type group.
By default, this component display all the work type group records, in Spring ‘22 this component is getting the capability to filter work type group records based on specified Ids.

You can check the component documentation here.

Work Type Group Hierarchy

Imagine you are a bank manager and a user is scheduling an appointment with the bank.
While scheduling an appointment you want the user to select hierarchical work type groups to know about the appointment context.

Example – Suppose the first level work type group hierarchy consists of personal banking, wealth management, etc. Based on user selection we will display the next level hierarchy. If the user selects personal banking then the second level work type group hierarchy consists of checking saving account, loan management, etc.

You can easily display work type group hierarchy in salesforce scheduler flows with clicks, not code! 🙂

Please check below 3 level hierarchy demonstration

Let’s look at how it is done,
Configuration is the same for Outbound, Inbound, and Guest flows, illustrating for Outbound Flow.

  1. Create a self lookup in the Work Type Group object.
    This will store the parent work type group record id.
  2. For this feature, we will be using the ‘Filter by work type group Ids’ input configuration of the ‘Select Work Type Groups’ component.
  3. Add a new Assignment element called “Set filter by work type group ids as null”, after Set Topic Stage.
    And the arrows pointing to the topic screen will now point to this element.

Assignment element :
In this element, we are setting the ‘FilterByWorkTypeGroupIds’ variable as null.

OOTB flow :

After change

  1. After the assignment element adds a “Get Records” element to fetch the work type group records.
    Initially, we are assuming variable workTypeGroupId is null.
    Work_Type_Group__c is the self lookup field.
  1. Loop over fetched work type group records and append the work type group Ids in the “FilterByWorkTypeGroupIds” variable.
  1. After the loop element adds a decision element to check FilterByWorkTypeGroupIds is not null (next-level hierarchy exist) or workTypeGroupId is null (If it’s loading for the first time).
  1. If the next level hierarchy exists then add an assignment element to clear the previously selected work type group and connect this element to “Topic Screen“.
    Else redirect to “Set Appointment Stage”.
  2. Optionally to display the selected hierarchy on top of the work type group selection screen, add a ‘Get Records’ element to fetch the selected work type group record.
    Redirect the topic screen to this element.
    Then add an assignment element and create a work type group collection variable to add the work type group record.
  3. Finally, connect the above assignment element to the assignment element that we have created in step 3.
  1. To show the selected hierarchy, drag and drop the “DisplayWorkTypeGroupHierarchy” custom component above the “Select Work Type Groups” component in the topic screen.
    And pass the work type group collection variable as input to this component that we have created in step 8. (optional)

That’s it! Activate and run the flow.

All the code is bundled at https://github.com/ruchi2994/workTypeGroupHierarchy. It contains the above component and updated flow.

From Chris Albanese: VRA Scheduling – Use Salesforce Scheduler and Visual Remote Assistant to provide virtual meetings

Salesforce Visual Remote Assistant (VRA) allows you to provide immediate assistance to your customers and employees over video chat. You can find much more information on VRA on Salesforce’s website at https://www.salesforce.com/products/service-cloud/features/visual-remote-assistant/

With the Winter ’22 release, VRA introduced a new feature to facilitate the scheduling of virtual appointments between agents or mobile workers and customers with Visual Remote Assistant. This means that if you are using Salesforce Field Service (SFS) or Salesforce Scheduler (Scheduler) you can now have VRA meetings automatically generated and shared with your customer at the time of appointment scheduling. For example, your customer can create an appointment today for a meeting next Tuesday at 9:00AM. When the appointment is created, the customer is sent a link for the Virtual Meeting that they will use at the time of the meeting.

Check out some more details on this below.

What comes in the VRA Scheduling package?

For customers who have purchased VRA and either SFS or Scheduler or both:

  • A separate managed package installed on top of VRA that contains the VRA Scheduling capabilities
    • Experience Cloud Lobby Component
      • for the user to await the start of the meeting
    • Service Appointment Component for VRA Scheduling
      • generates pop up reminder at time of appointment when the agent opens the Service Appointment
    • Custom Notification
      • to remind the Service Appointment Assigned Resource (agent) of their upcoming meeting
    • Session Lobby Invite object
    • Additions to the Visual Configuration setup screens to manage this feature
  • See VRA Installation and Configuration Manual and VRA Admin Manual for more info

Quick Video of it in action

See this video which illustrates what it can look like after a few simple configuration steps

Setting up VRA Scheduling for Salesforce Scheduler

Preliminary Things

  • Make sure Scheduler is enabled in your org
  • Determine when you want to generate VRA Scheduled Appointments
    • For example, if a user selects an Appointment Type of Video this would trigger the creation of the VRA Lobby Link
  • Plan a confirmation and reminder strategy
    • VRA Scheduling comes with a flow that you can use to send the initial confirmation via email
    • You may want to also send a reminder notification to your customer prior to the meeting, such as 1 hour before
  • Enable Digital Engagement so you can send SMS messages for the confirmation and reminder notifications
  • Install the latest versions of the packages

Object Model Changes

I made 2 small changes to the Service Appointment Object to add 2 text fields. This was done to simplify the sending of an SMS Message to the customer when scheduling a new virtual Service Appointment.

  • First Name – this text field stores the first name of the contact associated with the Service Appointment
  • Lobby Link – this text field stores the Session Lobby Invite url

Flow Setup

VRA Manage Lobby Invites
Configure the flow called VRA Manage Lobby Invites to meet your needs. This flow is configured out of the box to create, update and delete Session Lobby Invites based on the life cycle of a Service Appointment. You need to tailor the trigger criteria and the decision criteria to meet your needs.

Trigger Criteria
The flow triggers on Service Appointment and provides criteria that you can tailor. For Scheduler, I removed the work type criteria that is present and replaced it with Appointment Type = Virtual. Whenever a Service Appointment with Appointment Type = Virtual, this flow will fire.

Out of the Box Trigger Criteria

My criteria which is based on Appointment Type. Only Service Appointments with Appointment Type = video (this is the API value for Virtual meetings) will trigger this flow

Decision Criteria
The out of the box flow will create Session Lobby Link records when the Service Appointment is Dispatched. This applies nicely for Salesforce Field Service. For Scheduler, this may or may not apply, so edit this accordingly. I left this as is and created a supplemental flow (below this screen shot).

Supplemental Flow
I created an additional flow to accomplish 2 things: 1) make sure the SA has an email and phone saved to it and 2) change the status to Dispatched. This is to trigger the VRA Manage Lobby Invites flow.

It’s a triggered flow on SA whenever the SA is created and the Contact Id is present but the email address is not

Overview of flow. It retrieves the contact record from the SA and then updates the SA’s status, email and phone.

Update the SA status, email and phone

Session Lobby Link update flow
This is an additional flow which updates the lobby link to precisely share with the customer the experience site page containing the Lobby component. Out of the box, the link generated points to the experience site home page. This flow allowed me to put the Lobby component on a specific experience site page. This is not required, but it provided me with flexibility to put the lobby component on any page.

Overview of flow. Note that the last step is a Messaging Notification action to send an SMS message to the customer.

Updating the Lobby Link to the desired experience site page

Details of the formula used. It’s a simple text substitution to insert the specific experience site page. In my example, I placed the lobby component on a page called vra-lobby, so I’ve inserted that value into the url.

Updating the Service Appointment. In this step, I retrieve the Service Appointment using the Session Lobby Invite field called Record Id (not to be confused with Id). I’m updating my 2 custom fields to make the SMS messaging template easier to construct.

Messaging Template

This is the template I used. You can see I’m using the custom fields I created for First Name and the Lobby Link as well as the appointment start time.

Experience Cloud Changes

This was a simple change. I added a new page to my experience site called vra-lobby. I placed the Lobby Component on that page. I made sure that the page was accessible to a guest user.

Service Appointment Page Reminder Component

Add the Service Appointment reminder component to the Service Appointment page. This component requires no parameters. It generates a pop up notification when opening the SA to remind the user there is a virtual session that needs to start.

Summary

VRA Scheduling makes it easy to provide customers with links to a 2 way video session when scheduling an appointment.

Additional Notes about this Example

This is just a concept illustrator. You may want to add additional logic to your flows to send reminders to the customer along the channel of their choice. I’ve used SMS in my example, but making this more generic to support additional channels is recommended.

Appointment Reminders for the customer are not depicted in this example, but these would be easy to set up with a flow.

From Sunil Kumar Nandipati: Salesforce Scheduler with Einstein Activity Capture

Salesforce Scheduler

Salesforce Scheduler (formerly known as Lightning Scheduler) gives you the tools you need to simplify appointment scheduling in Salesforce. Create a personalized experience by scheduling customer appointments—in person, or by phone or video—with the right person at the right place and time.

Enabling Event Management for Salesforce Scheduler

When you enable the Event Management setting under Salesforce Scheduler settings, Salesforce Scheduler writes service resources’ appointments as events on their Salesforce calendar. To write events from Salesforce calendar to external calendars, use a calendar sync tool such as Einstein Activity Capture (EAC).

If a calendar sync tool doesn’t work for you, you can explore Platform Events and APIs related to the specific external calendar tool that you want to be externally synced with Events created in salesforce. For the sake of this article we will limit our conversations around EAC.

Einstein Activity Capture

Einstein Activity Capture (EAC) helps keep data between Salesforce and external email and calendar applications (like Microsoft Outlook or Google Calendar) up to date. Objects that can be synched using EAC include Tasks, Events, Contacts and Emails. This article will NOT cover the configuration of EAC, however will limit our conversations around events, since Scheduler “Event Management” is limited to the scope of only synching service appointments to Events.

Configurations to watch out between both applications when using Salesforce Scheduler

  1. Whenever a service appointment is created in “Salesforce Scheduler”, if the “Event Management” feature is turned ON, a related Event is created and added to the user’s Salesforce Calendar. For the EAC event sync directions – “Salesforce to External (Microsoft or Google or Similar)” and “Synch Both ways”; this implies that the said event will be synced to the user’s external calendar application (Microsoft or Google or Similar).
    1. Events that are created by the Salesforce scheduler when a service appointment is created are restricted from editing on Salesforce. Guidelines need to be setup on updating these events on an external calendar (after they are synced from Salesforce to external).
    2. If a user tries editing these events on an external calendar and tries to sync back to Salesforce – EAC will NOT be able to update existing event on the Salesforce Calendar, user needs to resolve these errors later by retaining original time as recorded on Salesforce.
    3. To understand what fields on the Service Appointment are mapped to create the Event record – Refer the field mapping table in the help documentation https://help.salesforce.com/s/articleView?id=ls_enable_event_mgmt.htm&type=5&language=en_US
    4. We will try to cover about mapping custom fields on the service appointment to the Event in a future blog
  2. For the EAC event sync directions – “External (Microsoft or Google or Similar) to Salesforce” and “Synch Both ways”; this implies that the events created in the external calendar application will be synched to Salesforce and related events would be created in Salesforce and will be viewable on the Salesforce calendar. When a scheduling flow executes, the system will look at the scheduling policy assigned to the Flow, and during that time if the system finds that the scheduling policy setting “Check Salesforce Calendar for Resource Availability” is turned ON, Scheduler will exclude the booked slots (of external synched events) along with existing Service Appointments booked for the resource and ONLY show the overall available slots in the Time Slot selection screen.

References:

Scheduler – https://help.salesforce.com/s/articleView?id=sf.ls_event_mgmt_overview.htm&type=5
EAC – https://help.salesforce.com/s/articleView?id=sf.einstein_sales_aac.htm&type=5
Events and EAC – https://help.salesforce.com/s/articleView?id=sf.aac_guidelines_sync.htm&type=5