Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Save editable grid data from JavaScript

Posted on by 10

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.

2337.pastedimage1593268445694v1.png

My Code:

 //get total rows from product grid
                        var productGrid = formContext.getControl('quotedetailsGrid').getGrid();
                        if (productGrid.getTotalRecordCount() > 0) {
                            var rows = productGrid.getRows();
                            rows.forEach(function (row) {

                                var totalAmt = row.getData().getEntity().attributes.get("baseamount").getValue();
                                if (totalAmt != null && totalAmt > 0) {
                                    var lineDiscount = (totalAmt * discountPct) / 100;
                                    var discountedTotal = totalAmt - lineDiscount;
                                    //apply the discount to all the items in product grid
                                    row.getData().getEntity().attributes.get("manualdiscountamount").setValue(lineDiscount);
                                }
                            });

This function will call in Quote form Discount Pct OnChange event.

Thank You.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Save editable grid data from JavaScript

    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.

    8311.png

    3.Associate this js to the Editable Grid.

    (1) Click Add button to lookup record dialog box, search and select file you created.

    12641.png

    (2) To the Handler Properties dialog box

    0172.png

    (3) Click Add button.

    0871.png

    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.

    5852.png

    After:

    clicking Save&Close button and open this Quote again, you will find that changed value has already saved in the database.

    1033.png

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans