Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Java Script on Subgrid

Posted on by 160

Hi There,

I am trying to add a java script to a Subgrid view on form. I want to change the Discount Amount and set a formular for this field.

pastedimage1600809211207v1.png

I dont know how to get this Subgrid using Java Script: it is the logic which i want to add:

var pricePerUnit = formContext.getAttribute ("input").value;
var percentage = formContext.getAttribute ("input").value;
var discountPercentage = percentage / 100;

 formContext.getAttribute("input").value = pricePerUnit * discountPercentage

Can anybody please help me?

Best Regards

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Java Script on Subgrid

    Hi Elpibato,

    Because I don't know the details of your business logic, so I'm just add it as Form OnSave event.

    pastedimage1600850786551v1.png

    Thanks. 

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Elpibato Profile Picture
    Elpibato 160 on at
    RE: Java Script on Subgrid

    Thank you for your Answer!

    Did you add this Code to the Subgrid Event on change? Can you pleas tell me, Which field did you use?

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Java Script on Subgrid

    Hi Elpibato,

    You can refer following js 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 priceperunit = row.getData().getEntity().attributes.get("priceperunit").getValue();//get value from sub-grid
                var discount = priceperunit * 0.2;
                var gridColumns = row.getData().getEntity().getEntityReference();
                //console.log(gridColumns);
                var id = gridColumns.id.replace('{', '').replace('}', '');
                var data =
                {
                    "manualdiscountamount": discount
                }
                // update the sub-grid record
                Xrm.WebApi.updateRecord("quotedetail", id, data).then(
                    function success(result) {
                        console.log("Quote line updated");
                    },
                    function (error) {
                        console.log(error.message);
                    }
                );
            });
        }
    }
    

    When I use setValue() to set discount field, I found that I can’t 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.

    So I use web API to update it, it works successfully.

    Test Result:

    Before:

    pastedimage1600841627869v1.png   

    After:

    pastedimage1600841767685v1.png

    Thanks. 

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Java Script on Subgrid

    Hi,

    I believe this is editable grid you added on form.

    Please look at below article which will provide you steps you add JS on Editable grid.

    neilparkhurst.com/.../

    Also, have you tried using Business Rule to achieve your requirement.

    Please mark my answer verified if i were helpful

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Java Script on Subgrid

    To be honest, this would be much better accomplished using a plugin or something.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans