, , , , ,

Hello Everyone,

I just wanted to share this piece of code actually with myself as this comes useful  quite very often now.

In my Previous Post I used City, State and Country as a Lookup in the Address field under Account.  With the selection of City the related records State and Country also gets populated automatically using JScript. Earlier the Jscript used included the use of ActiveXObject which is now deprecated from CRM 2013.

I read a very helpful post from PowerObjects related to this issue in this post. I used their code to build a small Jscript for myself which I use it most of the time directly.

The code is as follows:-

function SetStateCountry(results) {
if (results[0].new_State != null)
                        var name = results[0].new_State.Name;
                        var id = results[0].new_State.Id;
                        Xrm.Page.getAttribute("new_state").setValue([{ id: id, name: name, entityType: 'new_state'}]);

if (results[0].new_Country != null)
                        var name = results[0].new_Country.Name;
                        var id = results[0].new_Country.Id;
                        Xrm.Page.getAttribute("new_country").setValue([{ id: id, name: name, entityType: 'new_country'}]);

function getCSC() 
    var cty= Xrm.Page.data.entity.attributes.get("new_city");

    if (cty.getValue() == null)
ctyId = cty.getValue()[0].id;
var pagecontext = Xrm.Page.context;
var serverUrl = pagecontext.getServerUrl();
var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var oDataSelect = oDataPath + "/new_citySet?$select=new_State,new_Country&$filter=new_cityId" + " eq guid'" + ctyId + "'";

type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataSelect,

beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },

success: function (data, textStatus, XmlHttpRequest) {

error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + oDataSelect ); }


Thanks !!!