locked
Sample Code to Access Records using SOAP in CRM 2013 Javascript (Key/Value Pair) RRS feed

  • General discussion

  • function ExecuteRequest(_XML, Message) {
        var _ResultXML = null;
        var xmlhttp = new XMLHttpRequest();    

        var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();

        xmlhttp.open("POST", serverUrl + "/XRMServices/2011/Organization.svc/web", false);
        xmlhttp.setRequestHeader("Accept", "application/xml, text/xml, */*");
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        xmlhttp.send(_XML);
        _ResultXML = xmlhttp.responseXML;
        var errorCount = $(_ResultXML).find("a\\:error").length
        if (errorCount != 0) {
            var msg = $(_ResultXML).find("a\\:description").text();
            _ResultXML = null;
            return _ResultXML;
        }
        else {
            return _ResultXML;
        }

    }




    function buildFetchRequest(fetch, RequestName) {
        var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        request += "<s:Body>";

        request += '<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">' +
           '<request i:type="b:' + RequestName + 'Request" ' +
           ' xmlns:b="http://schemas.microsoft.com/xrm/2011/Contracts" ' +
           ' xmlns:i="http://www.w3.org/2001/XMLSchema-instance">' +
           '<b:Parameters xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">' +
           '<b:KeyValuePairOfstringanyType>' +
           '<c:key>Query</c:key>' +
           '<c:value i:type="b:FetchExpression">' +
           '<b:Query>';

        request += CrmEncodeDecode.CrmXmlEncode(fetch);

        request += '</b:Query>' +
           '</c:value>' +
           '</b:KeyValuePairOfstringanyType>' +
           '</b:Parameters>' +
           '<b:RequestId i:nil="true"/>' +

     '<b:RequestName>' + RequestName + '</b:RequestName>' +
           '</request>' +
           '</Execute>';

        request += '</s:Body></s:Envelope>';
        return request;

    }



    function UpdateReferenceSR() {
        
        if (Xrm.Page.data.entity.attributes.get("qfca_referencesrid").getValue() != null) {
            var referenceID = Xrm.Page.data.entity.attributes.get("qfca_referencesrid").getValue()[0].id;

            var fetchXML = "<fetch distinct='false' mapping='logical'>" +
                          "<entity name='incident'>" +
                          "<attribute name='incidentid' />" +
                          "<attribute name='ownerid' />" +
                          "<filter type='and'>" +
                                     "<condition attribute='incidentid' operator='eq' value='" + referenceID + "'/>" +
                                "</filter>" +
                          "</entity>" +
                          "</fetch>";

            var request = buildFetchRequest(fetchXML, "RetrieveMultiple");
            var resultXML = ExecuteRequest(request, "RetrieveMultiple");

            var ownerID = null;
            $(resultXML).find("a\\:Entity").each(function () {
                $(this).find("a\\:KeyValuePairOfstringanyType").each(function () {
                    var xmlElement = $(this);
                    var key = xmlElement.find("b\\:key").text();
                    if (key == "ownerid") {
                        ownerID = xmlElement.find("b\\:value").text();

                    }

                });
            });      

            var ID = ownerID.slice(0, 36);
            var ownerName = ownerID.slice(36, ownerID.length);
            SetLookupValue("fieldname", ID, ownerName, "entityname");
        }

    }

    function SetLookupValue(fieldName, id, name, entityType) {
        if (fieldName != null) {
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = id;
            lookupValue[0].name = name;
            lookupValue[0].entityType = entityType;
            Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
        }
    }

    Monday, February 2, 2015 2:11 PM