locked
Display a sum of a column(field) in the view RRS feed

  • Question

  • Hi

    I would like to show the sum of a revenue field(currency) in any view of the opportunity entity(only when revenue field exist in the view) , may be at the end of the view or as a alert message on button click., but sum should change dynamically based on the filters applied or selected records.

    E.g. Here Est. Revenue to be summed and displayed.

    Kindly help me... Thanks in advance.

    Rekha.J

    Tuesday, August 19, 2014 11:44 AM

Answers

  • Hi Rekha,

    Try below code to get sum of selected records..

    function gridAmountSum()
    {
     if (document != null)
     {
      var gridBar = document.getElementById('crmGrid');
      var headers = gridBar.getElementsByTagName('TH');
      var totalAmount = 0.00;
      for (var i = 0; i < headers.length; i++)
      {
       if ( headers[i].innerText== "SumAmount") // Change column name as in grid ie., Est. Revenue.
       {
        var grid = document.getElementById('crmGrid').control;
        for (var intRowNumber = 0; intRowNumber < grid.GetRecordsFromInnerGrid().length; intRowNumber++)
        {     
         var number = grid.GetRecordsFromInnerGrid()[intRowNumber][3].cells[3].outerText; //cells[3] will be ok as sumamount in my grid is also in 3 position.

         totalAmount = (totalAmount + Number(number.replace(/[^0-9\.]+/g,"")));
        }
       }    
      }
     }
    }

    Thanks

    Suresh Sorde





    • Proposed as answer by Suresh Sorde Saturday, January 3, 2015 10:20 AM
    • Edited by Suresh Sorde Saturday, January 3, 2015 10:31 AM
    • Marked as answer by Rekha J Monday, January 5, 2015 6:15 AM
    Saturday, January 3, 2015 10:19 AM

All replies

  • Try creating a SSRS report for this purpose. You can acheive this thing in grid/iframe also however it will require alot of development.

    Regards Faisal

    Tuesday, August 19, 2014 1:03 PM
  • Hi Faisal,

    Thank you for the reply.

    We have used SSRS reporting services  to show the different reports but this time requirement is to show it in grid itself. If you have any detailed link on developing this please share it with me.

    Thanks in advance.

    Rekha.J

    Wednesday, August 20, 2014 6:42 AM
  • you can use javascript or plugin(based on the event) to calculate the fields and show in a field and show the field in view.

    ms crm

    Wednesday, August 20, 2014 1:15 PM
  • Hi Rekha,

    Try below code to get sum of selected records..

    function gridAmountSum()
    {
     if (document != null)
     {
      var gridBar = document.getElementById('crmGrid');
      var headers = gridBar.getElementsByTagName('TH');
      var totalAmount = 0.00;
      for (var i = 0; i < headers.length; i++)
      {
       if ( headers[i].innerText== "SumAmount") // Change column name as in grid ie., Est. Revenue.
       {
        var grid = document.getElementById('crmGrid').control;
        for (var intRowNumber = 0; intRowNumber < grid.GetRecordsFromInnerGrid().length; intRowNumber++)
        {     
         var number = grid.GetRecordsFromInnerGrid()[intRowNumber][3].cells[3].outerText; //cells[3] will be ok as sumamount in my grid is also in 3 position.

         totalAmount = (totalAmount + Number(number.replace(/[^0-9\.]+/g,"")));
        }
       }    
      }
     }
    }

    Thanks

    Suresh Sorde





    • Proposed as answer by Suresh Sorde Saturday, January 3, 2015 10:20 AM
    • Edited by Suresh Sorde Saturday, January 3, 2015 10:31 AM
    • Marked as answer by Rekha J Monday, January 5, 2015 6:15 AM
    Saturday, January 3, 2015 10:19 AM
  • This is the Solution

    Add This Code to Desing Page:-

    <GridView id="GrdView1" Runat Server/>

    <asp:TemplateField HeaderText="Date" ItemStyle-Width="100px">
                            <ItemTemplate>
                               <%# Eval("Date","{0:MMM d,yyyy}").ToString()%>
                            </ItemTemplate>
                            <FooterTemplate>
                                Sub-Total:
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Amount" ItemStyle-Width="118px">
                            <ItemTemplate>
                                <%# GetAmount(decimal.Parse(Eval("Amount").ToString())).ToString()%>
                            </ItemTemplate>
                            <FooterTemplate>
                                <%# GetToalAmount().ToString("")%>
                            </FooterTemplate>
                        </asp:TemplateField>

    </GrigView>

    In the cs file write the code on pageload as

    decimal TotalAmountPaid = 0;
        public decimal GetAmount(decimal Price)
        {
            TotalAmountPaid += Price;
            return Price;
        }
        public decimal GetToalAmount()
        {
            return TotalAmountPaid;
        }

    Then Run This..............................

    Saturday, January 3, 2015 11:41 AM
  • Hi Suresh,

    Thank you so much for the code. It works fine for all the records of the view, but on selecting few records also shows the same sum of all the records.

    Regards,

    Rekha.J

    Monday, January 5, 2015 6:21 AM
  • Hi Rekha,

    You need to update script with the help of below link.

    http://asimsajjad.blogspot.com/2012/01/how-to-get-selected-rows-of-grid-using.html

    Thanks



    Monday, January 5, 2015 9:09 AM