yeye
yeye

Reputation: 513

Parse Salesforce.com XML message in Python

I'm trying to interface Python and Salesforce.

Salesforce sends me an outbound SOAP message which I correctly receive, acknowledge and read.

Now, I'd like to parse the message to determine what script to trigger in Python.

The following is a printed example of the body of the message received: (I've anonymized IDs with XXX)

b'<?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>00Dxxxx</OrganizationId>
   <ActionId>04k5A000000XXX</ActionId>
   <SessionId xsi:nil="true"/>
   <EnterpriseUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/c/45.0/00Dxxxx </EnterpriseUrl>
   <PartnerUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/u/45.0/00Dxxxx </PartnerUrl>
   <Notification>
    <Id>04l5A000XXX</Id>
    <sObject xsi:type="sf:QuoteHistory__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
     <sf:Id>a0B5A0XXX</sf:Id>
     <sf:Status__c>Send price request</sf:Status__c>
    </sObject>
   </Notification>
  </notifications>
 </soapenv:Body>
</soapenv:Envelope>'

What is the b in front of the str ? Python prints it when I print the body of the message. Does it have any impact ?

Now, to process my message, I'd like to read the lines in the sObject tab, ie those ones in my example:

    <sObject xsi:type="sf:QuoteHistory__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
     <sf:Id>a0B5A0XXX</sf:Id>
     <sf:Status__c>Send price request</sf:Status__c>
    </sObject>

Sometimes, I expect my message to have other fields than Status and Id sent, and I would like to parse the message properly into a table and then determine what action to trigger based on the fields sent and their values.

I'll manage on my side a table with field name/field values and action to trigger, that seems easy to me.

What would be the best way to read this message properly and dynamically?

Upvotes: 1

Views: 441

Answers (2)

yeye
yeye

Reputation: 513

Found my way using xmltodict

import pandas as pd 
import xmltodict

#Convert the msg to dict
ParsedSFMessage = xmltodict.parse(sfmsg)
#Keep only the needed level
ParsedSFMessage = ParsedSFMessage['soapenv:Envelope']['soapenv:Body']['notifications']['Notification']['sObject'] 
#Convert to df and transpose
ParsedSFMessage = pd.DataFrame.from_dict(ParsedSFMessage, orient='index').T.drop(columns = ['@xmlns:sf'])

Et voilà

@xsi:type   sf:Id   sf:Status__c
sf:QuoteHistory__c  a0B5A0XXXX  Send price request

Upvotes: 1

balderman
balderman

Reputation: 23815

Have a look below

import re
import xml.etree.ElementTree as ET

XML = '''<?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>00Dxxxx</OrganizationId>
   <ActionId>04k5A000000XXX</ActionId>
   <SessionId xsi:nil="true"/>
   <EnterpriseUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/c/45.0/00Dxxxx </EnterpriseUrl>
   <PartnerUrl>https://xxx-dev-ed.my.salesforce.com/services/Soap/u/45.0/00Dxxxx </PartnerUrl>
   <Notification>
    <Id>04l5A000XXX</Id>
    <sObject xsi:type="sf:QuoteHistory__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
     <sf:Id>a0B5A0XXX</sf:Id>
     <sf:Status__c>Send price request</sf:Status__c>
    </sObject>
   </Notification>
  </notifications>
 </soapenv:Body>
</soapenv:Envelope>'''

_xml = xmlstring = re.sub(' xmlns="[^"]+"', '', XML, count=1)
tree = ET.fromstring(_xml)
sobject = tree.find('.//sObject')
for idx, child in enumerate(list(sobject)):
    print('{}) {} => {}'.format(idx, child.tag[child.tag.find('}') + 1:], child.text))

output

0) Id => a0B5A0XXX
1) Status__c => Send price request

Upvotes: 2

Related Questions