如何基于N:N关系检索SOAP中的相关记录

时间:2012-06-20 16:25:49

标签: javascript dynamics-crm-2011 dynamics-crm

我们正在尝试使用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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

当然我正在使用2011服务,但这里是一个使用来自JavaScript的SOAP的例子http://blog.customereffective.com/blog/2011/05/execute-fetch-from-javascript-in-crm-2011.html

或者,你可以使用这篇文章中的内容然后传入FetchXML来查询相同的信息。如果你这样做,这也可能有所帮助......

http://blog.customereffective.com/blog/2011/05/parsing-and-consuming-the-crm-2011-soap-service-inside-javascript.html