In ribbon workbench I select entity acwapp_requestform where the subgrid is, and in that subgrid I want to hide the New Request Form button when the subgrid records has status of "Draft", "In Progress", or "Submitted", if the records has status of Rejected or Approved it will still show. Below is my whole code for javascript
function hideSubgridRibbonInRequest() {
try {
var formContext = Xrm.Page;
var parentEntityName = formContext.data.entity.getEntityName().toLowerCase();
console.log("Parent Entity Name:", parentEntityName);
if (parentEntityName === "acwapp_lease") {
var subgridControl = formContext.getControl("RequestForm");
console.log("Subgrid Control:", subgridControl);
if (subgridControl && subgridControl.getGrid()) {
var gridRows = subgridControl.getGrid().getRows().get();
console.log("Grid Rows:", gridRows);
if (gridRows.length === 0) {
return true; // Show "New" button if there are no rows
}
var hideNewButton = true; // Assume initially we should hide the "New" button
for (var i = 0; i < gridRows.getLength(); i++) {
var statusValue = gridRows.get(i).getData().getEntity().attributes.getByName("acwapp_status").getValue();
console.log("Status Value:", statusValue);
// Check if status is Draft, In Progress, or Submitted
if (statusValue === 557130000 || statusValue === 557130001 || statusValue === 557130002) {
hideNewButton = false; // Found an eligible status, show the "New" button
break; // No need to check further
}
}
return hideNewButton; // Return true to hide "New" button, false to show it
}
}
return true; // Default behavior: hide "New" button
} catch (error) {
console.error("Error in hideSubgridRibbonInRequest:", error);
throw error;
}
}