Home SalesforceApex Cuttly Url Shortener in Salesforce

Cuttly Url Shortener in Salesforce

by Dhanik Lal Sahni

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

  1. Signup in Cuttly
  2. Add Cuttly API Url in named credential
  3. Create Custom Metadata Type type to hold API Key
  4. Create a generic URL shortener Apex class
  5. 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.

Cuttly Shortener in Salesforce | SalesforceCodex

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:

NameCuttly URL Shortner (API-CuttlyURLShortner)
Urlhttps://cutt.ly/api/api.php
Identity TypeAnonymous
Cuttly Shortener in Named Credential | SalesforceCodex

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.

PasswordPassword__cText
UserNameUserName__cText

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.

Apex action in Flow | SalesforceCodex

References:

https://cutt.ly/api-documentation

Similar Post:

Use Named Credential to Upload File in S3

You may also like

2 comments

Customer Foster April 6, 2022 - 9:17 am

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

Reply
Dhanik Lal Sahni April 18, 2022 - 11:23 am

Hey @CustomerFoster, Thank you for sharing.

Regards
Dhanik

Reply

Leave a Comment