YouTube and Salesforce are among the most powerful platforms available out there today. In this blog I am going to take you through a step-by-step process for accessing API’s with the help of Salesforce. I’ll be using an example that is familiar to everyone – YouTube. Using YouTube APIs, I’ll show you how to access video details, i.e. the number of views, likes, dislikes and comments for your favorite cat video!
Step 1: Create a Salesforce Custom Object
In the Salesforce Org create a Custom Object called ‘YouTube Video’. For those who have just started finding your way around Salesforce, follows these steps for creating a Custom Object.
- Click Setup → Quick Find / Search → Create → Objects → New Custom Object
- Name Your Custom Object as YouTube Video (Plural form will be YouTube Videos) and your record name will be YouTube Video Name.
- Click Save
Step 2: Add Custom Fields & Relationships
The next step is to add Custom Fields & Relationships to your newly created YouTube Video Custom Object. Create Number of View Counts, Number of Likes, Number of Dislikes, Number of Comments, Video Id as your Custom Fields.
Number of View Counts : This Field will give how many Views the YouTube Videos have
Number of Likes : This Field will give how many Likes the YouTube Videos have
Number of Dislikes : This Field will give how many Dislikes the YouTube Videos have.
Number of Comments : This Field will give how many Comments the YouTube Videos have
Video Id : This Field will show the unique YouTube Video Id for every video on YouTube
Hint: How do you fetch the YouTube Video Id?
In the below example, the highlighted section in the video URL refers to the Id for this particular video. Similarly, the Id will be displayed after equal sign in the URL for each video.
Step 3: Create a Salesforce Custom App
Next, you need to create an App. In case you were wondering, here’s the path for that.
Setup → Quick Find / Search → Create → App → Custom App
Step 4: Create a Custom Tab
Next step is to create a Custom Tab for that.
- Click Setup → Quick Find / Search → Create → Tabs → Custom Tabs → New
- In the Object Picklist, select the object that you have created and the tab style.
- Save the tab.
Step 5: Edit the YouTube Details Page
- From the Force.com App Menu on the extreme right corner, select your app and customize your App from the Navigation bar by clicking (+).
- Add YouTube Videos Tab to it.
- Edit the details page as given below in the image by using Edit Layout option.
Note: How do you send Http request and get it back from Google API?
Step 6: Get the API Key
- Click the URL https://console.developers.google.com to start creating a new project. For YouTube API you need an End point and API Key or OAuth. In the current example, I will be using API Key.
- Click Google’s Developer Console on the extreme left of the navigation bar.
- Click Credentials → create Credentials → API key → Server key → Create. This will generate the API key for this call, which gives access to the YouTube API. Since I am accessing the API call from Salesforce, this key is just a token to get the required details from YouTube.
Step 7: Enable YouTube Data API
For the second End Point, Select Google Developer Console → Overview → Google API → YouTube APIs → YouTube Data API and Enable it.
Step 8: Send Request and Get Response as Endpoint
- Click on Try this API in APIs Explorer → videos.list → part = statistics → id = YouTube Id → Authorize and Execute
- The result will give you the Request and API key in the format that needs to be used in the Custom Setting.
Step 9: Create Custom Settings
- Now you need to create Custom Setting for accessing YouTube API from Salesforce.
Setup → Quick Find / Search → Develop → Custom Setting → New
- Name the custom setting as YouTube API Setting.
- Create custom field as Value with DataType as Text Area.
- Click Manage in Custom Setting Definition Detail page and create two fields called YouTube Endpoints and YouTube API Key.
- In the Endpoint field, Paste the Request that was generated in the previous step from the YouTube details and also mention the statistics and Id.
- In the API field, Paste the API key that you got from Credentials (Custom settings are used for storing the API Key and encoded key).
Step 10: Create Remote Site Settings
- Call external API from Salesforce with the help of Remote Site Setting through Setup → Quick Find / Search → Security Controls → Remote Site Settings → New
- Remote Site Name: You__Tube__API__Setting
- Remote Site URL: https://www.googleapis.com and make it active.
Step 11: Create New Record
- On the navigation bar, click YouTube Videos and start creating a new record.
- Fill the Name and YouTube Id field, leave the other fields empty and save it.
Once saved, you can see that the default buttons available are Edit, Delete and Clone. In our case, the clone button is not relevant, instead of which you need a Get Details button.
Step 12: How to Create Get Details Button?
- Setup → Quick Find / Search → Create → object → (Custom object) YouTube Video → Buttons, Links and Actions → New Button or Link → Name the Button as Get Details
- To display Get Details button in the YouTube Video details page, go to YouTube Video Details page → Click on Quick Access menu on the extreme right.
- Click Edit Layout → Buttons → Drag and drop the Get Details button to the YouTube Details page.
Step 13: Create Apex Class
Create Apex Class in the name of YouTube Video Details by Setup → Quick Find / Search → Develop → Apex Classes → New
The Code for the class is given in this link
Clicking the Get Details button on the YouTube Details page should display the details of a particular video. Here are the steps for doing this.
2.For adding a code to the Get Details button, follow this path
Setup → Quick Find / Search → Create → objects → YouTubeVideo ( custom object that we created) → Buttons, Links, and Actions → Get Details. Then, add the code as shown in the below screenshot.
3.Click the Get Details button in the YouTube Details Page.
Step 15: Final Output
You final output will display the details for your chosen video – the number of View Counts, Likes, Dislikes and Comments as given below.
This is just one of the many instances where you can use an external APIs with Salesforce. If you have come across similar examples, do share them in the comments section below. It may become an inspiration for another fun Salesforce project!