Skip to main content

Dynamics V9 – Filter Customer Lookup – Accounts Only

JavaScript function for filtering customer lookups. For example in situations where cases are only created for accounts only. This also prevents contact records from appearing when the user searches in the lookup.

I adapted the code from a blog post from power objects here

AddAccountNamePresearch = function (executionContext) {
    var formContext = executionContext.getFormContext();
    var customerAccountFilter = "<filter type="and"><condition attribute="contactid" operator="null"></condition></filter>";
    formContext.getControl("customerid").addPreSearch(function () {
        formContext.getControl("customerid").addCustomFilter(customerAccountFilter, 'contact');
    });

};

If you have multiple customerid controls, for example, one on the form and the other on a BPF, make sure to filter them all by changing the code to the below

AddAccountNamePresearch = function (executionContext) {
    var formContext = executionContext.getFormContext();
    var customerAccountFilter = "<filter type="and"><condition attribute="contactid" operator="null"></condition></filter>";
    //add the filters foreach customerid control
    formContext.getAttribute('customerid').controls.forEach(function (ctrl) {
        ctrl.addCustomFilter(customerAccountFilter, 'contact');
    });
};

Charles Osei

Dynamics 365 Consultant @ Hitachi MCSE Business Applications Computer Science – University of Lincoln 2017 Nottingham D365/CRM user group Chapter Leader TDG – Community leader Twitter @charlied3v LinedIn https://www.linkedin.com/in/charlesosei/

Leave a Reply