This was a fun challenge and I ended up using @InvocableMethod apex class so that the URL shortening could be kicked off by Process Builder. In my example below, I added a custom field “Short URL” to the Case object. I have a single Process that kicks off when a Case is created or updated and monitors when the “Short URL” field changes. If the field is blank then it calls out to Bitly to generate the url asynchronously (the async part is important and I’ll discuss that later). When the “Short URL” field becomes non-blank then Process Builder sends an email alert to the case owner with the shortened link.
Step 1. Sign-up for Bitly (FREE)
To use the Bitly API you will need to register. You can sign-up using Twitter, Facebook, or create a new login with their service. I chose to create a Bitly specific login because I don’t like sharing my social logins with websites.
Step 2. Create Named Credential in Salesforce
In Salesforce, go to Setup | Security Controls | Named Credentials and create a new Named Credential to store your Bitly username/password. Even though Bitly discourages us from storing the password, I think it’s ok in this scenario because (a) it’s ours and not someone else’s, and (b) Named Credentials stores the password securely and once set it’s no longer visible to other users. To me, that has a lot more security to it than if we were to store the Access Token in a Custom Setting.
Step 3. Create Custom Field “Short URL”
Create a custom URL field on your objects of interest to store the Bitly generated short urls. Depending on your situation you may have one or more. The sample code I share only supports one on the Case object, but you can easily adapt the code to your needs.
Step 4. Create Bitly Apex Classes
I developed two classes, one that encapsulates the Bitly http callouts and the other to expose the service as @InvocableMethod so it can be invoked by Process Builder. An important point to note is that the BitlyShortenURLInvocable class actually calls a @Future method to generate the short urls asynchronously. This is required because if you don’t then you’ll get an error in Process Builder that you have uncommitted work and you must either commit or rollback before making the http callout to the Bitly service. When the async process completes then it updates the Case objects with the new short urls which Process Builder will pick up on the change then send the email alert (or whatever you want it to do).
Step 5. Create Process to Automate URL Shortening
In our simple example, navigate to Process Builder to create a new Process on the Case object whenever it is created or edited.
- The first decision criteria should check if the custom “Short URL” field is blank.
- If true then call apex class BitlyShortenURLInvocable passing in the case record’s ID
- The second decision criteria should check if the custom “Short URL” field is not blank and was changed.
- If true then send an email alert or some other action to use the now populated “Short URL” field
Step 6. Create a New Case!
Activate your Process then go to create a new Case. Once saved, you may need to refresh the page after a couple seconds and then you’ll notice the “Short URL” field has been populated. Voila!
In this example, I setup a simple email alert to the Case owner with the short url link as shown in this screen shot: