Hi all,
I am using below code to filter lookup:
//aw - 9/7/2017 - aw - Begin
function WaterMeter_OnLoad() {
debugger;
Xrm.Page.getControl("aw_readingtype").addPreSearch(function () {
FilterRelatedReadingTypeCustomView();
});
RetrieveManager();
}
function FilterRelatedReadingTypeCustomView() {
debugger;
// set the randomly generated GUID for the view id
var viewId = "{2DFB2B35-B07C-34D1-768D-158DEEAB88E9}";
var objectTypeCode = 10188; //10188 for ReadingType....
var RelatedServiceObject = Xrm.Page.getAttribute("aw_serviceobject");
var RelatedServiceObjectId;
if (RelatedServiceObject != null) {
var RelatedValue = RelatedServiceObject.getValue();
if (RelatedValue != null) {
RelatedServiceObjectId = RelatedValue[0].id;
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
" <entity name='aw_readingtype'>" +
" <attribute name='aw_readingtypeid' />" +
" <attribute name='aw_typename' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_typename' descending='false' />" +
" <link-entity name='aw_readinggroup' from='aw_readinggroupid' to='aw_readinggroup' alias='ad'>" +
" <link-entity name='aw_serviceobjectgroup' from='aw_readinggroup' to='aw_readinggroupid' alias='ae'>" +
" <link-entity name='msdyn_customerasset' from='aw_serviceobjectgroup' to='aw_serviceobjectgroupid' alias='af'>" +
" <filter type='and'>" +
" <condition attribute='msdyn_customerassetid' operator='eq' value='" + RelatedServiceObjectId + "' />" +
" </filter>" +
" </link-entity>" +
" </link-entity>" +
" </link-entity>" +
" </entity>" +
"</fetch>";
var layoutXml = ("<layoutxml>" +
" <grid name='resultset' object='10188' jump='aw_typename' select='1' icon='1' preview='1'>" +
" <row name='result' id='aw_readingtypeid'>" +
" <cell name='aw_typename' width='300' />" +
" <cell name='createdon' width='125' />" +
" </row>" +
" </grid>" +
" </layoutxml>").toLowerCase();
// add the custom view for the lookup field
if (Xrm.Page.getControl("aw_readingtype")) {
Xrm.Page.getControl("aw_readingtype").addCustomView(viewId, "aw_readingtype", "Filtered Reading Type", fetchXml, layoutXml, true);
}
}
}
}
//aw - 9/7/2017 - aw - End
function RetrieveManager() {
debugger;
var RelatedServiceObject = Xrm.Page.getAttribute("aw_serviceobject");
var RelatedServiceObjectId;
var RelatedValue = RelatedServiceObject.getValue();
RelatedServiceObjectId = RelatedValue[0].id;
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
" <entity name='aw_readingtype'>" +
" <attribute name='aw_readingtypeid' />" +
" <attribute name='aw_typename' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_typename' descending='false' />" +
" <link-entity name='aw_readinggroup' from='aw_readinggroupid' to='aw_readinggroup' alias='ad'>" +
" <link-entity name='aw_serviceobjectgroup' from='aw_readinggroup' to='aw_readinggroupid' alias='ae'>" +
" <link-entity name='msdyn_customerasset' from='aw_serviceobjectgroup' to='aw_serviceobjectgroupid' alias='af'>" +
" <filter type='and'>" +
" <condition attribute='msdyn_customerassetid' operator='eq' value='" + RelatedServiceObjectId + "' />" +
" </filter>" +
" </link-entity>" +
" </link-entity>" +
" </link-entity>" +
" </entity>" +
"</fetch>";
encodedFetchXml = encodeURI(fetchXml),
queryPath = "/api/data/v8.2/aw_readingtypes?fetchXml=" + encodedFetchXml,//You must change the entity name including s and you must see the version of your web api as here v8.2
requestPath = Xrm.Page.context.getClientUrl() + queryPath,
req = new XMLHttpRequest();
req.open("GET", requestPath, false);// Third parameter is true than this is Asynchronous request, and when false then synchronous request
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
this.onreadystatechange = null;
if (this.status === 200) {
var returned = JSON.parse(this.responseText),
results = returned.value;
for (var i = 0; i < results.length; i++) {
}
} else {
alert(this.statusText);
}
}
};
req.send();
}
and getting below error. I not able to find my mistake. Please let me know What is wrong?
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #0F89F8F9Detail:
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
<ActivityId>d37c1060-5529-47e9-a50e-7ecd624ed1ce</ActivityId>
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
<Message>System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #0F89F8F9</Message>
<Timestamp>2017-09-10T14:50:45.2909901Z</Timestamp>
<ExceptionRetriable>false</ExceptionRetriable>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText i:nil="true" />
</OrganizationServiceFault>
Thanks
Regards,
AW
*This post is locked for comments