Saturday 31 March 2012

Retrieve multiple records using JavaScript SOAP request in MS CRM 2011 !!!


Below example to retrieve accountid based on the account name from account entity:-

function ExecuteRequest(_XML, Message) {
    try {
        var _ResultXML = null;
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", Xrm.Page.context.getServerUrl() + "/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/" + Message);
        xmlhttp.send(_XML);
        _ResultXML = xmlhttp.responseXML;
        var errorCount = _ResultXML.selectNodes('//error').length;
        if (errorCount != 0) {
            var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
            alert(msg);
            _ResultXML = null;
            return _ResultXML;
        }
        else {
            return _ResultXML;
        }
    }
    catch (Err) {
        alert(Err);
        return;
    }

function GetMultipleAccounts(AccountName) {

    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>accountid</b:string>" +
                 "</a:Columns>" +
                 "</a:ColumnSet>" +
                 "<a:Criteria>" +
                 "<a:Conditions>" +
                 "<a:ConditionExpression>" +
                 "<a:AttributeName>name</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'>" + AccountName + "</b:anyType>" +
                 "</a:Values>" +
                 "</a:ConditionExpression>" +
                 "</a:Conditions>" +
                 "<a:FilterOperator>And</a:FilterOperator>" +
                 "<a:Filters />" +
                 "</a:Criteria>" +
                 "<a:Distinct>false</a:Distinct>" +
                 "<a:EntityName>account</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 ID = _ResultXML.selectSingleNode("//a:Id").text
        return ID;
    }
    else
        return null;
}

Call GetMultipleAccounts function like as below:-

var _Id = GetMultipleAccounts(“Microsoft”);

Cheers !!!

8 comments:

  1. thanks Vikram, this code helped me

    ReplyDelete
  2. your welcome !! it's my pleasure !!!

    ReplyDelete
  3. Can you please let me know if I need single retrieve

    ReplyDelete
  4. Please check this Post

    http://vikramxrm.blogspot.com/2013/11/retrieve-account-record-based-on.html

    ReplyDelete
  5. Hi, when i used this I have a "NetworkError". What can I do ?

    ReplyDelete
    Replies
    1. Hi, what is complete error message ? what is CRM deployment type On-Premise or Online ?

      Delete
  6. Dear Vikram Singh,

    Thank you for the above code.
    However, I am facing a javascript error as follows:

    typeerror object doesn't support property or method 'selectnodes'

    Your JS code is working fine in Internet Explorer 8, but when I ran the same code in my server's Internet Explorer 11, then it threw me the above error.

    Now, it seems that Microsoft has stopped supporting this selectSingleNode() in Internet Explorer 10+ versions. (Refer: http://stackoverflow.com/questions/17743534/need-to-work-around-selectsinglenode-in-ie10)

    How do I solve this issue?? Please help out.

    Best Regards,
    Harshit Mehta
    hmehta@webdevinc.net

    ReplyDelete
    Replies
    1. Never mind a friend found a workaround to this solution..

      Here it is:
      http://crmjavascripts.blogspot.com/2013/11/replacement-of-soap-in-crm-for-retrieve.html

      Delete