I have found a simple JS function to take the values of some children, sum them and set a field on my parent form with that value.
My JS is :-
function timeout(){ setTimeout(calcCostoTotal, 3000); } function calcCostoTotal() { var grid = document.getElementById(‘Services’); var ids = grid.control.get_allRecordIds(); var sum = 0.00; var cellValue; for(i = 0; i < ids.length; i++) { var cellValue = grid.control.getCellValue(iss_salesprice,ids[i]); var number = Number(cellValue.replace(/[^0-9\.]+/g,”)); sum = sum + number; } Xrm.Page.data.entity.attributes.get(ava_tempgrossvalue).setValue(sum); }
And I have added the library to the form and called calcCostoTotal OnLoad but I get an undefined error.
Services is the name of a view on the form which shows the children
iss_salesprice is the child value
ava_tempgrossvalue is the parent field
Can anyone spot my mistake?
If this is an inappropriate approach can anyone point me in the direction of a better one please?
Thank you in advance
*This post is locked for comments
Should this line "var cellValue = grid.control.getCellValue(iss_salesprice,ids[i]);" contain the name of the child field to be summed or the label under which it appears in the grid?
When you adding grid to form you need to specify it name. Open form editor and find name of grid.
I only have a view. I'd put a grid on and hide it if I knew how.
'Services' is the name of view? You need to use name of grid. Try to debug you code and check that you can get your grid.
Uriy, they are in the correct format in CRM. I can't figure out why this wont work! Is it because Services is a view on the form?
>var grid = document.getElementById(‘Services’);
>Number(cellValue.replace(/[^0-9\.]+/g,”));
Wrong quotation marks. Change ‘ and ” to ' or "
The error says :-
There was something wrong with this field's customized event.
Field:Window
Event:onload
Error:'calcCostoTotal' is undefined.
Where I have referenced the fucntion on the form load in the properties I did not include the "()" to the fucntion call. Is that correct?
Hello Powell,
Your code looks ok.
Can you give us more details on the undefined error ? What is the log message displayed ?
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