function GetCountryFromAccount() {
//Get the GUIDvalue of this Contact's Parent Account
var ParentAccount = Xrm.Page.data.entity.attributes.get("parentcustomerid");
if (ParentAccount.getValue() != null) {
var ParentAccountGUID = ParentAccount.getValue()[0].id;
}
// Read the CRM Context to determine the CRM URL
var serverUrl = Xrm.Page.context.getServerUrl()
// Specify the ODATA End Point and Entity Collection
var ODATA_ENDPOINT = "XRMServices/2011/OrganizationData.svc";
var ODATA_EntityCollection = "/AccountSet";
// Specify the ODATA Query
var ODATA_Query = "(guid\'" + ParentAccountGUID + "\')?$select=new_Country";
// Combine into the final URL
var ODATA_Final_url = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Query;
//Calls the REST endpoint to retrieve the Country value of the Parent Account
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ODATA_Final_url,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
//This function will trigger asynchronously if the Retrieve was successful
var CountryGUID = data.d.new_Country.Id;
if (CountryGUID == null) {
// country on acct is null, we won't bother filtering the view
return;
}
else {
// call function to add custom view
ChangeLookupView_State(CountryGUID);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
//This function will trigger asynchronously if the Retrieve returned an error
//Error encountered, we won't bother filtering the view
return;
}
});
}
function ChangeLookupView_State(CountryGUID) {
//Set parameters values needed for the creation of a new lookup view...
var viewId = Xrm.Page.data.entity.getId(); // Your new lookup views needs a unique id. It must be a GUID. Here I use the GUID of the current record.
var entityName = "new_state"; // The entity your new lookup view relates to
var viewDisplayName = "States for Account Country"; // A name for new lookup view
var viewIsDefault = true; // Whether your new lookup view should be the default view displayed in the lookup or not
//Define the Fetch XML query your new lookup view will use. You can create this via Advanced Find. You'll need to massage the syntax a little though
var fetchXml =
"
"
"
"
"
"
"
"
"
"
"
//Define the appearance of your new lookup view
var layoutXml =
"
"
"
"
"
//Add your new view to the Lookup's set of availabe views and make it the default view
Xrm.Page.getControl("new_statelookupid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, viewIsDefault);
}