Field Service Lightning is Salesforce’s onsite job management platform that helps organizations to dispatch technicians/agents to do a particular job, by taking into account their skills and tools and also their driving and service times.
To help the technicians/agents, Salesforce also provides Field Service Mobile App for Android and iOS, which, at the moment this article is being written, lacks the end-user’s calendar synchronism capability.
Cronofy is a service that enables software applications to connect to users’ calendars and perform actions on their behalf, like create calendar events, check scheduling availability, etc..
Explaining the parameters:
RESPONSE_TYPE - must always be code
CLIENT_ID - it’s the one from Cronofy App
REDIRECT_URI - you’ll need to send this back to a Salesforce page that you also need to create, because when the user approves this authorization, it will send you back a code.
SCOPE - This parameter is the scope of privileges you want to perform your actions
On the REDIRECT_URI page, you will receive a code on URL, that you must use to request an access token, which will provide you with several important parameters to be used on the following calls. This is an example of what the response should look like:
{
"token_type": "bearer",
"access_token": "P531x88i05Ld2yXHIQ7WjiEyqlmOHsgI",
"expires_in": 3600,
"refresh_token": "3gBYG1XamYDUEXUyybbummQWEe5YqPmf",
"scope": "create_event delete_event",
"account_id": "acc_567236000909002",
"sub": "acc_567236000909002",
"linking_profile": {
"provider_name": "google",
"profile_id": "pro_n23kjnwrw2",
"profile_name": "example@cronofy.com"
}
}
Next step you’ll need to perform is a call to calendars, which will provide the list of calendars the user has authorized. This is an example of what the response should look like:
{
"calendars": [
{
"provider_name": "google",
"profile_id": "pro_n23kjnwrw2",
"profile_name": "example@cronofy.com",
"calendar_id": "cal_n23kjnwrw2_jsdfjksn234",
"calendar_name": "Home",
"calendar_readonly": false,
"calendar_deleted": false,
"calendar_primary": true,
"calendar_integrated_conferencing_available": true,
"permission_level": "sandbox"
},
{
"provider_name": "google",
"profile_id": "pro_n23kjnwrw2",
"profile_name": "example@cronofy.com",
"calendar_id": "cal_n23kjnwrw2_n1k323nkj23",
"calendar_name": "Work",
"calendar_readonly": true,
"calendar_deleted": true,
"calendar_primary": false,
"calendar_integrated_conferencing_available": false,
"permission_level": "sandbox"
}
]
}
You’ll need calendar_id parameter for the next call.
The final step will be to create a Record-Triggered Flow or a Trigger with Apex to call Cronofy’s upsert-event when Schedule Start and Schedule End dates are set on Service Appointments. This is an example of what the call should look like:
POST /v1/calendars/{CALENDAR_ID}/events
Host: {API_HOST}
Authorization: Bearer {ACCESS_TOKEN}
Content-Type: application/json; charset=utf-8
{
"event_id": "qTtZdczOccgaPncGJaCiLg",
"summary": "Scheduled Appointment",
"description": "Work on ...",
"start": "2021-03-27T15:30:00Z",
"end": "2021-03-27T17:00:00Z",
"location": {
"description": "Main Street, 12th"
}
}
Explaining the parameters:
CALENDAR_ID, API_HOST and ACCESS_TOKEN - are the ones from the previous calls
event_id - it’s your own generated id, it can be the Schedule Appointment Id