我们正在尝试使用 SOAP 来检索基于 N:N 关系的相关记录。因此,我们从当前表单中检索到 1 个实体的 1 个 GUID。然后我们需要来自其他实体的所有 GUID。我们如何正确地形成我们的 SOAP 请求。
RetrieveContacts = function (EntityName, EntityKeyName, contactGUID) {
// Prepare IDs to retrieve the records
var authenticationHeader = GenerateAuthenticationHeader();
// Prepare the SOAP message.
var xml = "";
xml += "<?xml version='1.0' encoding='utf-8'?>";
xml += "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'";
xml += " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'";
xml += " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>";
xml += authenticationHeader;
xml += "<soap:Body>";
xml += "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
xml += "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'";
xml += " xsi:type='q1:QueryExpression'>";
xml += "<q1:EntityName>" + EntityName + "</q1:EntityName>";
xml += "<q1:ColumnSet xsi:type='q1:ColumnSet'>";
xml += "<q1:Attributes>";
xml += "<q1:Attribute>" + EntityKeyName + "</q1:Attribute>";
xml += "</q1:Attributes>";
xml += "</q1:ColumnSet>";
xml += "<q1:Distinct>false</q1:Distinct>";
xml += "<q1:Criteria>";
xml += "<q1:Conditions>";
xml += "<q1:Condition>";
xml += "<q1:AttributeName>" + EntityKeyName + "</q1:AttributeName>";
xml += "<q1:Operator>Equal</q1:Operator>";
xml += "<q1:Values>";
xml += "<q1:Value xsi:type='xsd:string'>" + contactGUID + "</q1:Value>";
xml += "</q1:Values>";
xml += "</q1:Condition>";
xml += "</q1:Conditions>";
xml += "</q1:Criteria>";
xml += "</query>";
xml += "</RetrieveMultiple>";
xml += "</soap:Body>";
xml += "</soap:Envelope>";
// Prepare the xmlHttpObject and send the request.
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", xml.length);
xHReq.send(xml);
// Capture the result.
var resultXml = xHReq.responseXML;
// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
// Parse and display the results.
else {
var results = resultXml.getElementsByTagName('BusinessEntity');
var msg = "";
if (results.length == 0) {
msg = "No records to submit.";
alert(msg);
return;
}
else {
for (i = 0; i < results.length; i++) {
var idValue = results[i].selectSingleNode('./q1:' + EntityKeyName).nodeTypedValue;
//Now insert the new RSVP record
msg += idValue + "\t" + name + "\r";
}
alert(msg);
}
}
}