Get Account Status in JavaScript using OData in Dynamics CRM 2011

Here is the sample JavaScript code to get account status.
Make sure you have added jquery-1.9.1.min.js and JSON2.js in Entity form libraries. If not you can download these from following URL’s
http://jquery.com/download/
http://www.json.org/js.html

function GetAccountStatus(accountId) {

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

    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "AccountSet?$filter=AccountId eq guid'" + accountId + "'";

    $.ajax({
        type: "GET",
        async: false,
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: odataSelect,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            var accountState = data.d.results[0].StateCode.Value;
            alert(accountState);
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
    });
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s