我目前正在尝试使用以下代码片段合并salesforce中的两个帐户。然而,我总是收到500条回复。有人能指出我在定义XML请求体时所做的错误吗?这是当前的实现。
from simple_salesforce import Salesforce
import requests
def connect_sf(security_token, password, username):
sf = Salesforce(username, password,
security_token)
return sf
sf = connect_sf()
headers = { 'Content-Type': 'text/xml', 'Accept': 'application/soap+xml,application/dime, multipart/related, text/*', 'Authorization': 'Bearer ' + sf.session_id, 'SOAPAction': 'merge', 'Sforce-Auto-Assign': 'false', 'charset':'UTF-8'}
master_id = 'MASTERID'
victim_id = 'VICTIMID'
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:merge>
<ns1:masterRecord>
<masterRecord xsi:type="sobject:Account" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
{master_id}
</ns1:masterRecord>
<ns1:recordToMergeIds>
{victim_id}
</ns1:recordToMergeIds>
</ns1:merge>
"""
session = requests.Session()
sf.session.request(method = 'POST', url = 'https://' + sf.sf_instance +'/services/Soap/u/' + sf.sf_version, data = body, headers = headers)
谢谢你的帮助^^
三个问题
首先,您试图将授权头与SOAP一起使用;这是错误的。
第二,你错过了部分有效负载;在XML中,每个开始标记必须有一个结束标记。
第三,您遗漏了一些XML名称空间,这可能会导致问题。
我从另一个例子中复制粘贴了一些源代码,并提出了以下修改:
from simple_salesforce import Salesforce
import requests
def connect_sf(security_token, password, username):
sf = Salesforce(username, password,
security_token)
return sf
sf = connect_sf()
headers = {
'Content-Type': 'text/xml',
'Accept': 'application/soap+xml,application/dime, multipart/related, text/*',
'Authorization': 'Bearer ' + sf.session_id,
'SOAPAction': 'merge',
'Sforce-Auto-Assign': 'false',
'charset':'UTF-8'
}
master_id = 'MASTERID'
victim_id = 'VICTIMID'
body = f"""
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<n1:SessionHeader xmlns:n1="urn:enterprise.soap.sforce.com" env:mustUnderstand="0">
<n1:sessionId>REDACTED</n1:sessionId>
</n1:SessionHeader>
</env:Header>
<env:Body>
<n2:merge xmlns:n2="urn:enterprise.soap.sforce.com">
<n2:masterRecord xmlns:n3="urn:sobject.enterprise.soap.sforce.com" xsi:type="n3:Lead">
<n3:Id>{master_id}</n3:Id>
</n2:masterRecord>
<n2:recordToMergeIds>{victim_id}</n2:recordToMergeIds>
</n2:merge>
</env:Body>
</env:Envelope>
"""
session = requests.Session()
sf.session.request(method = 'POST', url = 'https://' + sf.sf_instance +'/services/Soap/u/' + sf.sf_version, data = body, headers = headers)
可能会有额外的问题,但这些是你需要的最小的改变。