In our day to day tasks related to dynamics 365 CRM, sometimes we need to filter Subgrid dynamically (placed on an entity form) based on data of some fields on the form. In this article, I am going to explain the same using JavaScript code.
Suppose the Subgrid name is “Booking”. This grid load records from an entity “bookable resource booking”. Our requirement was to filter this grid on the fly based on specific category value selected from an option set named “Category”. Here is the complete code.
function filterBookingsGrid(executionContext) { //---Get the form context using execution context var formContext = executionContext.getFormContext(); //---Suppose there is a subgrid named "Booking" placed on your entity's form var conSubGrid = formContext.getControl("Booking"); var category = null; if (formContext.getAttribute("new_category").getValue() != null) { category = formContext.getAttribute("new_category").getValue()[0].id.replace("}", "").replace("{", ""); } //Create FetchXML for sub grid to filter records //Set Grid to Empty if Technician is Null var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' + ' <entity name="bookableresourcebooking">' + ' <attribute name="bookableresourcebookingid" />' + ' <order attribute="bookableresourcebookingid" descending="true" />' + ' <filter type="and">' + ' <condition attribute="bookableresourcebookingid" operator="null" />' + ' </filter>' + ' </entity>' + '</fetch>'; if (category != null) { //Show Related Scheduled Bookings fetchXml = '<fetch version="1.0" output-format="xml - platform" mapping="logical" distinct="false"> ' + '<entity name="bookableresourcebooking" >' + '<all-attributes />' + ' <order attribute="createdon" descending="false" /> ' + ' <filter type="and"> ' + ' <condition entityname="BookingStatus" attribute="name" operator="eq" value="Scheduled" /> ' + ' <condition attribute="new_category" operator="eq" value="' + category + '" /> ' + ' </filter> ' + '<link-entity name="bookingstatus" from="bookingstatusid" to="bookingstatus" link-type="inner" alias="BookingStatus">' + '<attribute name="bookingstatusid" />' + '</link-entity>' + '</entity > ' + '</fetch > '; } formContext.getControl("Booking").getGrid().setParameter("fetchXml", fetchXml); //Refresh grid to show filtered records only. formContext.ui.controls.get("Booking").refresh(); }
Hello M. Fasih Akbar,
Above code works perfectly fine in Dynamics 365.
But in Unified Interface – Dynamics 365, it is failing with “formContext.getControl(…).getGrid(…).setParameter is not a function” error.
Any help will be appreciated.
LikeLike
Hi,
This is known issue as per this thread: https://github.com/MicrosoftDocs/dynamics-365-customer-engagement/issues/974
Possible tweaks which you can apply are mentioned in this post by Gautam:
https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/299697/dynamics-365-unified-interface-inject-fetchxml-into-subgrid?pifragment-97030=1#responses
Another link which might help you is this
View at Medium.com
On the other hand, you can see this thread what JavaScript changes came in version 9 are listed in this link
https://sachinbansal.blog/2018/04/27/all-javascript-changes-dynamics-365-9-0/comment-page-1/
Hopefully, it will be helpful to find an alternative approach for Unified Interface.
Thanks,
M. Fasih
LikeLike