Email Address Validation with External Services


Are you tired of hearing about Email Bounces from your Marketing Users ? Are you struggling with Email Address validation ? Are you loosing your potential Leads because they didn’t give you the correct Email Address ? If you have faced any of the above mentioned difficulties, I know how it feels like being a Salesforce Administrator.

As a Salesforce Administrator, you can implement Validation Rules and other custom Validations to ensure the quality of the Email Address. But at the end of the day, these options have their own limitations. So how can we resolve this trivial problem ? And most importantly, in a Admin friendly manner. In other words, with less or no code at all.

Email Address Validation APIs

Let’s welcome – Email Address Validation APIs. I know you have probably started to freak out when you read the word API. No, don’t be. There are a lot of services out there that offers Email Address Validation. 3 of them that got my attention are –

  1. https://www.mailgun.com/email-validation
  2. https://mailboxlayer.com/
  3. https://neverbounce.com/

Well, in this blog post let’s look at integrating Mailgun Email Validation API with Salesforce using External Services. So in fact, this is the 4th installment in the External Service Series. In case if you want to read more, please take a look at the below links –

  1. https://shrutisridharan.wordpress.com/2018/10/17/integrate-ibm-watson-tone-analyzer-api-with-salesforce-without-any-apex/
  2. https://shrutisridharan.wordpress.com/2018/04/12/introduction-to-external-services-2/
  3. https://shrutisridharan.wordpress.com/2018/02/12/introduction-to-external-services-part-1/

Without further ado let’s start building it!

Exploring the Mailgun Email Address Validation API

At first, sign-up for Mailgun service here – https://signup.mailgun.com/new/signup. After completing the sign-up,  you will be navigated to the Dashboard. Now, click on the Email Validation tab and copy the Public Validation Key like as shown below –

Public Validation Key

Now, let’s look at the API that needs to be fired with the Public Validation Key and Email Address for validation. The API endpoint that needs to be used for the validation is shown below –

https://api.mailgun.net/v3/address/validate?address=shruti.sridharan22@gmail.com&api_key=<PASTE THE PUBLIC VALIDATION KEY HERE>

Simply supply the address and the api_key as URL parameters and you should be golden! The response upon invoking the API successfully would look like this =

{
    "address": "shruti.sridharan22@gmail.com",
    "did_you_mean": null,
    "is_disposable_address": false,
    "is_role_address": false,
    "is_valid": true,
    "mailbox_verification": "true",
    "parts": {
        "display_name": null,
        "domain": "gmail.com",
        "local_part": "shruti.sridharan22"
    },
    "reason": null
}

Now what’s important here is the is_valid node. A value of true indicates that the Email Address is correct.

Well that’s with the API. Now in order to invoke this from Salesforce, we will employ External Service. But for External Service to work, we need to write a Swagger Schema for the same. Let’s take a stab at it.

Swagger Schema for the Mailgun Email Address Validation API

Here is your Swagger Schema for the above mentioned API –

{
    "swagger": "2.0",
    "info": {
        "description": "Mailgun Email Address Validation API",
        "version": "3.0",
        "title": "Mailgun Email Address Validation API"
    },
    "host": "api.mailgun.net",
    "schemes": [
        "https"
    ],
    "paths": {
        "/v3/address/validate": {
            "get": {
                "summary": "Validate an Email Address",
                "produces": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "in": "query",
                        "name": "address",
                        "description": "Email Address to be Verified",
                        "required": true,
                        "type": "string"
                    },
                    {
                        "in": "query",
                        "name": "api_key",
                        "description": "API Key",
                        "required": true,
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Result of the Email Address Validation",
                        "schema": {
                            "$ref": "#/definitions/validationResponse"
                        }
                    }
                }
            }
        }
    },
    "definitions" : {
        "validationResponse" : {
            "properties": {
                "is_valid": {
                    "description": "is_valid",
                    "type": "boolean"
                }
            }
        }
    }
}

No, it’s not tough. If you are NOT able to decipher that (which you really don’t need to), you can take a brief look at the previous External Service blog posts.

As opposed to the previous blog posts, a new addition here is the introduction of complex schema/object as the output. The return type when the API call was successful (200) looks like this =

"validationResponse" : {
    "properties": {
        "is_valid": {
            "description": "is_valid",
            "type": "boolean"
        }
    }
}

As mentioned earlier, since we are looking only at the is_valid node, we don’t really have to care about the other nodes in the JSON response.

Registering the External Service

Before we register, make sure we have a Named Credential created with this URL –

https://api.mailgun.net

Now, switch to Lightning Experience and from the Setup, click on External Services. Hit Add an External Service and then copy and paste the Swagger Schema after selecting the checkbox – Provide Complete Schema. At the end, you should be able to see something like this =

External Service Actions.png

Voila! That’s like completing 70% of the whole thing. Registering an API is so simple that it hurts.

Creating a Flow to use the External Service

Let me break the suspense right away! Your Flow would be as simple as this =

Flow.png

Let’s look at each of the elements in the Flow.

1. Screen – Email Address Validation

The screen will just have a Text Box.

Screen - Email Address Validation.png

2. Apex Action (available in Flow as result of the External Service registration)

ES - Validate Email Address

 

Set the address to the Screen Input Field and the api_key would be the Public Validation Key.

Wait! We need to configure the Outputs also. The response from the API (out of which we are looking ONLY for the is_valid) will be collected in a Variable of type – Boolean.

ES - Validate Email Address OP.png

3. Screen – Validation Result

All you need to do is to add a Display Text and print the value within the Variable that was created in Step 2 to hold the response from the API.

Result Screen.png

That’s all! Now let’s see it in action.

Flow in Action

We can always expand the Flow by updating a checkbox on the Lead record or so to indicate the validation status. Trust me – API Integration has never become so simple!

Image Credits: salesforce.com, Mailgun
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s