My company uses data.com premium. We want to create a trigger that will update a lead based on company information since the out-of-the-box functionality only matches based on the lead. (contact). I have learned how to write triggers/classes out of necessity (lone admin), but this seems to require someone with more experience than I have.
I have spent about 40 hours trying to piece together information from the Integration Workbook, the heroku Json2apex converter and many other articles and help documentation. Based on the example in the Integration Workbook I was able to write a trigger that passes a list of lead ids to a class.. The class seems to gather the right information for the JSON request (I think) but since the list of leads needs the lead ID to update the lead, and the info returned from the API does not include the ID.. I am just at a loss. I have asked for help from data.com but the (rightly) want to charge me for services. We were not aware that this was not out-of-the box functionality when we purchased the premium. Our bad.
Any help would be much appreciated. Currently this trigger is only attempting to populate the company name, my plan was to get this working, and then incorporate all of the other fields. Most of our leads will only contain company, email, phone, country for matching purposes, we want to return as much info as possible.
Class
public class JSON2Apex {
public static void consumeObject(JSONParser parser) {
Integer depth = 0;
do {
JSONToken curr = parser.getCurrentToken();
if (curr == JSONToken.START_OBJECT ||
curr == JSONToken.START_ARRAY) {
depth++;
} else if (curr == JSONToken.END_OBJECT ||
curr == JSONToken.END_ARRAY) {
depth--;
}
} while (depth > 0 && parser.nextToken() != null);
}
public class Attributes {
public String type {get;set;}
public Attributes(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'type') {
type = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Attributes consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Entities {
public Attributes attributes {get;set;}
public String City {get;set;}
public String Country {get;set;}
public String DunsNumber {get;set;}
public String Name {get;set;}
public String Phone {get;set;}
public String Street {get;set;}
public String Zip {get;set;}
public Entities(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'attributes') {
attributes = new Attributes(parser);
} else if (text == 'City') {
City = parser.getText();
} else if (text == 'Country') {
Country = parser.getText();
} else if (text == 'DunsNumber') {
DunsNumber = parser.getText();
} else if (text == 'Name') {
Name = parser.getText();
} else if (text == 'Phone') {
Phone = parser.getText();
} else if (text == 'Street') {
Street = parser.getText();
} else if (text == 'Zip') {
Zip = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Entities consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public List<Entities> entities {get;set;}
public MatchOptions matchOptions {get;set;}
public JSON2Apex(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'entities') {
entities = new List<Entities>();
while (parser.nextToken() != JSONToken.END_ARRAY) {
entities.add(new Entities(parser));
}
} else if (text == 'matchOptions') {
matchOptions = new MatchOptions(parser);
} else {
System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
public class MatchOptions {
public String fields {get;set;}
public MatchOptions(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'fields') {
fields = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'MatchOptions consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
@future (callout=true) // indicates that this is an asynchronous call
public static void postOrder(List<Id> leadIds) {
List<Lead> ld = [SELECT Id, Company, Country, phone, street, postalcode FROM Lead where Id IN :leadIds];
for (Lead l : ld) {
// Create a JSON generator object
JSONGenerator gen = JSON.createGenerator(true);
// open the JSON generator
gen.writeStartObject();
gen.writeStringField('Name', l.Company);
gen.writeEndObject();
// close the JSON generator
// create a string from the JSON generator
String jsonOrders = gen.getAsString();
// debugging call, which you can check in debug logs
System.debug('jsonOrders: ' + jsonOrders);
// create an HTTPrequest object
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://cs15.salesforce.com/services/data/v32.0/match/DunsRightMatchEngine/DatacloudCompany/DunsRightMatchRule');
req.setHeader('Content-Type', 'application/json');
req.setBody(jsonOrders);
// create a new HTTP object
Http http = new Http();
// create a new HTTP response for receiving the remote response
// then use it to send the configured HTTPrequest
HTTPResponse res = http.send(req);
{
// Retrieve all of the Lead records
// originally passed into the method call to prep for update.
List<Lead> leads =
[SELECT Id FROM Lead WHERE Id IN :ld];
// Create a list of entities by deserializing the
// JSON data returned by the fulfillment service.
List<Entities> ddcdata =
(List<Entities>)JSON.deserialize(res.getBody(),
List<Entities>.class);
// Create a map of Lead IDs from the retrieved
// invoices list.
Map<Id, Lead> leadMap =
new Map<Id, Lead>(leads);
for ( Entities ddc : ddcdata ) {
Lead lead = leadMap.get(ddc.Id);
lead.Company = String.valueOf(ddc.Name);
}
// Update all leads in the database with a bulk update
update leads;
}
}
}
}
Trigger
trigger LeadDDCAccountMatch on Lead (after insert) {
List<Id> leadIds = new List<Id>();
for (Lead lead: Trigger.new) {
if (lead.status == 'Cold'){
leadIds.add(lead.Id);
}
}
if (leadIds.size() > 0) {
Lead_fromJSON.postOrder(leadIds);
}
}
Info needed in the JSON request
{
"entities": [
{
"attributes": {
"type": "DatacloudCompany"
},
"City": //city from lead
"Country": //country from lead
"DunsNumber": //companydunsnumber from lead
"Name": //company name from lead
"Phone": //phone from lead
"Street": //street from lead
"Zip": //postalcode from lead
}
],
"matchOptions": {"fields": "City, CompanyId, Country, Description, DunsNumber, Name, Website, YearStarted, Zip"
}
}
company = [select id from company where name LIKE : l.Company];