How to convert UTC time to Local time in Dynamics 365 using JavaScript

 How to convert UTC time to Local time in Dynamics 365 using JavaScript

[TIPS] 3 Ways to Filter Subgrid

 [TIPS] 3 Ways to Filter Subgrid

  1. View: Edit Filter Criteria
  2. JavaScript
  3. Plugin 

       View: Edit Filter Criteria
  • No customization
  • Out of the Box feature
JavaScript
  • Easier to implement
  • For medium complex requirements

Sample Code:

function onLoadFunction(executionContext)
{
    var formContext = executionContext.getFormContext(); // get the form context
    var tabContext = formContext.ui.tabs.get("tab_Bed_Assignment"); // get the tab context by its name
 
    // Register the event handler for the tab's state change event
    tabContext.addTabStateChange(filterSubgridBedAssignments);
    filterSubgridBedAssignments(executionContext);
 
   
   
}
 
function filterSubgridBedAssignments(executionContext)
{
    var formContext = executionContext.getFormContext();
    var currentBooking = formContext.getAttribute("tri_bookingid").getValue();
    var subgrid = formContext.getControl("subgrid_BedAssignments");
   
    //Check if Current Booking Lookup Field is not null
    if (currentBooking != null)
    {
        //Get Lookup Field Id
        var currentBookingId = currentBooking[0].id.replace("{", "").replace("}", "");
 
        if (subgrid)
        {
            //Set Filter XML for Subgrid
            var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"
            + "<entity name='tri_bedassignment'>"
            + "<filter type='and'>"
            + "<condition attribute='tri_bookingid' operator='eq' value='" + currentBookingId + "' />"
            + "</filter>"
            + "</entity>"
            + "</fetch>";
 
            subgrid.setFilterXml(fetchXml);
            subgrid.refresh();
        }        
    }
    else
    {
        subgrid.setVisible(false);
    }
}


Plugin
  • For higher complex requirements

Sample Code:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

public class Filter_MovementsPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        tracingService.Trace("Filter_MovementsPlugin: Plugin started.");

        if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is FetchExpression fetchExpression)
        {
            var fetchXmlDoc = XDocument.Parse(fetchExpression.Query);
            var entityNode = fetchXmlDoc.Descendants("entity").FirstOrDefault();
            var attributeLatestOnly = fetchXmlDoc.Descendants("attribute").FirstOrDefault(attr => attr.Attribute("name")?.Value == "kc_latestonly");

            if (attributeLatestOnly == null)
            {
                tracingService.Trace("Filter_MovementsPlugin: required attribute not found in FetchXML query. Skipping Latest Movement Filter plugin execution.");
                return;
            }

            if (entityNode != null && entityNode.Attribute("name")?.Value == "tri_offendermovement")
            {
                tracingService.Trace("Filter_MovementsPlugin: tri_offendermovement entity found.");

                EntityCollection originalResults = context.OutputParameters["BusinessEntityCollection"] as EntityCollection;

                // Filter the original results to include only the latest created records for each subject
                var modifiedResults = new EntityCollection();
                modifiedResults.EntityName = "tri_offendermovement";

                var subjects = originalResults.Entities.Select(e => e.GetAttributeValue<EntityReference>("tri_offenderid")).Distinct();

                foreach (var subject in subjects)
                {
                    var subjectId = subject.Id;

                    // Get the latest created record for each subject from the original results
                    var latestCreatedEntity = originalResults.Entities
                        .Where(e => e.GetAttributeValue<EntityReference>("tri_offenderid").Id == subjectId)
                        .OrderByDescending(e => e.GetAttributeValue<DateTime>("createdon"))
                        .FirstOrDefault();

                    if (latestCreatedEntity != null)
                    {
                        modifiedResults.Entities.Add(latestCreatedEntity);
                    }
                }

                // Set the modified results as the output of the RetrieveMultiple message
                context.OutputParameters["BusinessEntityCollection"] = modifiedResults;
            }
            else
            {
                tracingService.Trace("Filter_MovementsPlugin: tri_offendermovement entity not found.");
            }
        }
        else
        {
            tracingService.Trace("Filter_MovementsPlugin: FetchExpression not detected.");
        }
        tracingService.Trace("Filter_MovementsPlugin: Plugin execution completed.");
    }
}




[TIPS] How to get Attribute/Field value without executionContext input

 [TIPS] How to get Attribute/Field value without executionContext input

[TIPS] Difference between formContext.data.entity.addOnLoad() and Add function to form on load event directly

 [TIPS] Difference between formContext.data.entity.addOnLoad() and Add function to form on load event directly

[TIPS] Difference between Team privileges only and Direct User/Basic access level and Team privileges in Dynamics 365 Security Role Setting

 [TIPS] Difference between Team privileges only and Direct User/Basic access level and Team privileges in Dynamics 365 Security Role Setting

Reference: 1

[TIPS] Dynamics 365 / Power Apps / Power Platform Version Control

 [TIPS] Dynamics 365 / Power Apps / Power Platform Version Control

Reference: 1

[TIPS] Create Power Automate Flow to integrate with ChatGPT

 [TIPS] Create Power Automate Flow to integrate with ChatGPT

[TIPS] Solution for error "Cannot read properties of undefined (reading 'getRelationship')" when click new button on Subgrid

 [TIPS] Solution for error "Cannot read properties of undefined (reading 'getRelationship')" when click new button on Subgrid

[TIPS] Add Custom Connector for ChatGPT in Power Platform

 [TIPS] Add Custom Connector for ChatGPT in Power Platform

Information needed when developing an integration between Dynamics CRM and a third-party system

Information needed when developing an integration between Dynamics CRM and a third-party system

[TIPS] View error message of SSIS output

[TIPS] View error message of SSIS output

Reference: 1 2 

[TIPS] Add connection with online Dynamics 365 in KingswaySoft /SSIS

 [TIPS] Add connection with online Dynamics 365 in KingswaySoft /SSIS

Reference 1 2

[TIPS] Use dropdown field in Canvas App to show Dataverse Table views list

 [TIPS] Use dropdown field in Canvas App to show Dataverse Table views list

[TIPS] Filter lookup type field based on multiple values acrossing multiple entities

 [TIPS] Filter lookup type field based on multiple values acrossing multiple entities

Reference: 3 4

Grids and subgrids in model-driven apps

Grids and subgrids in model-driven apps

[TIPS] Append new component to an Array

 [TIPS] Append a new component to an Array in JavaScript

[TIPS] Filter Lookup type Field values in Dynamics 365/CE

 [TIPS] Filter Lookup type Field values in Dynamics 365/CE

Relationship between work order system statuses and booking statuses

Relationship between work order system statuses and booking statuses 

Here is a table summarizing the out-of-the-box relationship between work order system statuses and booking statuses.