Creating Custom Lookup Dialogs in Microsoft Dynamics 365

 Customizing user interactions in Microsoft Dynamics 365 often involves more than just data entry; it requires tailored solutions for record selection based on specific criteria. This article demonstrates how to create a custom lookup dialog that can be adapted for any entity, enabling users to select records filtered by custom conditions using JavaScript and the Xrm.Utility API

.

General Approach to Custom Lookup Dialogs

The ability to dynamically filter and select records in a lookup dialog enhances the application's usability and ensures data integrity. Here’s a step-by-step guide to implementing this functionality:

Step 1: Define the Lookup Function

Start by defining a JavaScript function that can be triggered from any form or button within Dynamics 365. This function will open a lookup dialog with specific filters applied.


function openCustomLookup(entityName, filters)
{
    if (!entityName)
    {
        console.log("Entity name is not provided.");
        return;
    }

    var lookupOptions = {
        defaultEntityType: entityName,
        entityTypes: [entityName],
        allowMultiSelect: false,
        filters: filters
    };

    Xrm.Utility.lookupObjects(lookupOptions).then(
        function (results)
        {
            if (results && results.length > 0)
            {
                var selectedRecordId = results[0].id;
                console.log("Selected Record ID: " + selectedRecordId);
            }
            else
            {
                console.log("No Record Selected");
            }
        },
        function (error)
        {
            console.log("Error: " + error);
        }
    );
}


Step 2: Configure and Trigger the Lookup

Filters are essential for narrowing down the records displayed in the lookup. Define and pass filters to the function when triggering it from any script or user interaction within Dynamics 365.


// Example filters and function call
var filters = [
    {
        filterXml: `<filter>
                        <condition attribute="parentid" operator="eq" value="994f0f66-da8f-ee11-8179-000d3af4fc19" uiname="demo account" uitype="account" />
                        <condition attribute="addressnumber" operator="gt" value="2" />
                        <condition attribute="addresstypecode" operator="eq" value="1" />
                    </filter>`,
        entityLogicalName: "customeraddress" // Ensure this matches the entity used in the lookup
    }
];

// Configure filters as required and call the function with the appropriate entity name
openCustomLookup('customeraddress', filters);


Test Result:




Conclusion

Custom lookup dialogs in Dynamics 365 allow developers to build interactive and user-friendly interfaces tailored to specific business needs. By implementing the above function in your environment, you can facilitate efficient and accurate record selection.

No comments:

Post a Comment