With the new SalesPerson APIs, you can freely associate Marketo leads to SalesPerson records in instances without a native CRM integration. This allows usage of {{lead.Lead Owner Email Address}} and related fields and tokens within Marketo.
Creating SalesPerson records
In order to associate leads to SalesPerson records, we first need to input our SalesPerson records into Marketo. This is done with the Create/Update/Upsert SalesPerson endpoint. Here’s an example class in PHP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php class SalesPerson{ private $auth;//auth object private $action;// string designating request action, createOnly, updateOnly, createOrUpdate private $dedupeBy;//dedupeFields or idField private $input;//array of salesperson objects for input //takes an Auth object as the first argument public function __construct($auth, $input){ $this->auth = $auth; $this->input = $input; } //constructs the json request body private function bodyBuilder(){ $body = new stdClass(); $body->input = $this->input; if (isset($this->action)){ $body->action = $this->action; } if (isset($this->dedupeBy)){ $body->dedupeBy = $this->dedupeBy; } return json_encode($body); } //submits the request to Marketo and returns the response as a string public function postData(){ $url = $this->auth->getHost() . "/rest/v1/salespersons.json?access_token=" . $this->auth->getToken(); $ch = curl_init($url); $requestBody = $this->bodyBuilder(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); curl_getinfo($ch); $response = curl_exec($ch); curl_close($ch); return $response; } //getters and setters public function setAction($action){ $this->action = $action; } public function getAction($action){ return $this->action; } public function setDedupeBy($dedupeBy){ $this->dedupeBy = $dedupeBy; } public function getDedupeBy($dedupeBy){ return $this->dedupeBy; } public function addSalesPersons($input){ if($this->input != null){ if (is_array($input)){ foreach($input as $item){ array_push($this->input, $item); } }else{ array_push($this->input, $input); } }else{ $this->input = $input; } } public function getInput(){ return $this->input; } } |
In the above class, the $input variable is an array of stdClass objects with possible members:
- externalSalesPersonId(only valid on create)
- id(only as key in updateOnly mode)
- fax
- firstName
- lastName
- mobilePhone
- phone
- title
More detailed information about types and fields lengths can be retrieved through the Describe SalesPerson call.
Synching Leads
Here’s a quick example class for synching the leads we need:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<?php class Leads{ private $auth;//auth object private $action;// string designating request action, createOnly, updateOnly, createOrUpdate private $lookupField;//dedupeFields or idField private $input;//array of salesperson objects for input private $asyncProcessing;//specify whether input should be processed asynchronously private $partitionName;//partition name for update if requires //takes an Auth object as the first argument public function __construct($auth, $input){ $this->auth = $auth; $this->input = $input; } //constructs the json request body private function bodyBuilder(){ $body = new stdClass(); $body->input = $this->input; if (isset($this->action)){ $body->action = $this->action; } if (isset($this->lookupField)){ $body->lookupField = $this->lookupField; } return json_encode($body); } //submits the request to Marketo and returns the response as a string public function postData(){ $url = $this->auth->getHost() . "/rest/v1/leads.json?access_token=" . $this->auth->getToken(); $ch = curl_init($url); $requestBody = $this->bodyBuilder(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); curl_getinfo($ch); $response = curl_exec($ch); curl_close($ch); return $response; } //getters and setters public function setAction($action){ $this->action = $action; } public function getAction(){ return $this->action; } public function setDedupeBy($lookupField){ $this->dedupeBy = $lookupField; } public function getDedupeBy(){ return $this->dedupeBy; } public function addLeads($input){ if($this->input != null){ if (is_array($input)){ foreach($input as $item){ array_push($this->input, $item); } }else{ array_push($this->input, $input); } }else if (is_array($input)){ $this->input = $input; }else{ $this->input = [$input]; } } public function getInput(){ return $this->input; } public function setAsyncProcessing($asyncProcessing){ $this->asyncProcessing = $asyncProcessing; } public function getAsyncProcessing() { return $this->asyncProcessing; } public function setPartitionName($partitionName){ $this->partitionName = $partitionName; } public function getPartitionName() { return $this->partitionName; } } |
Process
Here’s an example creating two salesperson records and associating them to two lead records:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php require 'Auth.php'; require 'SalesPerson.php'; require 'Leads.php'; //Create an Auth object for authentication $auth = new Auth("https://284-RPR-133.mktorest.com", "7f99bd49-0e0e-4715-a72a-0c9319f75552", "O5lZHhrQDfDKRhulY89IU42PfdhRSe6m"); //Compose new SalesPerson Records $sales1 = new stdClass(); $sales1->externalSalesPersonId = "SalesPerson 1"; $sales1->email = "sales1@example.com"; $sales1->firstName = "Jane"; $sales1->lastName = "Doe"; $sales2 = new stdClass(); $sales2->externalSalesPersonId = "SalesPerson 2"; $sales2->email = "sales2@example.com"; $sales2->firstName = "John"; $sales2->lastName = "Doe"; //Compose lead records $lead1 = new stdClass(); $lead1->email = "marketoDev@example.com"; //Add the external id of the desired salesperson $lead1->externalSalesPersonId = $sales1->externalSalesPersonId; $lead2 = new stdClass(); $lead2->email = "devBlog@example.com"; $lead2->externalSalesPersonId = $sales2->externalSalesPersonId; //Create a new instance of SalesPerson to submit records $salesPerson = new SalesPerson($auth, [$sales1, $sales2]); $spResponse = $salesPerson->postData(); print_r($spResponse . "rn"); $json = json_decode($spResponse); //Check for success on synching SalesPersons if ($json->success){ $leads = new Leads($auth, [$lead1, $lead2]); $leadsResponse = $leads->postData(); print_r($leadsResponse . "rn"); }else{ print_r("SalesPerson request failed with errors:rn"); foreach ($json->errors as $error){ print_r("code: " . $error->code . ", message: " . $error->message . "rn"); } } |
For our example classes, we’re just creating stdClass objects to represent our SalesPerson and Lead records which need to be synched, with each desired field added as a member. After execution of this code, the leads marketoDev@example.com and devBlog@example.com will both have the Lead Owner Email, Lead Owner First Name, and Lead Owner Last name fields populated, affording them the ability to use the relevant tokens for those fields and be filtered by the relevant smart list filters.
Auth Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php class Auth{ private $host;//host of the target Marketo instance private $clientId;//client Id private $clientSecret;//client secret private $token;//access_token private $expiry; function __construct($host, $clientId, $clientSecret){ $this->host = $host; $this->clientId = $clientId; $this->clientSecret = $clientSecret; } public function getToken(){ if (!isset($this->token) || $this->expiry > time()){ $data = $this->getData(); $this->expiry = time() + $data->expires_in; $this->token = $data->access_token; return $this->token; }else{ return $this->token; } } private function getData(){ $url = $this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json')); $response = curl_exec($ch); $json = json_decode($response); curl_close($ch); return $json; } public function getHost(){ return $this->host; } } |