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

function GetAccountStatus(accountId) {

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

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

        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;
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s