How to retrieve the record Id of the parent form using (Form Component Control)

We were initially tasked with displaying a list of Work Order Incidents that are associated with the Resource Requirement, which, in turn, is linked to the current Booking record. To fulfil this requirement, we utilized the Form Component Control feature on the Booking form. This allowed us to incorporate a Resource Requirement form with a subgrid displaying the Work Order Incidents.

Subsequently, we received an additional requirement to hide the +Add new button on the Work Order Incidents subgrid if the Booking Status of the associated booking record is set to a Completed state. However, since the ribbon button is specific to the Work Order Incidents context, we needed to find a way to retrieve the parent context of the Booking form.

To address this challenge, we discovered that utilizing the pageContext in conjunction with the parent object would enable us to access the parent form in a supported manner. To achieve this, the following line of code can be used:

var parentEntityId = parent.Xrm.Utility.getPageContext().input.entityId;

Below is the complete example of the code that I used on the Ribbon button:

CRM.WorkOrderIncident.HideNewButton = async function (formContext) {
    var resourcerequirement = formContext.data.entity.getId();
    var parentEntityId = parent.Xrm.Utility.getPageContext().input.entityId;
    var bookingstatus = await Chg.ResourceRequirement.GetBookingStatus(parentEntityId);
    var systemstatus = bookingstatus[0]["msdyn_fieldservicestatus"];
    if (systemstatus === BOOKING_STATUS_FS_STATUS_COMPLETED) {
        return false;
    }
};

CRM.WorkOrderIncident.GetBookingStatus = async function (bookingId) {
    return new Promise(function (resolve, reject) {
        var fetchXml = "?fetchXml=<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' count='1'>" +
            "  <entity name='bookingstatus'>" +
            "    <attribute name='bookingstatusid' />" +
            "    <attribute name='msdyn_fieldservicestatus' />" +
            "    <link-entity name='bookableresourcebooking' from='bookingstatus' to='bookingstatusid' link-type='inner' alias='ac'>" +
            "      <filter type='and'>" +
            "        <condition attribute='bookableresourcebookingid' operator='eq' value='" + bookingId + "' />" +
            "      </filter>" +
            "    </link-entity>" +
            "  </entity>" +
            "</fetch>";

        Xrm.WebApi.retrieveMultipleRecords("bookingstatus", fetchXml).then(
            function success(result) {
                resolve(result.entities);
            },
            function (error) {
                resolve("error");
            }
        );
    });
}

Leave a Reply

Your email address will not be published. Required fields are marked *