2
votes

Apologies, since I may not know the terminologies for the salesforce API. I just started programming a connector to interact with salesforce and I am stuck.

I have a requirement, where each time a new entry is added to the Leads section, I will have to retrieve a couple of fields (Firstname and Product Code) and pass it to a different software that makes use of PHP.

<?php    
require "conf/config_cleverbridge_connector.inc.php";

require "include/lc_connector.inc.php";


// Start of Main program

// Read basic parameters
if ($LC_Username === "")
{
    $LC_Username  = readParam("USER");
}
if ($LC_Password === "")
{
    $LC_Password  = readParam("PASSWORD");
}
$orderID      = "";
$customerID   = substr(readParam("PURCHASE_ID"), 0, 10);
$comment      = readParam("EMAIL")."-".readParam("PURCHASE_ID");

// Create product array
$products = array();

$itemID     = readParam("INTERNAL_PRODUCT_ID");

$quantity   = 1;
if (!ONCE_PER_PURCHASED_QUANTITY)
{
    $quantity = readParam("QUANTITY");
}

// Add product to the product array
$products[] = array (
    "itemIdentification" => $itemID,
    "quantity"           => $quantity,
);

// Create the order
$order = array(
    "orderIdentification"    => $orderID,
    "customerIdentification" => $customerID,
    "comment"                => $comment,
    "product"                => $products,
);

// Calling webservice
$ticket = doOrder($LC_Username, $LC_Password, $order);
if ($ticket)
{
    Header("HTTP/1.1 200 Ok");
    Header("Content-Type: text/plain");
    print TICKET_URL.$result->order->ticketIdentification;
    exit;
}
else
{
    $error = "No result from WSConnector_doOrder";
    trigger_error($error, E_USER_WARNING);
    printError(500, "Internal Error.");
    exit;
}

// End of Main program

?>

Now this is the code that I got and have to work with. And this is hosted on a different remote server. I am very very new to salesforce and I am not really sure how to trigger calling this php file over a remote site.

The basic idea is: 1. New entry in Lead is created. 2. Immediately 2 fields (custID and prodID) are sent to this PHP file I have pasted above (some of the variables are different) 3. This does its processing and sends 2 fields back to salesforce.

Any help or guidance is appreciated. Even links to read up on is okay as I am completely clueless.

PS: I have another example where it makes use of JSON Messages if that may make any difference. Thanks

1
Check these 2: salesforce.stackexchange.com/questions/23977/…, stackoverflow.com/questions/13121688/web-hook-in-salesforce. Should give you at least idea which keywords to look for :) Write bit more whether it has to be instant or can it work in batches fired say every 15 minutes. Where you'd like to have most of the logic, in SF or are you more comfortable with PHP (will the PHP app be the only one that will rely on this interface or more to come in future?). What SF edition you're on (Enterprise)? What's the estimated daily amount of leads? - eyescream
Thank you for the links @eyescream gives me something to read now which might help me out in searching better. It has to be immediately after a new record in lead is created, so almost instantly (I wouldn't mind a slight delay). I wouldn't mind whether the logic is there within SF or in PHP although I am comfortable with PHP. This PHP App will reply only on this interface, and I am sure with this in place I can create more even if there a need for the future. Currently we have the enterprise version version of SF, although I am using the developer account to do all my development and testing. - Vivian Lobo

1 Answers

1
votes

I'll repost the links from my comment :)


If your PHP endpoint is visible on the open web (not a part of some intranet or just your own localhost) then simplest thing to do would be to send an Outbound Message from Salesforce. No coding required, just some XML document you'll have to parse on the PHP side. Plus it will automatically attempt to resend the messages if the host is unreachable...

If your app can't be accessed from SF servers then I think your PHP app will have to be the "actor". Querying SF every X minutes for new Leads or maybe subscribing to Streaming API... This will mean you'd have to store credentials to SF on your PHP app and remember to either change the password periodically or set on the "integration user"'s profile the "password never expires" checkbox.

So you're getting the notification, you generate your tickets, time to send them back. Will you want to pretend the update of Lead was done by the person that created it or will you want to see "last modified by: Integration User"? Outbound message can contain session id which you can use to act as the person who initiated the action (created the lead and fired the workflow) - at least until they log out or the session timeouts.

For message back you can use SOAP or REST salesforce apis - read the docs to figure out how to send an update command (and if you want to make it clear it was done by special user associated with this PHP app - how to log in to the APIs). I think the user's profile must have "API enabled" ticked before you could reuse somebody's session so maybe it's better to have a dedicated account for integrations like that...

Another thing to keep in mind if it'd be outbound messages is to ignore the messages sent from sandboxes so if somebody makes a test environment you will not call your "production" database of tickets. You can also remember to modify the outbound message and remote site setting every time a sandbox is made so you'll have "prod talking to prod, test talking to test". I know you can include user's session id in the OM - so maybe you can also add organization's id (for production it'll stay the same, every new sandbox will have new id).


The problem with this approach is that it might not scale. If 1000 leads is inserted in one batch (for example with Data Loader) - you'll get spammed with 1000 outbound messages. Your server must be able to handle such load... but it will also mean you're using 1 API request to send every single update back. You can check the limit of API requests in Setup -> Company Information. Developer Edition will have this limit very low, sandboxes are better, production is best (it also depends how many user licenses have you bought). That's why I've asked about some batching them up.

More coding but also more reliable would be to ask SF for changes every X minutes (Streaming API? Normal query? check the "web hook" answer) and send an update of all these records in one go. SELECT Id, Name FROM Lead WHERE Ticket__c = null (note there's nothing about AND LastModifiedDate >= :lastTimeIChecked)...