Salesforce Rest API Integration with Veriphone
In this blog, you will get to know how you can integrate your Salesforce org with Veriphone (Phone number validator tool).
First, you need to create a custom field that will store the status of the phone whether it is valid or not, and an account of Veriphone.
Sign up for the Veriphone account and after successfully signing up, you will get the API key and 1000 free credits to test the API of Veriphone.
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.veriphone.io
Don't forget to check out: What Are The Benefits of Implementing Salesforce and eCommerce Integration?
Now navigate to the Developer Console of your org and 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; } }
Below is the class which is 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); } }
I am performing the Veriphone phone validation after the contact creation so I have created a trigger as well to achieve the same:
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); } }
Check out another amazing blog by Udit here: Salesforce Rest API Integration with Neverbounce
Now create a new contact with a phone number and save it. If that number is valid then you will get the valid result and if not then invalid results will be returned. After saving the record, you need to refresh the page once or twice in order to reflect the result on the UI part.
You can check the whole response by setting up the debug logs in your org.
Note: When entering the phone, make sure you are providing the country code with the phone number as well in order to get the correct desired status of the phone. If you don’t do so then the phone number will be saved in the phone format like (123)-456-7890 and this won’t return the correct results because this format is not accepted in Veriphone’s endpoint URL.
As callout is made from the trigger and if the trigger has 200 records in a transaction will the governor limit of 100 callouts in the transaction will hit?