View Single Post
  #1  
Old 05-13-2008, 09:19 AM
mike mike is offline
Administrator
 
Join Date: May 2007
Posts: 248
Send a message via AIM to mike Send a message via MSN to mike Send a message via Yahoo to mike Send a message via Skype™ to mike
Using Salesforce Outbound SOAP Messages with PHP

Recently I have been using the Salesforce outbound SOAP messages while I was working on some workflow's. Honestly I had no idea that this could be implemented and I am in awe right now.

My organization needed to find a way to change the Owner Id on a custom object when a certain field changed. We implemented a workflow, which in turn would thin trigger an outbound message from Salesforce via the API. An outbound message looks like and is formatted in XML. Here is an example:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <notifications xmlns="http://soap.sforce.com/2005/09/outbound">
   <OrganizationId>00DR......</OrganizationId>
   <ActionId>04kR00000004CCXIA2</ActionId>
   <SessionId xsi:nil="true"/>
   <EnterpriseUrl>https://cs2-api.salesforce.com/services/Soap/c/8.0/4e1300DR00000000XcD</EnterpriseUrl>
   <PartnerUrl>https://cs2-api.salesforce.com/services/Soap/u/8.0/4e1300DR00000000XcD</PartnerUrl>
   <Notification>
    <Id>04lR0000000CcIFIA0</Id>
    <sObject xsi:type="sf:Design_Registration__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
     <sf:Id>a06R00000009QZaIAM</sf:Id>
     <sf:BU_Reviewer_Lookup__c>00540000000lnc2AAA</sf:BU_Reviewer_Lookup__c>
     <sf:DP_cust_eng_email__c>me@email.com</sf:DP_cust_eng_email__c>
     <sf:DP_reg_id__c>JXD08-7CLLB7</sf:DP_reg_id__c>
     <sf:DR_Number__c>R080500453</sf:DR_Number__c>
     <sf:Field_reviewer_lookup__c>00540000000yHbGAAU</sf:Field_reviewer_lookup__c>
     <sf:Name>URT</sf:Name>
     <sf:OwnerId>00540000000mBqsAAE</sf:OwnerId>
    </sObject>
   </Notification>
  </notifications>
 </soapenv:Body>
</soapenv:Envelope>
The key to this is that you are allowed to include almost any field in the outbound message. You can set these values when you are creating the trigger in Salesforce.

Once you set the fields, they are sent in the sObject portion of the SOAP message:

Code:
<sObject xsi:type="sf:Design_Registration__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
     <sf:Id>a06R00000009QZaIAM</sf:Id>
     <sf:BU_Reviewer_Lookup__c>00540000000lnc2AAA</sf:BU_Reviewer_Lookup__c>
     <sf:DP_cust_eng_email__c>me@email.com</sf:DP_cust_eng_email__c>
     <sf:DP_reg_id__c>JXD08-7CLLB7</sf:DP_reg_id__c>
     <sf:DR_Number__c>R080500453</sf:DR_Number__c>
     <sf:Field_reviewer_lookup__c>00540000000yHbGAAU</sf:Field_reviewer_lookup__c>
     <sf:Name>URT</sf:Name>
     <sf:OwnerId>00540000000mBqsAAE</sf:OwnerId>
    </sObject>
The great thing is that you can then kick off almost any application with this with a SOAP Server that is setup locally (we have one running on our server).

The key is that you have to send a response back to your instance of Salesforce.

So once the SOAP Server is setup on your server, you can respond to an incoming message like this:

PHP Code:
//Reads SOAP incoming message from Salesforce/MAPS
$data fopen('php://input','rb');
$content fread($data,5000); 
This code reads the raw data coming in from Salesforce and then sticks it in a variable called $content.

You can then respond to the message like this:

PHP Code:
if ($content)
{
    respond('true');
}
else
{
    respond('false');
}

function respond($tf)
{

    print '<?xml version "1.0" encoding "utf-8"?>
   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <soapenv:Body>
           <notifications xmlns="http://soap.sforce.com/2005/09/outbound">
               <Ack>' . $tf . '</Ack>
              </notifications>
          </soapenv:Body>
      </soapenv:Envelope>';
}
So $tf is set to true and responds to the outbound message and Salesforce knows that the message has been accepted and is removed from the queue.


So what can you really do with this? We are using this workflow to change the OwnerId on a custom object when a certain field is changed

I will continue to update this thread with different topics and ideas on what I have been able to accomplish with this on demand feature

~Mike
Reply With Quote