In this blog, you’ll discover how to seamlessly integrate your Salesforce organization with Veriphone, a phone number validation tool.
To begin, create a custom field within Salesforce to store the phone number’s validation status. Additionally, you’ll need to set up an account with Veriphone.
Once you’ve signed up for Veriphone and successfully registered, you’ll receive an API key and 1000 free credits to test the Veriphone API.
Moving on to the Salesforce side, create a field that will store the response received after making API calls. Additionally, configure the Remote Site Settings by creating a remote site with your preferred name and set the URL to: https://api.veriphone.io
Next, access the Developer Console of your organization and proceed to create a REST Class:
//Veriphone Integration with Salesforce public class VeriphoneSalesforce { @future(callout=true) public static void retrievePhoneStatus(set<id> contactId) { String Key = '*************************' //API Key here; String username = '*************************'; //API Key here String password = '*************************'; // Veriphone’s password List<Contact> contactsWithPhone = [Select Id,Phone,Phone_Status__c from Contact where Id IN:contactId AND Phone != Null]; System.debug('Contact details ====='+contactsWithPhone); System.debug('Email details====='+contactsWithPhone[0].Phone); List<Contact> contactsToValidate= new List<Contact>(); For(Contact con:contactsWithPhone) { HttpRequest req = new HttpRequest(); req.setHeader('Content-type', 'application/json'); req.setMethod('GET'); String url = 'https://api.veriphone.io/v2/verify?phone='+con.Phone+'&key='+Key; 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); //HTTPResponse res = http.send(req); Http http = new Http(); HttpResponse resp = http.send(req); System.debug('response body is ===== '+resp.getBody()); //Calling VeriphoneJSON2Apex to deserialize the response VeriphoneJSON2Apex vp = (VeriphoneJSON2Apex)System.JSON.deserialize(resp.getbody(), VeriphoneJSON2Apex.CLASS); system.debug('vp**'+vp); System.debug('Result is ====='+vp.status); System.debug('Result is car--'+vp.carrier); System.debug('Result is ==validd--'+vp.phone_valid); con.Phone_Status__c = vp.status; con.IsPhoneCorrect__c = vp.phone_valid; contactsToValidate.add(con); } update contactsToValidate; } }
Here is the class responsible for parsing the JSON response:
public class VeriphoneJSON2Apex { public String status; public String phone; public Boolean phone_valid; public String phone_type; public String phone_region; public String country; public String country_code; public String country_prefix; public String international_number; public String local_number; public String e164; public String carrier; public static VeriphoneJSON2Apex parse(String json) { return (VeriphoneJSON2Apex) System.JSON.deserialize(json, VeriphoneJSON2Apex.class); } }
To perform the Veriphone phone validation after creating a contact, I have implemented a trigger to achieve this functionality.
trigger PhoneValidationTrigger on Contact (after insert) { // working line of code set<id> setofContactid=new set<id>(); for(Contact con:trigger.new){ setofContactid.add(con.id); } if(!setofContactid.isEmpty()){ VeriphoneSalesforce.retrievePhoneStatus(setofContactid); } }
After creating a new contact with a phone number and saving it, the result will depend on whether the number is valid or not. A valid number will yield a positive result, while an invalid number will produce an invalid result. To see the updated status on the UI, you may need to refresh the page once or twice.
To review the entire response, set up debug logs in your Salesforce org.
It’s essential to note that when entering the phone number, ensure that you include the country code to obtain the correct status of the phone. Failing to do so will save the phone number in a specific format like (123)-456-7890, which may not be recognized by Veriphone’s endpoint URL, resulting in incorrect results