Many Pies

Many Pies

Friday, April 20, 2018

Free Salesforce postcode checker

While you can get Salesforce apps that check postcodes for you, here's a bit of Javascript that will check a UK postcode for you using the postcodes.io API and tell you if it's valid or not. It's not clever enough to suggest what the postcode might be, but then it is free.

Create a new button on the Contact object (Display Type: Detail Page Button, Behaviour: Execute JavaScript) and put this javascript on it:

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
var postcode = '{!Contact.MailingPostalCode}'; 
if (postcode != '') { 
postcode = postcode.replace(' ','%20'); 
sforce.connection.remoteFunction({ 
url : 'http://api.postcodes.io/postcodes/'+postcode+'/validate', 
async : true, 
cache : false, 
method: "GET", 
onSuccess : function(response) { 
if (response.slice(-5) == 'true}') { 
alert('OK'); 
} else { 
alert('Wrong - check using https://www.royalmail.com/find-a-postcode'); 
} 
}, 
onFailure : function(response) { alert('ERROR: '+response); } 
}); 
}
For the Account object use
{!Account.BillingPostalCode}

or
{!Account.ShippingPostalCode}
as appropriate.

No comments: