Hi Team,
Can you please help me to save the edited value in Quote product grid from the JavaScript.
Currently, I've developed a JavaScript function to update the discount value in Quote product grid. In my Quote main form there is a field to enter discount percentage. When change the discount pct, all the discount values in Quote product editable grid will be updated. Now I want to save the updated values in Quote product grid. I tried to save it by click on Save Button on the grid, but it didn't work. Even the save event didn't fired.
Can someone please advice me to do this.
My Code:
This function will call in Quote form Discount Pct OnChange event.
Thank You.
Hi AkilaCham87,
By clicking save button, the sub-grid data that changed by JS cannot be saved. This is by design.
However, you can try to use Web Api to update sub-grid records that you have changed if you want to achieve it.
Please refer it. https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/updaterecord
Here are steps.
1.The following is my example code.
function update(executionContext)
{
var formContext = executionContext.getFormContext();
var productGrid = formContext.getControl('quotedetailsGrid').getGrid();
if (productGrid.getTotalRecordCount() > 0) {
var gridRows = productGrid.getRows();
gridRows.forEach(function(row, i) {
var discount = row.getData().getEntity().attributes.get("manualdiscountamount").getValue();
var gridColumns = row.getData().getEntity().getEntityReference();
//console.log(gridColumns);
var id = gridColumns.id.replace('{', '').replace('}', '');
var data =
{
"manualdiscountamount": discount
}
// update the record
Xrm.WebApi.updateRecord("quotedetail", id, data).then(
function success(result) {
console.log("Account updated");
},
function(error) {
console.log(error.message);
}
);
});
}
}
2. Add your JavaScript code in a Script web resource.
3.Associate this js to the Editable Grid.
(1) Click Add button to lookup record dialog box, search and select file you created.
(2) To the Handler Properties dialog box
(3) Click Add button.
4.Test.
Before: The Discount value is changed by JS code and it cannot be saved. After clicking save button, it will show the original value when you open the Quote.
After:
clicking Save&Close button and open this Quote again, you will find that changed value has already saved in the database.
Regards,
Clofly