Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Filter lookup

(0) ShareShare
ReportReport
Posted on by 12,081 Moderator

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

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Filter lookup

    Hi Aric Levin

    I am trying to filter lookup.

    Thanks

    Regards,

    AW

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Filter lookup

    Hi Alex Shlega

    When I open my lookup I have this error.

    Thanks

    Regards,

    AW

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Filter lookup

    Hi,

     do you know if the error is happening in the RetrieveManager or in the FilterRelatedReadingTypeCustomView? 

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Filter lookup

    Hi Prateek

    no luck. 

    Any one is here to help?

    Thanks

    Regards,

    AW

  • Suggested answer
    prt33k Profile Picture
    prt33k 6,907 on at
    RE: Filter lookup

    If you are using addCustomView then why addPreSearch?

    Cant this work:

    function WaterMeter_OnLoad() {

    RetrieveManager();

    }

    Register FilterRelatedReadingTypeCustomView(); on change event of the lookup.

    Thanks,

    Prateek

  • Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: Filter lookup

    That's fine, but in your custom filter for a lookup only specify the filter portion, and not the entire fetchXml.

    Design it in that way.

    I am not sure, but based on your view you are trying to modify a subgrid, not a lookup control.

    Which is it?

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,081 Moderator on at
    RE: Filter lookup

    Hi Aric Levin,

    I need to filter records from multiple entities not from one entity.

    Thanks

    Regards,

    AW

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: Filter lookup

    You don't need your entire fetchXml in the presearch. See below example:

    function addPresearchToControl()

    {

           Xrm.Page.getControl("bgx_lookupid").addPreSearch(function () {

               addLookupFilter(recordTypeCode);

           });

    }

    function addLookupFilter(recordTypeCode)

    {

       var fetchXml = "<filter type='and'><condition attribute='bgx_recordtypecode' operator='eq' value='" + recordTypeCode + "' /></filter>";

       Xrm.Page.getControl("bgx_lookupid").addCustomFilter(fetchXml);

    }

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans