Introduction
Model-driven apps in Power Platform offer a powerful way to create data-centric business apps with minimal code. In this post, we’ll walk through the steps to create a custom command bar button on the Main Grid view that allows users to add a new row to a table (entity).
This guide assumes you have some basic familiarity with the Power Apps Maker Portal and Dataverse.
Step 1: Create Your Model-Driven App
- Go to Power Apps.
- Click Apps from the left navigation pane.
- Click + New app > Model-driven app.
- Give your app a name (e.g., Data Form) and click Create.
- Use the app designer to add a table (e.g., Data) to your app.
- Save and publish the app.
Step 2: Add a Custom Command bar Button
We will now add a custom button to the command bar of the Main Grid for the table.
2.1 Navigate to the App
- In the Maker Portal, go to View.
- Select your view (e.g., DataForms view).
- Click the three dots (⋮) beside the view.
- Click Edit under the Edit command bar options.
2.2 Edit the Main Grid Command Bar
- In the command designer, choose Main grid.
- Click +New and select Command.
2.3 Configure the Custom Button
- Label: Add New Row
- Icon: Choose an appropriate icon or upload a custom one.
- Action: Choose JavaScript or Modern Action (recommended).
Step 3: Add the Button Logic (Use JavaScript)
- Create a JavaScript web resource.
- Add the following function:
- Click Add Library button to open new side panel
- Click New web resource
Below is the JavaScript Code.
function AddNewRowToMainGrid(primaryControl) {
try {
// Check if primaryControl is defined (for main grid button)
if (!primaryControl || !primaryControl.data || !primaryControl.data.entity) {
console.log("No record selected. This is a new record creation.");
}
// Define the table name (logical name of your table)
var tableName = "new_dataform"; // Make sure this is correct!
// Prepare default values for the new row
var defaultData = {
"new_dateoftillcheck": null,
"new_tillid": null,
"new_nameofcurrenttillholder": "",
"new_discrepancydeficiency": "",
"new_discrepancysurplus": "",
"new_actiontakeen": "",
"new_postplusadjustmentdate": null,
"new_signatureoftillholdercheckingofficer": "",
};
//console.log("Creating record in table: " + tableName);
//console.log("Data: ", JSON.stringify(defaultData));
// Create a new record in Dataverse
Xrm.WebApi.createRecord(tableName, defaultData).then(
function success(result) {
//console.log("Record created successfully with ID: " + result.id);
// Refresh the main grid after adding the record
if (primaryControl && primaryControl.refresh) {
primaryControl.refresh();
}
else {
console.warn("primaryControl not available. Trying page refresh.");
Xrm.Page.ui.refresh(); // Fallback
}
},
function(error) {
console.error("Error creating record: ", error.message);
Xrm.Navigation.openErrorDialog({
message: "Error adding new record: " + error.message
});
}
);
}
catch (e) {
console.error("Unexpected error:", e);
Xrm.Navigation.openErrorDialog({
message: "Unexpected error occurred: " + e.message
});
}
}
Step 4: Test the Custom Button
- Open your model-driven app.
- Navigate to the DataForm Main Grid.
- Click on your custom Add New Row button.
- A new row added, allowing the user to add a new record.
Conclusion
Customizing command bars in model-driven apps can significantly enhance user experience and streamline operations. Whether using Power Fx for low-code scenarios or JavaScript for more complex logic, adding a "New Row" button makes it easier for your users to create data on the fly.
No comments:
Post a Comment