Getting Sums from related List view

Hiya wonderful people!

I have a couple of issues:

I have a view that has a sum on it:

But in the related view there is no sum :frowning:

Is there a way to get the sum showing in the related view?

Additionationally, is there any way to get that sum and place it in a field?

Thanking you much! :slight_smile:

Hello @Jamal_Smith-Graham !

To calculate the sum and show it in the form is the only option to display the total by the column.

To do so, please use the code below. Also, you can disable the Total field so a user will not set it manually.

Please do the following changes in the code:

  • Replace {SPDataTable0} with the List or Library Control internal Name.

  • Change Value in the code (in places like value[i].Value and value[i].Value.replace(/$/g,'')) with the InternalName of the Total Sale Price column. You can check the InternalName in the designer, if you open the child form:

     function calcTotal() {
    
    var value = fd.control('{SPDataTable0}').widget.dataItems();
    var total = 0;
    if(value){
        for (var i = 0; i < value.length; i++){
            if(value[i].Value)
              total += parseInt(value[i].Value.replace(/[^0-9.]/g, ""));
        }
    }
    fd.field('Total').value = total;
    }
    var myVar = 0;
    
    fd.spRendered(function() {
    
    // disable the field
    fd.field('Total').disabled = true;
    
    //refreshing the  total
    var myVar = setInterval(calcTotal, 2000);
    
    }); 
    
    fd.spBeforeSave(function(spForm) {
        var myVar = setInterval(calcTotal, 2000);
        calcTotal();
        clearInterval(myVar);
    });
2 Likes

Thanks for this @mnikitina it worked! Pefectly!

You are a :star: :slight_smile:

1 Like

The line of code that attempts to get the table is returning an error: Cannot read property 'widget' of undefined

I have verified that I have the table name spelled correctly so I know I have that right.

Here is my code.

function calcTotal() {
    var value = fd.control('{SPDataTable1}').widget.dataItems();
    var total = 0;
    if(value){
        for (var i = 0; i < value.length; i++){
            if(value[i].Value)
              total += parseInt(value[i].Value.replace(/[^0-9.]/g, ""));
        }
    }
    fd.field('Numeric1').value = total;

};

Hello @smithme,

Please remove braces {} from the filed name. I've used them to highlight that the field name should be changed, now I think it was a bad idea :sweat_smile:

function calcTotal() {
    var value = fd.control('SPDataTable1').widget.dataItems();
    var total = 0;
    if(value){
        for (var i = 0; i < value.length; i++){
            if(value[i].Value)
              total += parseInt(value[i].Value.replace(/[^0-9.]/g, ""));
        }
    }
    fd.field('Numeric1').value = total;
};
1 Like