Got Data? A Salesforce and Rapleaf Mashup

Salesforce-Rapleaf Mashup – A 4:00 Overview

Any company with customers and/or prospective customers ought to know about customer segmentation.  I mean all customers (and prospective customers) are not equal, right?  Some are older, some are younger.  Some live here, some live over there.  They have different interests, backgrounds, and even purchasing histories.  Customer segmentation gives you the ability to tailor targeted messaging to specific channels of your business, or even to develop algorithms to predict customer retention, among numerous other benefits.  Anyone with a database of customers and prospects should be employing some level of customer segmentation.

This thinking should naturally lead to the questions:

  • What information about my customers and/or prospects do I need?
  • Where do I find this data?
  • What if I only have a limited amount of information to start with?
  • How long will it take to gather this info?

After thinking through these questions myself, and doing some research, I found a company named Rapleaf.  Rapleaf’s proclaimed mission is to “create a more personalized world”.  They do this by aggregating a potentially large amount of data and building a profile around email addresses.  This is unique because the only piece of information that you really need in order to access their database is an email address.  Since an email address is something that we typically have for most of our customers and prospects this service looks like it could be pretty valuable.   This led me to a few more questions such as, “What type of information can I get?”, “How much will it cost”?, “How accurate is Rapleaf’s data”?, and “How can I integrate it with Salesforce”?

I checked out the API docs to understand how to access Rapleaf’s database, and after trading a few emails  with Rapleaf’s Co-Founder Manish Shah I was able to get a full listing of potential data and pricing.  Setting up a free account with Rapleaf will get you an API Key and access to unlimited age, gender, and location data queries.  There are many additional fields available, and since it’s my understanding that Rapleaf is working on revising their pricing, you can just contact them and they’ll be happy to go over the cost structure with you. 

I searched the Salesforce community and didn’t find any existing code for integrating with Rapleaf so I decided to build something myself in Apex and share it.  There are three classes that handle the Rapleaf mashup, Rapleaf, RapleafResponse, and RapleafSetup.  I’ve also included a sample trigger for fetching Rapleaf data when a new lead is inserted.  Test classes are included, as well.  The full source code can be downloaded here. It basically works like this:


Method Arguments Return Type Description
searchRapleaf String email, String hash RapleafResponse This method takes an email address and returns the RapleafResponse with data. Hash is an optional parameter used for hashing the email address for security. Valid values are ‘MD5′ or ‘SHA1′


Method Arguments Return Type Description
getAge String Returns the Age field corresponding to the supplied email address, as received from the Rapleaf database
getGender String Returns the Gender field corresponding to the supplied email address, as received from the Rapleaf database
getLocation String Returns the Location field corresponding to the supplied email address, as received from the Rapleaf database


Variable Name Value Description
API_KEY String your Rapleaf api key Paste your api key. It will be used to construct the HTTP Request.
PERSONALIZATION_URL ‘’ This is the base URL that is used to construct the endpoint for the HTTP Request. It might be subject to change, and if it does, you’ll just need to change it here.
FORMAT ‘xml’ Defines the type of HTTPResponse that you want Rapleaf to send. By default, Rapleaf sends a JSON response.
METHOD ‘GET’ The HTTP method used to send a request. Currently, Rapleaf just supports the GET method.

Below is an example of a sample trigger used to implement the Rapleaf mashup.

trigger LeadTrigger on Lead (after insert)

public class LeadManager {

	public static boolean isTest = false;
	public static String hash = null;

	 * This method handles after insert logic for leads.  Here, it creates a Set of Lead Id's from the trigger and calls the futureRapleafCall() method.
	 * @param newList   A list of leads passed in from Trigger.New.
	public static void handleNewLeadsAfterInsert(List<Lead> newList)
		Set <Id> leadIds = new Set <Id>();
		for(Lead lead : newList)
			if(lead.Email != '')

	 * This is the asynchronous method that allows the program to make callouts from the trigger.  It constructs a new Rapleaf object for each lead and calls
	 * the searchRapleaf() method.  Then, it assigns values from the response to the appropriate Lead field.
	 * @param ids   A set of Ids that is passed in from the handleNewLeadsAfterInsert() method above.
	@future (callout=true)
	private static void futureRapleafCall(Set<Id> ids)
		Map<Id,Lead> leadMap = new Map<Id,Lead>([SELECT Lead.Age__c, Lead.Gender__c, Lead.Location__c, Lead.Email, Id
								 FROM Lead
								 WHERE Id
								 IN :ids]);
		for(Id id : ids)
			Lead lead = leadMap.get(id);
				Rapleaf rap = new Rapleaf();
				// System.debug('MADE A NEW RAPLEAF OBJECT');
				// System.debug('TEST: ' + isTest);
				if(isTest) rap.isTest = true;
				// System.debug('RAP TEST: ' + rap.isTest);
				RapleafResponse resp = rap.searchRapleaf(, hash);
				// System.debug('RAPLEAFRESPONSE: ' + resp);
				lead.Age__c = resp.getAge();
				// System.debug('LEAD AGE: ' + lead.Age__c);
				lead.Location__c = resp.getLocation();
				lead.Gender__c = resp.getGender();
				catch(RapleafResponse.RapleafException e)
					System.debug('ERROR: ' + e);

		update leadMap.values();

	public static void handleNewLeadsTest(List<Lead> testList)
		isTest = true;

As always, I look forward to any feedback, thoughts, and/or suggestions. If you’re using a similar service to collect this type of data I’d love to hear about that, too.


Got Data? A Salesforce and Rapleaf Mashup

7 Responses

  1. This is really interesting: thank you. I have downloaded the code and look forward to trying it out.