Custom Subgrid Button in Model-driven App Using Power Platform OOB + JavaScript + Power Automate

 Custom Subgrid Button in Model-driven App Using Power Platform OOB + JavaScript + Power Automate

JavaScript Sample Code:

function updateProductViaPowerAutomate(productIds, bookableResourceId) {
    if (typeof ($) === 'undefined') {
        $ = parent.$;
        jQuery = parent.jQuery;
    }

    var flowUrl = "https://prod-13.canadacentral.logic.azure.com:443/workflows/a164415ddd054321921a5dc5bec8139e/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=_dzC4ahzoyrATA8pRscE_TBZtEeKx7EwHDBPj0mgjPA";

    var parameters = {
        "ProductIds": productIds,
        "BookableResourceId": bookableResourceId
    };

    return $.ajax({
        type: "POST",
        url: flowUrl,
        data: JSON.stringify(parameters),
        contentType: "application/json"
    });
}

function assignBookableResourceToSelectedProducts(primaryControl, selectedRecordIds) {
    var bookableResourceId = primaryControl.data.entity.getId().replace("{", "").replace("}", "");

    var productIds = selectedRecordIds.map(function(itemId) {
        return itemId.replace("{", "").replace("}", "");
    });

    updateProductViaPowerAutomate(productIds, bookableResourceId).then(function() {
        Xrm.Navigation.openAlertDialog({ text: "Courses updated successfully.", title:"Assign teachers to courses"});

        refreshSubGrids(primaryControl, ["AvailableCoursesSubGrid", "RegisteredCoursesSubGrid"]);

    }, function(error) {
        Xrm.Navigation.openAlertDialog({ text: "Error updating courses: " + JSON.stringify(error), title:"Assign teachers to courses" });
    });
}

function refreshSubGrids(primaryControl, subGridNames) {
    subGridNames.forEach(function(gridName) {
        var subGridControl = primaryControl.getControl(gridName);
        if (subGridControl && subGridControl.refresh) {
            subGridControl.refresh();
        }
    });
}

No comments:

Post a Comment