0

I'm working on trying to merge a list of records in Salesforce via the SOAP API using Python.

I am able to get a Postman POST request to work, but I am running into a roadblock with getting the SOAP request to work in Python. The error I keep getting is 500, which may or may not mean that my code is not working.

This is the code that I have so far:

import requests
from simple_salesforce import Salesforce


headers = { 
    'Content-Type': 'text/xml', 
    'Accept': 'application/soap+xml,application/dime, multipart/related, text/*', 
    'Authorization': 'Bearer ' + session_id, 
    'SOAPAction': 'merge', 
    'Sforce-Auto-Assign': 'false', 
    'charset':'UTF-8'
}


body = f"""
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com" xmlns:urn1="urn:sobject.enterprise.soap.sforce.com">
    <soapenv:Header> 
        <urn:SessionHeader>
            <urn:sessionId>REDACTED</urn:sessionId>
        </urn:SessionHeader> 
    </soapenv:Header>
   <soapenv:Body>
    <ns1:merge xmlns:ns1='urn:partner.soap.sforce.com'>
        <ns1:merge>
            <ns1:masterRecord>
                <ens:type xmlns:ens='urn:sobject.partner.soap.sforce.com'>Account</ens:type>
                <ens:Id xmlns:ens='urn:sobject.partner.soap.sforce.com'>MASTER_ID</ens:Id>
            </ns1:masterRecord>
            <ns1:recordToMergeIds>CHILD_ID</ns1:recordToMergeIds>             
        </ns1:merge>                                   
    </ns1:merge>  
   </soapenv:Body>
</soapenv:Envelope>

"""
session = requests.Session()
sf = Salesforce(username='my_username', password='my_password', security_token='my_token', session=session, domain='test')
response = sf.session.request(method='POST', url = 'https://companyname.my.salesforce.com/services/Soap/u/53.0/', data = body, headers=headers)

print(response)

I have also tried looking at an example through this blog post, but I'm still getting a 500 error.

Not sure where I am going wrong with this or if it is possible to do something like this with Python.

2
  • It's certainly possible. A 500 error means something went wrong, but you need to read the error message to figure out what the problem is. There will definitely be an error message with a 500 error.
    – sfdcfox
    Commented Nov 23, 2021 at 21:48
  • @sfdcfox Figured out how to get the error message to appear finally. This is what I get: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="schemas.xmlsoap.org/soap/envelope"> <soapenv:Body> <soapenv:Fault> <faultcode>soapenv:Client</faultcode> <faultstring>The processing instruction target matching &quot;[xX][mM][lL]&quot; is not allowed.</faultstring> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>
    – Perdue
    Commented Nov 23, 2021 at 22:05

1 Answer 1

0

After looking at this post, I figured out that the carriage return that I had at the beginning and ending of my XML was what was causing the issue. Once I took those out, then everything worked perfectly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .