web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Add filter on Lookup entity field

(0) ShareShare
ReportReport
Posted on by

Capture222.PNG

I have an entity called Source Type, it's a lookup field. It's displayed in Account form, in the form has Department lookup field, when the Department changes, it will call the below code to trigger Source Type filter as follows:

var sourceField = formContext.getControl('org_sourcetype');                

sourceField.removePreSearch(filterFn);  
sourceField.addPreSearch(filterFn);
var filterFn = function (executionContext) {
    var formContext = executionContext.getFormContext();
    var filterXml = '<filter type="and"><condition attribute="name" operator="eq" value="Anonymous"/></filter>';
    formContext.getControl('org_sourcetype').addCustomFilter(filterXml); 
}


it's not working as expected. Could you help me to point out the problem cause it doesn't raise any error?

Capture333.PNG

Capture334.PNG

Capture335.PNG

Primary key is "name"

This function is triggered when Department changes:

function updateFilterByDepartment(executionContext) {
    try {
        var formContext = executionContext.getFormContext();

        var departmentValue = formContext.getAttribute('department').getValue();

        if (!departmentValue) {
            return;
        }

        formContext.getAttribute('sourcetype').setValue(null);

        var departmentId = departmentValue[0].id;
        var sourceField = formContext.getControl('sourcetype');                

        sourceField.removePreSearch(filterFn);         
        sourceField.addPreSearch(filterFn);

    } catch (e) {
        console.log(e);
    }
}


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    formContext.getControl('sourcetype').addCustomFilter(filterXml); < -- can you double confirm the schema name, because in above function you are using 'sra_sourcetype', but below is 'sourcetype'

  • Community Member Profile Picture
    on at

    Hi Wei, I updated the post again. No error on that by the way

  • Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    can you show us the field schema name?

  • Community Member Profile Picture
    on at

    Hi Wei, I updated the post. Is it right?

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    I tested the code below, and it is working fine.

    Make sure you change the below highlighted area according to your JS pattern.

    function FormOnload(executionContext) 
    {
    
        // get the form context
        formContext = executionContext.getFormContext();
        formContext.getControl("cra7a_sourcetype").addPreSearch(filtersourcetype); //according to the picture, the attribute is "sourcetype"
    }
     function filtersourcetype() 
    {
    
        
        var SourceTypeFilter = "<filter type='and'><condition attribute='cra7a_name' operator='eq' value='A'/></filter>"; //double check your attribute name
        formContext.getControl("cra7a_sourcetype").addCustomFilter(SourceTypeFilter, "cra7a_sourcetype"); //according to the picture, the attribute is "sourcetype", please also double check the entity logical name.
    }

    I have source type with record A and B.

    7673.13.PNG
    The lookup will only show 'A'
    7673.13.PNG

    Compare below code, yours and mine
    formContext.getControl('sourcetype').addCustomFilter(filterXml); <-- you are missing the logicalname
    formContext.getControl("cra7a_sourcetype").addCustomFilter(SourceTypeFilter, "cra7a_sourcetype");  

    docs.microsoft.com/.../addcustomfilter

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Try with this  -

    //Register the funtion in Onchange and Onload of the Department field /Forms
    
    function OnChangeOnloadOfdepartmentForms(executionContext)
    {
        var sourceField = formContext.getControl('sourcetype');  
        sourceField.removePreSearch(filterPriorityByMisconductFn);      
        sourceField.addPreSearch(function () {
            filterPriorityByMisconductFn(executionContext);
        });
    }

    var filterPriorityByMisconductFn = function (executionContext) { var formContext = executionContext.getFormContext(); var filterXml = "<filter type='and'><condition attribute='name' operator='eq' value='Anonymous'/></filter>"; formContext.getControl('sourcetype').addCustomFilter(filterXml); }

  • Community Member Profile Picture
    on at

    Hi Wei,

    Thank you for helping me, the problem is the function contains filter is not be hooked when user clicks on Lookup Icon so even I add one more param in addCustomFilter, it doesn't work

  • Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    can you show us the source type entity schema name and the primary key schema name?

  • Community Member Profile Picture
    on at

    Hi Wei, I updated the post to include things you suggest.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    function onchange(executionContext)

    {

       // get the form context

       formContext = executionContext.getFormContext();

       formContext.getControl("sourcetype").addPreSearch(filtersourcetype);

    }

    function filtersourcetype()

    {

       var SourceTypeFilter = "<filter type='and'><condition attribute='name' operator='eq' value='Anonymous'/></filter>"; 

       formContext.getControl("sourcetype").addCustomFilter(SourceTypeFilter, "sourcetype");

    }

    can you register above function(onchange) inside the "Department" lookup field on change event?

    Remember to tick below item too.

    5353.17.PNG

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans