Salesforce Rest API Integration with Neverbounce
Let’s learn how you can integrate our Salesforce org with Neverbounce and get the status of the emails of Contact records.
First, you need to create a custom field that will store the status of the email whether it is valid or not, and an account of Neverbounce.
Sign up for the Neverbounce account and navigate to the Apps section and here you can create an application that will provide the private key for our integration.
Steps to create an app in Neverbounce:
- Click on Apps > Create New App
- As you are going to integrate it with Salesforce, select Custom Integration and provide the name of the app and click Create App
- After the app creation, you will get the private key at the very top of the page.
Now on the Salesforce side, create a field that is going to store the response which you will get after hitting the API and also in Remote Site Settings, create a remote site with the name you want and the URL will be: https://api.neverbounce.com
Don't forget to check out: Low-Code Salesforce REST API Integration - All You Need to Know
Now navigate to the Developer Console of your org and create a REST Class:
public class NeverBounceSalesforce { @future(callout=true) public static void retrieveEmailStatus(set<Id> contactId) { String apiKey = '*********************************'; //Neverbounce API Key here String username = '*********************************'; //Neverbounce API Key here String password = '**************'; List<Contact> contactsWithEmail = [Select Id, Email,Validate_Email_NB__c, Email_Status__c from Contact where Id=:contactId AND Email != Null]; System.debug('Contact details ====='+contactsWithEmail); System.debug('Email details====='+contactsWithEmail[0].Validate_Email_NB__c); List<Contact> contactsToValidate = new list<Contact>(); For(Contact cont:contactsWithEmail) { Http h = new Http(); HttpRequest req = new HttpRequest(); req.setHeader('Content-type', 'application/json'); req.setMethod('GET'); String url = 'https://api.neverbounce.com/v4/single/check?key='+apikey+'&email='+cont.Email; req.setEndpoint(url); System.debug('Value of Endpoint =====' +url); Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue); System.debug('Value of header value =====' +headerValue); req.setHeader('Authorization', authorizationHeader); System.debug('Working fine till here AUTHORIZATION HEADER ====='+authorizationHeader); Http http = new Http(); HttpResponse resp = http.send(req); System.debug('response body is ===== '+resp.getBody()); //Calling NBClass to deserialize the response NBClass nbc = (NBClass)System.JSON.deserialize(resp.getbody(), NBClass.CLASS); System.debug('Result is ====='+nbc.result); cont.Email_Status__c = nbc.result; contactsToValidate.add(cont); } update contactsToValidate; } }
Below is the class which is parsing the JSON response:
//JSON to APEX class for NeverBounceSalesforce Class public class NBClass { public String status; public String result; /*public List<Flags> flags;*/ public String suggested_correction; public Integer execution_time; /*public class Flags { }*/ public static NBClass parse(String json) { return (NBClass) System.JSON.deserialize(json, NBClass.class); } }
Check out another amazing blog by Udit here: All You Need to Know About Salesforce Marketing Cloud Admin Settings
I am performing the Neverbounce email validation after the contact creation so I have created a trigger as well to achieve the same:
trigger NeverBounceSalesforceTrigger on Contact (after insert) { Set<Id> setOfContactID = new Set<Id>(); For(Contact con : Trigger.new) { setOfContactID.add(con.Id); } If(!setOfContactID.isEmpty()) { NeverBounceSalesforce.retrieveEmailStatus(setOfContactID); } }
After implementing all the above classes and tasks, create a new Contact with an email address and save it. You need to refresh the page once or twice to get the status of the email in the custom field you defined above. As it is working after the insert of the record, you can check the result in debug logs as well and in order to reflect it on the UI, you need to refresh the page.
Responses