locked
javascript SOAP retrieve contact records by contactid - network error RRS feed

  • Question

  • hi, when I try retreive contact record with this method I have a NetworkError. What is wrong with this methods ?

    function ExecuteRequest(soapBody, method) {
        try {
            var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            xmlHttpRequest.Open("POST", '/mscrmservices/2007/CrmService.asmx', false);
            xmlHttpRequest.setRequestHeader("SOAPAction", 'http://schemas.microsoft.com/crm/2007/WebServices/' + method); //Fetch,Execute,Create
            xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soap:Envelope xmlns:soap=\'http://schemas.xmlsoap.org/soap/envelope/\' xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\'>" +
            GenerateAuthenticationHeader() + "<soap:Body>" + soapBody + "</soap:Body></soap:Envelope>";
            xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
            xmlHttpRequest.send(xml);
    
            var resultXml = xmlHttpRequest.responseXML;
            var errorCount = resultXml.selectNodes('//error').length;
            var status = xmlHttpRequest.status;
    
            if (errorCount != 0) {
                var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
                alert(msg);
    
                return null;
            }
    
            return resultXml;
        }
        catch (err) {
        }
    
        return null;
    }
    
    function GetContact(contactID) {
    
        var request = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
                     "<s:Body>" +
                     "<RetrieveMultiple xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services'     xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
                     "<query i:type='a:QueryExpression' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>" +
                     "<a:ColumnSet>" +
                     "<a:AllColumns>false</a:AllColumns>" +
                     "<a:Columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
                     "<b:string>fullname</b:string>" +
                     "<b:string>mobilephone</b:string>" +
                     "<b:string>emailaddress1</b:string>" +
                     "<b:string>telephone2</b:string>" +
                     "</a:Columns>" +
                     "</a:ColumnSet>" +
                     "<a:Criteria>" +
                     "<a:Conditions>" +
                     "<a:ConditionExpression>" +
                     "<a:AttributeName>contactid</a:AttributeName>" +
                     "<a:Operator>Equal</a:Operator>" +
                     "<a:Values xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
                     "<b:anyType i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>" + contactID + "</b:anyType>" +
                     "</a:Values>" +
                     "</a:ConditionExpression>" +
                     "</a:Conditions>" +
                     "<a:FilterOperator>And</a:FilterOperator>" +
                     "<a:Filters />" +
                     "</a:Criteria>" +
                     "<a:Distinct>false</a:Distinct>" +
                     "<a:EntityName>contact</a:EntityName>" +
                     "<a:LinkEntities />" +
                     "<a:Orders />" +
                     "<a:PageInfo>" +
                     "<a:Count>0</a:Count>" +
                     "<a:PageNumber>0</a:PageNumber>" +
                     "<a:PagingCookie i:nil='true' />" +
                     "<a:ReturnTotalRecordCount>true</a:ReturnTotalRecordCount>" +
                     "</a:PageInfo>" +
                     "<a:NoLock>false</a:NoLock>" +
                     "</query>" +
                     "</RetrieveMultiple>" +
                     "</s:Body>" +
                     "</s:Envelope>";
    
        var _ResultXML = ExecuteRequest(request, "RetrieveMultiple");
        var count = _ResultXML.selectSingleNode("//a:TotalRecordCount").text;
        if (count >= 1) {
            var fullName = _ResultXML.selectSingleNode("//a:fullname").text;
            var mobPhone = _ResultXML.selectSingleNode("//a:mobilephone").text;
            var email = _ResultXML.selectSingleNode("//a:emailaddress1").text;
            var tel = _ResultXML.selectSingleNode("//a:telephone2").text;
    }

    Thursday, January 23, 2014 5:35 PM

All replies

  • Have you considered doing this with REST instead of SOAP?

    It would look something like this:

    var req = new XMLHttpRequest();
    req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet" + 
    	"(guid'15BB2236-B57F-E311-BB6D-6C3BE5A881A4')?$select=EMailAddress1,FullName,MobilePhone"), true);
    req.setRequestHeader('Accept', 'application/json');
    req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(req.responseText).d;
                var name = result.FullName;
                alert(name);
            } else {
                alert(this.statusText);
            }
        }
    };
    req.send();
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, January 23, 2014 6:06 PM
    Moderator
  • With this I have "not found" message.
    Thursday, January 23, 2014 7:30 PM
  • Did you replace the GUID of the contact with the value you are passing in? The Not Found message indicates it can't find the record based on the GUID

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, January 23, 2014 10:24 PM
    Moderator
  • Yes, I did:)
    Thursday, January 23, 2014 10:26 PM