Validate Sub-Grid Count:

In this blog, we will walk through the process of implementing a JavaScript function to compare the subgrid count with a value stored in a lookup field, raising an error message when the limit is exceeded.

In Below Image , There is a Entity called “Participant” this Entity Have Field Called Count  here we have gave Count = 1

Validate Sub-Grid Count

Below  image displays another entity named “Hotel”, which includes a Participants subgrid. In this subgrid, two records have been successfully created

Now, Add java script code for Show the Error Message if the Subgrid Record Count is Exceeds the limit Add the java script code is Web Resource of the Hotel Entity And Also call the function in OnLoad and OnChange of Participant Field in Hotel then save And Publish.

Here is The Code,

function validateSubgridCount(executionContext) {
    // Get the form context
    var formContext = executionContext.getFormContext();

    // Get the lookup field value
    var lookupField = formContext.getAttribute(“vijay_participants”); // Replace with your lookup field name
    if (!lookupField || !lookupField.getValue()) return;

    // Retrieve the count field value from the lookup record
    var lookupId = lookupField.getValue()[0].id.replace(“{“, “”).replace(“}”, “”);
    Xrm.WebApi.retrieveRecord(“vijay_participants”, lookupId, “?$select=vijay_count”).then(
        function(result) {
            var lookupCount = result.vijay_count; // Correct field name for count

            // Prepare FetchXML to get related records in the subgrid
            var fetchXml = `<fetch aggregate=”true”>
<entity name=”vijay_hotel”> <!– Replace with your subgrid entity name –>
<attribute name=”vijay_hotelid” alias=”recordCount” aggregate=”count” />
<filter>
<condition attribute=”vijay_participants” operator=”eq” value=”${lookupId}” /> <!– Dynamic lookup ID –>
</filter>
</entity>
</fetch>`;

            // Execute the FetchXML query
            Xrm.WebApi.retrieveMultipleRecords(“vijay_hotel”, “?fetchXml=” + encodeURIComponent(fetchXml)).then(
                function(response) {
                    var subgridRecordCount = response.entities.length > 0 ? response.entities[0].recordCount : 0;

                    // Check if subgrid count exceeds the lookup count
                    if (subgridRecordCount > lookupCount) {
                        // Show error if count is exceeded
                        formContext.ui.setFormNotification(
                            “The number of records in the subgrid exceeds the allowed count in the lookup record.”,
                            “ERROR”,
                            “count_exceeded”
                        );
                    } else {
                        // Clear notification if within limit
                        formContext.ui.clearFormNotification(“count_exceeded”);
                    }
                },
                function(error) {
                    console.log(“Error retrieving subgrid records: ” + error.message);
                }
            );
        },
        function(error) {
            console.log(“Error retrieving lookup record: ” + error.message);
        }
    );
}

 

 

Now Save And Publish .

In Above Images ,

In participant Entity Count =1  We have gave

And , In Participant Entity we have created two records in subgrid then we will get the message like this below,

 

For any Help or Queries Contact us on info@crmonce.com or +91 9493926112.

https://www.crmonce.com/how-to-update-date-fields-in-dynamics-365-crm/