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:
Name | Cuttly URL Shortner (API-CuttlyURLShortner) |
Url | https://cutt.ly/api/api.php |
Identity Type | Anonymous |
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.
Password | Password__c | Text |
UserName | UserName__c | Text |
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.
References:
https://cutt.ly/api-documentation
Similar Post:
Use Named Credential to Upload File in S3
2 comments
This may be useful for your readers.
We have created a Native Salesforce solution for URL Shortener available on Salesforce AppExchange (Shortify). This is a Native solution, So no need for any 3rd party service integration. Best part is that it is totally free. Currently we support Salesforce Campaign, CSV upload as a data source to generate short URLs and you can also create easy to remember short URLs as http://cfsu.in/appexchange (this short URL is for our Salesforce AppExchange listing page [https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3u00000MRhZ1EAL] generated from Shortify Solution).
Thanks
Hey @CustomerFoster, Thank you for sharing.
Regards
Dhanik