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 @Margo 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

Hello,
This will give total of all records,
I want total of some particular records that have unique loginID field,
like just 4 or 5 records total that have loginID as abc@gmail.com

Thanks.

Hello @harshp924,

You can add any condition to the code to calculate totals only for specific records. For instance, the code below calculates the total price of records whose status is equal to 'active'.

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].Price && value[i].Status == 'active')
              total += parseInt(value[i].Value.replace(/[^0-9.]/g, ""));
        }
    }
    fd.field('Total').value = total;
};
2 Likes

Thanks @Margo,
Its works for me...!

Hello @Margo,
I am trying to pass data from parent form to child form,
Here is Document that i was gone through,
In Parent Form its works completed fine, i can get UserID from Parent Form to child form,
But when i am using Edit in the parent form, i can not get value from parent to child,
I am useing SharePoint online,

Here is my code,

var parentForm = window.top.fd;
console.log(parentForm,"Parentform");
  if (parentForm) {

    //Set field values with the values from the parent on form load
    fd.field('Myid').value = parentForm.field('Mylogin').value;
    //disable mytumid 
    fd.field('Myid').disabled = true;

  }

When I try to open child form in Parent Form 'New', is shows console as below Screenshot,
but in Parent form 'Edit', it shows as undefine.


Have a good day,
Thanks in Advance.

Hello @harshp924,

Please make sure that you've defined fd globally in both New and Edit parent forms with this code:

window.fd = fd;

Thanks, @Margo,
It's Completely worked for me...!
Cheers

1 Like