In order to retrieve data of a record using OData. Let us take a scenario in which I have a A,B and Test entities, A entity form has lookup of B entity and Test entity has lookup of both A and B entity. So I have created some records in B entity and A entity. Now In my Test Entity form when I select a Lookup of A, The lookup in B must Automatically selected. Since A entity form has lookup of B entity, i.e, In A-record form, B-record is selected in lookup.
So the above snippet code can be used on change event of a A-lookup.
function customentity_onchange() {
debugger;
var lookupObject = Xrm.Page.getAttribute("schema-name-of-Alookup").getValue();
var z=lookupObject[0].id; // Guid of lookup
alert(z);
if (z != null) {
var entityA="A-entitynameSet"; //A-entity name with "Set" attached to it for Odata Query.
var field = "B-lookup_feild_name_in_A-Entity"; //lookup feild name in A entity.
var a = GetAdata(z,entityA,field);
if(a!=null)
{
//alert(a[0]["B-lookup_feild_name_in_A-Entity"].Name);
var lookupData = new Array();
var lookupItem = new Object();
lookupItem.id=a[0]["B-lookup_feild_name_in_A-Entity"].Id;
alert(lookupItem.id);
lookupItem.name = a[0]["B-lookup_feild_name_in_A-Entity"].Name;
alert(lookupItem.name);
lookupItem.entityType ="b-Entity_name";
alert(lookupItem.entityType);
lookupData[0] = lookupItem;
Xrm.Page.getAttribute("schemaname_of_b_lookup_in_TestEntity").setValue(lookupData);
//Xrm.Page.getAttribute("hrm_b").setValue(a);
}
}
}
function GetAdata(z,entityA,field) {
var serverUrl = Xrm.Page.context.getServerUrl();
var oDataUri = serverUrl + "/XRMServices/2011/OrganizationData.svc/"+entityA+"(guid'" + z + "')?$select="+field;
var jSonArray = new Array();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataUri,
async: false,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//alert(hai);
},
success: function (data, textStatus, XmlHttpRequest) {
if (data && data.d != null) {
jSonArray.push(data.d);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
//alert("Error : has occured during retrieval of the A details");
}
});
return jSonArray;
}
No comments:
Post a Comment