Create Dynamic Calendly Scheduling button in Salesforce

sf_cal
3 min readMar 27, 2021

--

Calendly provides an excellent guide for creating a custom button in Salesforce to schedule Calendly meetings; however, this guide assumes that your Salesforce users are all using the same Calendly link. This means that you would need to create multiple buttons to support each user’s unique Calendly link which can be a problem for larger organizations.

In this article we are going to create a single button that will open the Salesforce user’s unique Calendly url.

Create a Calendly link field on the user object

Before we create the Scheduling Meeting button we first need to create a custom field to store the user’s unique Calendly link:

  • From the Setup menu search Object Manager
  • From the Object Manager screen select User > Fields & Relationships > New > Text Field

On the New Custom Field screen enter the follow information:

Field Label: Calendly Link
Length: 255
Field Name: Calendly_Link

Create custom user field screen

Create Custom Button

We are going to follow the Calendly guide here with one small change. Instead of hard coding our Calendly scheduling link we will instead use the {!$User.Calendly_Link__c} merge field which will result in the following button link:

https://calendly.com/{!$User.Calendly_Link__c}?sfid={!Opportunity.Id}&first_name={!Contact.FirstName}&last_name={!Contact.LastName}&email={!Contact.Email}

$User is a global merge field that contains information about the currently logged in user. This means that {!$User.Calendly_Link__c} will return the unique Calendly scheduling link for the specific user that clicks the Calendly scheduling button.

But we have hundreds of users, how can we easily sync their scheduling links to Salesforce?

To sync the user’s Calendly link to Salesforce we are going to use Calendly’s V2 api which will allow us to retrieve a list of users in our Calendly organization.

While you can use the HttpRequest Apex class to interact with the v2 api, we are going to install the calendly-salesforce-sdk package to more easily interact with the api.

You can install and setup the calendly-salesforce-sdk by following the setup guide here.

Once you’ve successfully installed and setup the package above, we can now sync the users’ Calendly scheduling links to Salesforce by running the following Apex code:

CalendlyApi.OrganizationMembershipQueryParams params = new        CalendlyApi.OrganizationMembershipQueryParams();CalendlyApi.Organization org = CalendlyApi.getCurrentOrganization();CalendlyApi.OrganizationMembershipCollection result;Map<String, String> emailSchedulingLinkMap = new Map<String, String>();Boolean hasNextPage = true;/* Retrieve all Calendly Organization Memberships */
while (hasNextPage) {
result = org.getOrganizationMemberships(params);

for (CalendlyApi.OrganizationMembership m : result.collection) {
String email = m.user.email;
String schedulingLink = m.user.slug;
emailSchedulingLinkMap.put(email, schedulingLink);
}

hasNextPage = result.pagination.hasNextPage();
}
Set<String> emails = emailSchedulingLinkMap.keySet();List<User> calendlyUsers = [
SELECT Id, Email, Calendly_Link__c
FROM User
WHERE Email
IN :emails
];
List<User> usersToUpdate = new List<User>();for (User calendlyUser : calendlyUsers) {
String email = calendlyUser.Email;
String calendlyLink = emailSchedulingLinkMap.get(email);
calendlyUser.Calendly_Link__c = calendlyLink;
usersToUpdate.add(calendlyUser);
}
update usersToUpdate;

If the code above ran successfully then all of your Salesforce organization’s user records (for users who are also in your Calendly organization) will have the Calendly_Link__c field populated with the user’s unique Calendly link.

--

--

No responses yet