Salesforce tips, techniques, and news.

By Sabra Rathbun  Posted on  September 23rd, 2022  in  Salesforce

Connect Google Ads Lead Form Extension to Salesforce

Lead Form extensions are one of the newest extension types Google Ads has introduced. These extensions allow you to capture leads when people submit a lead form directly on your ad. You have the option to download your leads in a CSV file or create a webhook integration to receive them directly in your CRM. Today, we're going to demo how to create a custom webhook to capture those leads in Salesforce.

Setting Up Your Salesforce Org

There are just a few things that need to be set up in Salesforce before establishing your webhook. You’ll be creating a custom metadata type, a new site, and a custom object that can log any errors thrown from your webhook.

Create a Custom Metadata Type

Using a custom metadata type to store relevant information for your webhook rather than hardcoding this information in Apex allows for an easier way to update the webhook key and optional email. You can name this anything you’d like. For simplicity, we named our’s Google Leads Webhook Settings. This will hold your Google lead form secret, and if you want to send an email notification upon creation of the lead, an email address.

salesforce webhook custom metadata type

After creating the custom metadata type, you will then create a record for it. Our record is labeled “Default Settings,” which we will query for and reference in our Apex class. The record will store your Google lead key that you’ll create via any random key generator you choose and will store the email address you would like the notification sent to, if you chose to send a notification.

salesforce webhook custom metadata type record

Custom Object for Error Logging

In order to log any errors you may experience when using your webhook, you will want to create a custom object to log those errors. For this example, the error logging object’s name is “Google Lead Webhook Error.” You will create one custom text field that will be used to store the error message received. After creating this object, you are ready to set up your Apex controller.

salesforce custom error object

Apex Controller for Your Webhook

Now that your org is set up, you’ll need to create an Apex class that will create a lead and, optionally, send an email notification.

Create a class that uses Rest Resource that has a URL map to the Lead object. 

@RestResource(urlMapping='/Lead/*')
global with sharing class googleLeadsWebhook {}

Next, you will create an httpPost method that will retrieve the lead information sent for your Google Ads Lead Form.

@HttpPost 
    global static void handleNotification() { 
       String requestBody = RestContext.request.requestBody.toString();  
    }

Inside of this method, you will grab the body from the request and parse through the body's data. After parsing through the data and performing some error handling, you'll create a new Lead record, populate the information with the lead information in the request body, and insert the new record into your org.

Optional: Send a New Lead Email Notification

If you want to receive an email notification every time a new lead is added from the lead form, you'll need to create an additional method to do so. You'll perform a query on your custom metadata type record to grab your notification email address. From there, you'll populate the necessary information to send an email through Apex using the Messaging class. The Messaging class allows you to set the "Reply to" email address, which allows you to reply directly to the person that submitted the lead form. You can add any necessary information such as the lead's name, phone number, description, etc. into the body of the email. After customizing the email, your work in Apex is finished, and you're ready to create the custom website.

Custom Site

Your custom site will bridge the gap between Google Ads and Salesforce. After labeling and naming your site, set up your site with the settings displayed in the image below.

salesforce webhook site details

Save and activate your new site and copy the Site URL to use later on when setting up your webhook in Google Ads.

After saving, select the "Public Access Settings" button. This will take you to the profile that was created for this site. Give this profile access to your Apex class. If you're sending a notification email, give the profile access to send email.

Setting Up A Google Lead Form Extension

In the Ads & Extensions section of Google Ads, go to Extensions. Click the + button to add a new extension and select Lead Form Extension. You can add the extension to your entire account or just a campaign. Select Create New and fill out the desired fields. Note: You will need a URL that links to your company's Privacy Policy to create a lead form extension.

Saving Your Webhook and Key on the Google Lead Form

At the bottom of the lead form you just created, there is a section labeled “Export leads from Google Ads.” Under “Other data integration options” you will insert your Site URL from the site you created in Salesforce into the webhook URL section and insert your key that is saved in your custom metadata type.

google ads lead form extension export integration salesforce

After inserting those, you will select “Send test data” at the bottom, and you will either receive a success message or a failure message. After receiving your success message, a new test lead should be generated in your Salesforce org and your optional notification email should be sent.  

Conclusion

Lead Form extensions from Google Ads are a quick and easy way to obtain leads directly from your ad. With a little help from Apex and a webhook, you'll be automating lead creation in no time.

Need help with your Salesforce digital transformation? Contact us today to discuss Salesforce consulting, implementation, development, and support!

Sabra Rathbun thumbnail
Sabra Rathbun

Sabra is an enthusiastic and dependable application developer who is motivated by helping clients and coworkers succeed. Her positive attitude, accommodating nature, and consistency make her a valuable team member and a reliable developer.