URL Shortening is required when we send email with some URL link or while sending SMS to customer/vendor from Salesforce Application. Sending SMS/Email is a very common requirement in each application to update about the status of requirements/case/requests to customers. This post will give step by step guide to integrate Cuttly URL shortener in Salesforce using API.
Steps to integrate Cuttly API
- Signup in Cuttly
- Add Cuttly API Url in named credential
- Create Custom Metadata Type type to hold API Key
- Create a generic URL shortener Apex class
- Use Url shortener class in your code, flow
1. Signup in Cuttly
We need the Cuttly API URL to start integration within Salesforce. We have to signup at Cuttly first. After signing up, confirm the activation link which is sent to the provided email.
2. Add Cuttly API Url in named credential
After activation login into to Cutt.ly site and create an API key for yourself. Click the API menu, and right-side top section, you will see the manage API link to create a new API key.
Now let us create one named credential to store Cuttly API URL and generated API key. You can also use custom setting or custom metadata to store these detail but in that case, you have to add that URL in Remote Site Setting.
Named Credential Detail:
|Cuttly URL Shortner (API-CuttlyURLShortner)
3. Create Custom Metadata Type type to hold API Key
Cuttly is Get API request so we can not use Named Credential username/password field in API URL. We have to store API keys in custom setting or custom metadata. Based on your application architecture save API key.
I have used custom metadata to store API key. This metadata can store other API detail as well.
Create Custom Metadata API Configurations (APIConfiguration__mdt). Add two custom fields UserName and password of text type in this metadata.
Add records in metadata for Cuttly API. To get Metadata detail in code, create an Apex class.
4. Create a generic URL shortener Apex class
Create a generic Cuttly Service class to generate a short URL from a long URL.
5. Test CuttlyService class in Apex
To test the above created CuttlyService apex class use the below code in your apex.
//Single Paramter new CuttlyService().shortUrl('https://salesforcecodex.com/salesforce/optimize-soql-filter-in-apex-code'); //Short URL will be //https://cutt.ly/mPjZimC //with custome url new CuttlyService().shortUrl('https://salesforcecodex.com/salesforce/optimize-soql-filter-in-apex-code','ap-02'); //Short URL will be //https://cutt.ly/ap-02
6. Use CuttlyService class in Flow
We can use CuttlyService in flow as well. For this, we have to create one InvocableMethod so that we can use that in Flow. Create an apex class CuttlyServiceInv for this.
This method now can be used as apex action in flow.