Leverage Cloud-Computing, Saas, and Better Business Processes to Drive Change in Your Industry

Clint Lee

Subscribe to Clint Lee: eMailAlertsEmail Alerts
Get Clint Lee: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Cloud Computing, Salesforce.com Journal

Cloud Computing: Blog Post

Got Data? A Salesforce and Rapleaf Mashup

Code and video demo

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:

Rapleaf

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'

 

RapleafResponse

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

 

RapleafSetup

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 'https://personalize.rlcdn.com/v4/dr?' 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.

[java]
trigger LeadTrigger on Lead (after insert)
{
LeadManager.handleNewLeadsAfterInsert(Trigger.New);
}
[/java]

 

[java]
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 != '')
{
leadIds.add(lead.Id);
}
}
futureRapleafCall(leadIds);
}

/**
* 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);
try
{
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(lead.email, 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;
handleNewLeadsAfterInsert(testList);
}
}
[/java]

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.

More Stories By Clint Lee

Clint is a Principal and Founder of The Flywheel Group, a premier provider of innovative solutions to the franchise industry. The FranchiseFlywheel™ application was developed out of the industry's need for a cutting-edge and affordable franchise management tool for franchisors. Spending several years in the franchise industry directing daily activities related to franchise and business development processes, marketing, lead-generation, and contract administration, and an understanding of the needs of peers and colleagues, led to the development of the application. The Flywheel Group is focused on improving best practices and driving change in the franchise industry by introducing industry-leading solutions and working with its clients to re-engineer business processes that will ultimately enhance revenue and improve bottom line efficiency. Clint actively writes and shares his thoughts on the Flywheel Blog.