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.
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
Hi Elpibato,
Because I don't know the details of your business logic, so I'm just add it as Form OnSave event.
Thanks.
Regards,
Leah Ju
Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.
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?
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:
After:
Thanks.
Regards,
Leah Ju
Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.
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.
Also, have you tried using Business Rule to achieve your requirement.
To be honest, this would be much better accomplished using a plugin or something.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156