I developed a solution with Salesforce to receive SMS alerts before a meeting
Hey! I am convinced that it's really important to prepare well for the meetings we get, but it's so easy to forget them. So, to avoid this situation, I've created a new solution. When you are on the Calendar object on Salesforce, and you want to add an Event, you can specify a time before a meeting, and you will receive a SMS alert at this time. With this, you are sure to be the best prepared for your meeting!
Step one: Configuration
To develop this solution, we will need to integrate Salesforce with Twilio API. Don't worry, I will explain everything!
The first thing to do is to sign up for the Twilio API. Then you have to go to the Twilio console, and to get your credentials. You will need three things: an API token, an Account SID, and a sender phone number. These three pieces of information will allow you to connect to the Twilio API, and to send some messages. Even if sending an SMS with this API has a cost, you can benefit from a 15 dollars credit with the trial version. And you don't need to enter your credit card information, so that's pretty cool, and to be honest, the 15 dollars will be really enough to test our development.
Now that we have this information, we can create a custom metadata type, called Twilio_Credentials__mdt. On this custom metadata type, we create three fields: Account_SID__c, Phone_Number__c, and Token__c. The phone number will be the number from which Twilio will send the SMS.
When it's done, we can add a new custom metadata type record, Send_SMS, and add the given information in it.
Then, we have to authorize the connexion to Twilio. For this, we use a remote site setting.
Now that the connexion is authorized, we can move to the purely functional part. What do we want? We want a picklist with some values of when to send the sms. So we create it, with these values.
Now, when we want to create an event on the calendar, we got this:
Step 2: The Apex part
Now that the UI is set, what do we want? We want that, when we create an event, we send an SMS.
So, using a trigger is the thing to do. A flow could be used too, but in every case, we will have to use some Apex code(it's especially true when we have to call an API), so we prefer to do everything in Apex instead of using a flow which will call some Apex code, but it's up to you to choose between both.
We also have to be aware of the fact that here, handling the events inserts is not sufficient. What do we do when we change the date of an event. Does the SMS have to be sent at the previous time? And when do we delete the event?
For this, we check the context(before, after, update, insert, delete) on the trigger, and then we call the trigger handler.
Using a trigger handler is really good to keep our code clean.
The trigger handler
As you guessed, all the methods you've seen called on the trigger are defined right here, on the handler.
For this handler, the biggest challenge was to remove the existing jobs when it was needed. I am sure you noticed that I didn't add some State filters on the SOQL I made for the cronTriggers. It's because if I did, I would have a bug every time I would try to change the date of an event, when the SMS has already been sent(the State of the job is "deleted", but it's still on the database, so the new job can't have the same name). This part has still to be improved.
The REST callout
So, here we schedule a callout. For this, we need both the Schedulable interface, and a future method(callouts cannot be made from a Schedulable alone). By knowing this, we associate the current user phone, the credentials, and the Twilio phone, to make our callout. When it's done, we log a debug('success' or 'failure').
Conclusion
Now that everything is in place, we can receive an SMS every time we add an event to the calendar, which is great! At this time, the message is not personalized, but it could be, by adding some other lines of code!