Sum Calculated Field from Related List Items
Hi,
I would like to sum the related list items on a form. However, the column to be sumed is a calculated column. I'm using the following but my answer still comes up as '0'.
calculateTotals();
function calculateTotals(){
var subtotal= 0;
var rows=fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item){
subtotal += parseFloat(item['SubTotalScore.']);
});
console.log("Sub-Total Score " +subtotal);
}end calculateSubtotals
fd.onsubmit(function(){
fd.field('B7bTotalScore').value(subtotal);
});
Help please. Thanks
I would like to sum the related list items on a form. However, the column to be sumed is a calculated column. I'm using the following but my answer still comes up as '0'.
calculateTotals();
function calculateTotals(){
var subtotal= 0;
var rows=fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item){
subtotal += parseFloat(item['SubTotalScore.']);
});
console.log("Sub-Total Score " +subtotal);
}end calculateSubtotals
fd.onsubmit(function(){
fd.field('B7bTotalScore').value(subtotal);
});
Help please. Thanks
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear enillrae,
The command should work with Calculated fields, make sure that the Intenal Name of the field is correct (do you need the dot in the end?).
Also, what about the console? Do you get 0 in the console as well? The issue I see here is that subtotal variable is initialized inside calculateTotals() function, but then used outside of it in fd.onsubmit event, but it would be unacessible by this point, so this might be the reason you are not getting the correct result.
Something like this might work better:
The command should work with Calculated fields, make sure that the Intenal Name of the field is correct (do you need the dot in the end?).
Also, what about the console? Do you get 0 in the console as well? The issue I see here is that subtotal variable is initialized inside calculateTotals() function, but then used outside of it in fd.onsubmit event, but it would be unacessible by this point, so this might be the reason you are not getting the correct result.
Something like this might work better:
Code: Select all
var subtotal= 0;
function calculateTotals(){
var rows=fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item){
subtotal += parseFloat(item['SubTotalScore.']);
});
console.log("Sub-Total Score " +subtotal);
}
calculateTotals();
fd.onsubmit(function(){
fd.field('B7bTotalScore').value(subtotal);
});
Cheers
-
- Posts: 11
- Joined: Tue Jun 10, 2014
Hello Nikita, I have tried this code and it works - sort of. I've seen this posted a few times and I've never seen a proper solution. Here goes:
If I use a related-items control on an edit form and I create new items, and then submit it, it always counts the rows twice. If go back an edit the edit form, it properly counts the rows once.
Do you understand what I mean?
Thanks,
If I use a related-items control on an edit form and I create new items, and then submit it, it always counts the rows twice. If go back an edit the edit form, it properly counts the rows once.
Do you understand what I mean?
Thanks,
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear bnunweiler,
It does happen occasionally that items get duplicates, but it can be resolved by filtering rows to only the unique ones, like this:
It does happen occasionally that items get duplicates, but it can be resolved by filtering rows to only the unique ones, like this:
Code: Select all
var rows = fd.relatedItems(0).data('ctx').ListData.Row;
var dupes = {};
var singles = [];
$.each(rows, function(i, el) {
if (!dupes[el.ID]) {
dupes[el.ID] = true;
singles.push(el);
}
});
//do the calculations for singles variable, instead of rows
Cheers
Hello Nikita, I am attempting the above, but cant seem to get it working.
I do have a few possible hurdles though, if you could please let me know if it is something that i need to change or if i can code around it?
I have 2 related-items lists, in 2 separate tabs. Currently i am doing a work around on the child-level, where a workflow updates the total on the parent list. It works fine, except when users start deleting items on the child-list and the totals are no longer accurate.
Apart from them being in tabs (for GUI purposes to save space) the names of the fields on both child-lists are exactly the same.
The script you offered above mentions fd.relatedItems(0), which i am assuming would be the first child list and fd.relatedItems(1) would become the second. How would i positively recognize which is which?
Long story short, i need to take the totals added in my Quotation Entries child-list (field called Amount) and update it to my parent list (field called Quote Value) The process for the Order part is exactly the same as the Quotation part.
Assistance with this would be greatly appreciated.
Thank you.
I do have a few possible hurdles though, if you could please let me know if it is something that i need to change or if i can code around it?
I have 2 related-items lists, in 2 separate tabs. Currently i am doing a work around on the child-level, where a workflow updates the total on the parent list. It works fine, except when users start deleting items on the child-list and the totals are no longer accurate.
Apart from them being in tabs (for GUI purposes to save space) the names of the fields on both child-lists are exactly the same.
The script you offered above mentions fd.relatedItems(0), which i am assuming would be the first child list and fd.relatedItems(1) would become the second. How would i positively recognize which is which?
Long story short, i need to take the totals added in my Quotation Entries child-list (field called Amount) and update it to my parent list (field called Quote Value) The process for the Order part is exactly the same as the Quotation part.
Assistance with this would be greatly appreciated.
Thank you.
- Attachments
-
- Quotation Value.jpg (52.79 KiB) Viewed 4371 times
-
- Quotation Amount.jpg (78.9 KiB) Viewed 4371 times
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Wikus,
To find out which Related Items you select, try to enter this code into browser's console:
It will return a JQuery element which corresponds to the particular Related Items control on the form. If you place the mouse cursor over it, it will highlight (done in Chrome):
You can also check if all rows are available, if you enter the following code into the console:
To find out which Related Items you select, try to enter this code into browser's console:
Code: Select all
fd.relatedItems(NUMBER_HERE);
Code: Select all
fd.relatedItems(NUMBER_HERE).data('ctx').ListData.Row;
Cheers
Thank you for the clarification Nikita.
It must be my poor coding skills but I dont seem to be able to pin this down.
I have created a test list just to try work this out, but not sure where I am going wrong.
Please could you kindly just check if I am on the right track with the fields I am updating?
Thank you.
Edit. I see it doesnt show the bold text, but i am looking for the values for Field_I_Am_Totalling and Field_To_Update_With_Total
It must be my poor coding skills but I dont seem to be able to pin this down.
I have created a test list just to try work this out, but not sure where I am going wrong.
Code: Select all
var subtotal= 0;
function calculateTotals(){
var rows=fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item){
subtotal += parseFloat(item['[b]Field_I_Am_Totalling[/b].']);
});
console.log("[b]Field_To_Update_With_Total[/b] " +subtotal);
}
calculateTotals();
fd.onsubmit(function(){
fd.field('[b]Field_To_Update_With_Total[/b]').value(subtotal);
});
Thank you.
Edit. I see it doesnt show the bold text, but i am looking for the values for Field_I_Am_Totalling and Field_To_Update_With_Total
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Wikus,
The code looks more or less correct, please, check the browser's console for errors. I suspect that the Internal Names of the fields might be wrong. Also, remember that this code doesn't auto update the subtotal - it only calculates it when you save the form.
To check the InternalNames of the fields, you can go to List Settings, select the appropriate column and check its URL: P.S. Don't forget to run the actual calculation once again before saving the form:
The code looks more or less correct, please, check the browser's console for errors. I suspect that the Internal Names of the fields might be wrong. Also, remember that this code doesn't auto update the subtotal - it only calculates it when you save the form.
To check the InternalNames of the fields, you can go to List Settings, select the appropriate column and check its URL: P.S. Don't forget to run the actual calculation once again before saving the form:
Code: Select all
var subtotal= 0;
function calculateTotals(){
var rows=fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item){
subtotal += parseFloat(item['Field_I_Am_Totalling']);
});
console.log("Field_To_Update_With_Total " +subtotal);
}
calculateTotals();
fd.onsubmit(function(){
//add this here:
calculateTotals();
fd.field('Field_To_Update_With_Total').value(subtotal);
});
Cheers
-
- Information
-
Who is online
Users browsing this forum: No registered users and 20 guests