Editar

Compartilhar via


addCustomFilter (Client API reference)

Adds filters to the results displayed in the lookup. Each filter will be combined with any previously added filters as an AND condition.

Control types supported

Lookup

Syntax

formContext.getControl(arg).addCustomFilter(filter, entityLogicalName)

Parameters

  • filter: String. The fetchXml filter element to apply. For example:

    <filter type="and">
      <condition attribute="address1_city" operator="eq" value="Redmond" />
    </filter>
    
  • entityLogicalName: (Optional) String. If this is set, the filter only applies to that table type. Otherwise, it applies to all types of tables returned.

Remarks

This method can only be used in a function in an event handler for the Lookup Control PreSearch Event.

Example

The following code sample is for the Opportunity form Account (parentaccountid) lookup. When the Sdk.setParentAccountIdFilter function is set in the form Onload event handler, the Sdk.filterCustomAccounts function is added to the PreSearch event for that lookup. Remember to select the option to pass in the execution context when setting the function in the form Onload event handler. The result is that only accounts with the Category (accountcategorycode) value of Preferred Customer (1) will be returned.

// A namespace defined for SDK sample code
// You should define a unique namespace for your libraries
var Sdk = window.Sdk || {};

// set 'Sdk.setParentAccountIdFilter' in the Opportunity form onload event handler
Sdk.setParentAccountIdFilter = function (executionContext) {

    // get the form context
    formContext = executionContext.getFormContext();
    formContext.getControl("parentaccountid").addPreSearch(Sdk.filterCustomerAccounts);
}

Sdk.filterCustomerAccounts = function () {

    // Only show accounts with the type 'Preferred Customer'
    var customerAccountFilter = "<filter type='and'><condition attribute='accountcategorycode' operator='eq' value='1'/></filter>";
    formContext.getControl("parentaccountid").addCustomFilter(customerAccountFilter, "account");
}

addPreSearch
formContext