[TIPS] Filter Lookup type Field values in Dynamics 365/CE
Refernce: 1 2
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 supportedLookup
SyntaxformContext.getControl(arg).addCustomFilter(filter, entityLogicalName)
Parameters note : You may use XrmToolBox --> FetchXML Builder to create the above XML
Click to download XrmToolBox.
This method can only be used in a function in an event handler for the Lookup Control PreSearch Event .
ExampleThe following code sample is for the Requesting Warehouse lookup field.
Its value needs to be filtered by the value of another field: Request Made By Fire.
If Request Made By Fire is set to Yes, Requesting Warehouse field should only show the warehouses whose "Wildfire" field is not null.
Solution 1:
//Added by Forrest on Jan 4, 2023.
//Purpose: filter requesting warehouse field
function filterRequestingWarehouse (executionContext )
{
debugger ;
var formContext = executionContext .getFormContext ();
formContext .getControl ("qt_msdyn_warehouse" ).addPreSearch (filterWarehouse );
}
function filterWarehouse (executionContext )
{
debugger ;
var formContext = executionContext .getFormContext ();
var warehouseFilter = null ;
//Get Request Made By Fire field value
var reqMadeByFire = formContext .getAttribute ("qt_requestmadebyfire" ) == null ? null : formContext .getAttribute ("qt_requestmadebyfire" ).getValue ();
//If Not Made by Fire
if (reqMadeByFire == 0 )
{
warehouseFilter = "<filter type='and'><condition attribute='qt_wildfire' operator='null'/></filter>" ;
}
else if (reqMadeByFire == 1 )
{
warehouseFilter = "<filter type='and'><condition attribute='qt_wildfire' operator='not-null'/></filter>" ;
}
else
{
return ;
}
formContext .getControl ("qt_msdyn_warehouse" ).addCustomFilter (warehouseFilter , "msdyn_warehouse" );
}
Solution 2:
Use setDefaultView to set the default view for the lookup control dialog box.
Syntax formContext.getControl(arg).setDefaultView(viewId);
ParameterName Type Required Description viewId String Yes The ID of the view to be set as the default view.
//Added by Forrest on Jan 4, 2023.
//Purpose: filter requesting warehouse field
function filterRequestingWarehouse2 (executionContext )
{
debugger ;
var formContext = executionContext .getFormContext ();
var warehouseFilter = null ;
//Get Request Made By Fire field value
var reqMadeByFire = formContext .getAttribute ("qt_requestmadebyfire" ) == null ? null : formContext .getAttribute ("qt_requestmadebyfire" ).getValue ();
//If Not Made by Fire
if (reqMadeByFire == 0 )
{
formContext .getControl ("qt_msdyn_warehouse" ).setDefaultView ("{17e51d15-7c45-ed11-bba3-0022483cbd89}" );
}
else if (reqMadeByFire == 1 )
{
formContext .getControl ("qt_msdyn_warehouse" ).setDefaultView ("{82cbccac-e922-ed11-9db2-000d3a84c8da}" );
}
else
{
return ;
}
}
VIDEO
No comments:
Post a Comment