Salesforce Emails as Tasks
From ESM Wiki
Contents |
Introduction
This document provides an example integration of Email Stream Management with Salesforce CRM system. The idea is to have each mail from/to a Contact with an open Opportunity in Salesforce to be automatically recorded as Task for the corresponding Opportunity. Tasks are displayed on home page of Salesforce account thus a Manager can capture and review all communication of his Stuff with Customers. Stuff are not required to lean CRM and to record their communication manually - everyting is done automatically on network (Mailspect+MTA) level. Stuff use only their favorite mailers (MUA). This concept is illustrated on the following diagram:
MPP configuration options for this example intergration are provided in example config file. It contains only relevant options some of which contains example values. Additional efforts will be required to modify system and credential specific information and to add options for other features for entire config to start working on a specific installation. Relevant options are described step-by-step in further sections.
Pipeline
Strip body pipeline is used because this pipeline is executed at the end of all processing when spam/virus filtering is applied and decision that message is good already made. Pipeline is enabled with <strip_body_enabled> and engines are specified with <strip_body_engines>. The pipeline consists from the following engines:
- salesforce_http.login
- salesforce_http.find_opportunity
- salesforce_http.attach_attachment_to_opportunity
- salesforce_http.create_task_for_opportunity
Salesforce HTTP
This is a collection of salesforce queries. It starts with <http id="salesforce_http"> tag.
Tag <method> defines default HTTP request method for all queries. It will be used when a query doesn't define its own. All salesforce queries uses POST .
Tag <ssl_context> with <load_veriy_file> subtag is mandatory because queries use https:// scheme. They defines Certificate Authority (CA) to use for verifying peer. File /usr/share/ssl/cert.pem is a default file that comes with OpenSSL installation. It contains certificates of many widespread CA's. The location may be different for different installations (for example /etc/pki/tls/cert.pem) so this options should be modified to work with a specific installation.
Tag <headers> defines default HTTP request headers template for all queries. It will be used when a query doesn't define its own. All salesforce queries uses same headers template. This template uses $http_date macros which is RFC 1123 complient current GMT date (for example: Fri, 25 Sep 2009 17:58:50 +0000). Content-Type and Accept headers are text/xml because Salesforce uses XML/SOAP HTTP entities. Charset is utf-8 because ESM works with this charset. Cache-Control and Pragma disables response caching. SOAPAction must be present and have empty value according to Salesforce enterprise.wsdl. Other standard required headers Host and Content-Length are added automatically by implementation.
Login
Login is a method of Salesforce HTTP Engine. It starts with <query id="login"> tag.
Tag <uri> is a template that defines URI to connect to. In this case it is just a constant value take from enterprise.wsdl file. WSDL file formally defines rules and formats of all possible queries to salesforce. It is published by salesforce.
Tag <entity> defines HTTP entity (any data) that is sent to the server in a request. For salesforce it is a XML/SOAP data formated according to WSDL rules. If you don't fully understand how to write XML/SOAP data guided by WSDL you may find additional examples here. And description for salesforce objects and methods here.
Sequences "<![CDATA" at the beginning and "]]>" at the end are required. This will not appear in result request. This is needed for MPP to ignore XML markup inside when reading configs. Note that sequence:
<entity><![CDATA[<?xml version="1.0"?>
MUST appear as it is in example: don't put space or linebreaks anywhere inside. Otherwise you will get an error from salesforce server that say something about unrecognized format. Also note that you must track if your template contains "]]>" sequence inside and replace all such sequences (if any must appear) with "]]]]><![CDATA[>".
Replace your_user_name, your_password and your_security_token with your credentials. Remove '+' sign which is here for visual separation. See for details here.
Tags <result> defines how results of login queries are calculated. Because salesforce always returns XML as response entity then XML functions can be used to extract data from response entity to engine results. Function ${xml_xpath ...} evaluates given XPath expression and extracts all character data from first matched node (whether element or attribute). For quick tutorial for XPath see for example here or full spec here.
XPath expressions used in this example extracts character data from a first found node with specified name. Note that such a long expression for filtering nodes by name MUST be used because responses uses namespaces so just specifying node names will not work. Strictly saying it should be also namespace comparisons but they can be avoided for simplicity.
Login response provides two important values:
- serverUrl - URI of a server to perform further requests to.
- sessionId - Session ID to include in further requests as SessionHeader.
This values are extracted and exported as engine results.
Tags <validate> provides protocol errors processing mechamism. They are used for validating calculated results. Special macro $result is available withing template options of <validate> tags which refer to result being validated. It may consist of zero or more cases and default actions. Cases are evaluated in turns untill the one with matched condition is found and then actions for such a case are executed. If no matched case condition found the default actions are executed. Available action are:
- log - put log message to MPP log. Logging level is specified with "level" attribute for <log> tag;
- throw - execution of a pipeline will be stopped and <on_error> action will be performed.
Note: value of throw tag is directly inserted in SMTP (and other protocol) responses for MTA. So you should make sure that value doesn't contain line breaks and feeds and other characters unsafe for SMTP protocol. General recomendation is to combine comprehansive writing to logs and short simple throw messages.
Tag <result id="faultstring"> is dedicated to errors processing. It checks for fault string in response and throws if finds it. Because it goes before other results it will be executed first.
Find Opportunity
Find opportunity is a method of Salesforce HTTP Engine. It starts with tag <query id="find_opportunity">.
Tag <uri> uses serverUrl and SOAP SessionHeader uses sessionId results from login query.
SOAP <soapenv:Body> contains invocation of SOQL query that selects Opportunities that are open and that contains any of sender or recipients as its Contact (through OpportunityContactRole). For details of SOQL see here. Because MUA's uses information from message headers the query uses senders ($headers.from, $headers.sender, $headers.reply_to) and recipients ($headers.to, $headers.cc, $headers.bcc) identity from headers and not SMTP sender ($sender) and recipients ($recipient).
Function ${xml_cdata ...} wraps its argument into "<![CDATA[...]]>" brackets. It also replaces all internal "]]>" with "]]]]><