3

I am using the following java script code to retrieve contacts by account Id. I am set setting the alert message debugging. It does not enter in success call back message function.

Ending up with following error

Error while retrieval "error" : { "lang":"en-US", "Value":"Syntax error'\ufffd' at position 20." }

I am using the following code.

function retrieveMultiple(odataSetName, select, filter, successCallback) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "?";

    alert("retrieveMultiple"+odataUri);

    if (select) {
        odataUri += "$select=" + select + "&";
        alert("select error="+odataUri);
    }

    if (filter) {
       odataUri += "$filter=" + filter;
       alert("filter error="+odataUri);
    }

    $.ajax({
       type: "GET",
       contentType: "application/json; charset=utf-8",
       datatype: "json",
       url: odataUri,
       beforeSend: function (XMLHttpRequest) {
           //Specifying this header ensures that the results will be returned as JSON.
           var x = XMLHttpRequest.setRequestHeader("Accept", "application/json");
           alert(" in Ajax :beforeSend:" + x );
       },
       success: function (data, textStatus, XmlHttpRequest) {
           alert("In success function outside success");

           if (successCallback) {
               alert("successCallback in if");

               if (data && data.d && data.d.results) {
                   alert("data && data.d && data.d.results"+data + data.d + data.d.results);
                   successCallback(data.d.results, textStatus, XmlHttpRequest);
                   alert("data.d.results, textStatus, XmlHttpRequest" + data.d.results + textStatus +       XmlHttpRequest);
                }
                else if (data && data.d) {
                     successCallback(data.d, textStatus, XmlHttpRequest);
                }
           else {
               successCallback(data, textStatus, XmlHttpRequest);
           }
       }
    },
    error: function (XmlHttpRequest, textStatus, errorThrown) {
        alert(" In erro function");

        if (XmlHttpRequest && XmlHttpRequest.responseText) {
           alert(" In error function If");
           alert("Error while retrieval ; Error – " + XmlHttpRequest.responseText);
        }
    }
});
}

function readRecordsOnSuccess(data, textStatus, XmlHttpRequest) {
    // Loop through the retrieved records
    for (var indx = 0; indx < data.length; indx++) {
         alert("Name – " + data[indx].name);
    }
}

function retrieveContactsByAccountId() {
    // Pass ‘Contact’ set name since we are reading Contacts
    var oDataSetName = "ContactSet";

    // Column names of ‘Contact’ (Pass * to read all columns)
    var columns = "FirstName";

    // Read Account Guid
    var accountId = Xrm.Page.data.entity.getId()

    // Prepare filter
    var filter = "AccountId/Id eq guid’" + accountId + "‘";

    alert("retrieveContactsByAccountId"+filter);

    retrieveMultiple(oDataSetName, columns, filter, readRecordsOnSuccess);
}

1 Answer 1

1

Looks like common mistype ;) Notice following string you are passing:

var filter = "AccountId/Id eq guid’" + accountId + "‘";

Your apostrophes are different from usual ones

You need to use regular '

var filter = "AccountId/Id eq guid'" + accountId + "'";
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. now its going through all the function, but still does not showing the desired results.
Does it reach successCallback function?
Ending up with name undefine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.