Showing the total amount for a column in a listview, using just logic hooks and jQuery in Sugar 6.5

sugarcrmdevelopers —  November 6, 2012 — 27 Comments

I came across a blog post the referenced another forums post on how to display a sum total for the currency values displayed in a listview result. This is a pretty common use case I would imagine, as often times you would want to do these summations to avoid pulling out the calculator if you are doing forecasting by hand for example with the opportunities module. Nonetheless, reading the customization details on how to do this seemed quite involved ( and rather upgrade-unsafe ), so I thought I would dig in for a simpler way to do this.

The crux of what was needed is this:

  • A simple way to sum up all the values in the column(s) of our choosing ( in this case Opportunities )
  • A way to interject this display into the template

We have two logic hooks which are quite adept at doing this. First is process_record, which is a hook called for each bean record listed on a ListView. The second is after_ui_frame, which is called after the main content display is done, as is a nice way to add in additional content to a view.

Let’s smack these together in a single logic hook class, as shown below.

How this works, is for each record processed, we increment a static counter in the class ( doing this gets around the case where the Logic Hooks functionality creates new instances of the class ). Then at the end with the after_ui_frame hook, use jQuery to stick the total count exactly where we want it in the display ( between the module title and the search form itself ).

The best part? Completely upgrade-safe and doesn’t touch anything with core files. Worst case, you just need to adjust the jQuery positioning code to put things where you like on the form.

27 responses to Showing the total amount for a column in a listview, using just logic hooks and jQuery in Sugar 6.5


    Sounds like a great addition!
    I’m new to sugarcrm. How would you install this?



      First of all, copy this file as, for example, sumopp.php in the directory custom/modules/Opportunities.

      Then, in the same directory find the file logic_hooks.php if you have one..

      Refer to manual, on how to write logic hooks, but basically you declare in this file arrays indicating what and when you want to trigger the proccess:

      (the first for the sum of each row function inside the class)
      $hook_array[‘process_record’][] = Array(1, ‘Sum_Each’, ‘custom/modules/Opportunities/sumopp.php’,’SumOpportunitiesHook’, ‘sumTotalFromEachRow’);

      (this is for the display function inside the class)
      $hook_array[‘after_ui_frame’][] = Array(1, ‘Display_Sum’, ‘custom/modules/Opportunities/sumopp.php’,’SumOpportunitiesHook’, ‘displaySumTotal’);

      By the way, I’ve implemented it and works like a charm: Thank you John!

      Hope it helps..


    I’m having problems implementing the logic hook. Did you just paste the following code to “logic_hook.php” in custom/modules/Opportunities?


    So you have two files. logic_hook.php and sumopp.php?


      Didn’t see the code; could you link to pastebin or gist instead?


        Actually it’s just the code from Daniel.

        $hook_array[‘process_record’][] = Array(1, ‘Sum_Each’, ‘custom/modules/Opportunities/sumopp.php’,’SumOpportunitiesHook’, ‘sumTotalFromEachRow’);

        $hook_array[‘after_ui_frame’][] = Array(1, ‘Display_Sum’, ‘custom/modules/Opportunities/sumopp.php’,’SumOpportunitiesHook’, ‘displaySumTotal’);

        Sorry for double posting…


    Actually it’s just the code from Daniel.

    $hook_array[‘process_record’][] = Array(1, ‘Sum_Each’, ‘custom/modules/Opportunities/sumopp.php’,’SumOpportunitiesHook’, ‘sumTotalFromEachRow’);

    $hook_array[‘after_ui_frame’][] = Array(1, ‘Display_Sum’, ‘custom/modules/Opportunities/sumopp.php’,’SumOpportunitiesHook’, ‘displaySumTotal’);


    Check out the updated version of the logic_hook.php file I did here…

    Also, rename in logic_hooks.php.


      Sorry, for the late response. Thank you John, now it’s working fine!

      Now, I’m going to try getting in the lower column “header”. Hope to get this fixed. Otherwise I’m going to ask here.

      Thanks again!


    Hi John, great article, thanks. Could you briefly explain how this could be used in a dashlet? e.g. Let’s say you have a custom module called Orders and you want to display a total of this month’s orders both in the listview and in a dashlet. Could you poinst me in the right direction?


    This is really cool solution, thanks! The only issue I have is I’m able to access only the data that is displayed on the list. I would need to access other data of each record too, to be able to do some conditions. Is this somehow possible? Thanks!


    its didnt show the total amount
    this is the complete code? or i must edit the code of the .tpl?


      Are you using Sugar 6.5? Make sure you use Quick Repair and Rebuild. There are larger examples that include custom templates within the linked blogs that you could also try.


    Hi, many thanks for this brillant post. Is there a way to get the display the sum of the whole data, not the sum of only of the displayed data rows? = I mean the total as if there were no pagination at all?

    Many thanks,


      i think you can use this

      * Called as a after_ui_frame logic hook on the Opportunities module
      public function displaySumTotal($event, $arguments)
      if ($GLOBALS[‘action’] == ‘index’ || $GLOBALS[‘action’] == ‘ListView’) {
      global $db;
      $opportunity = new Opportunities();
      $opportunites = $opportunity->get_full_list();

      foreach($opportunites as $opp){
      self::$sum += $opp->amount_usdollar;

      $sum = currency_format_number(self::$sum);

      i hope its help


        Is there a way to do this for just details view in Opportunities? I would like to gather a total of a currency field and sum all rows returned, then display that in details.


    how i can create the dynamic calculation when i use the search field in list view and not just calculate the show data (20 data) in the list view but it calculate all of the searched data

Trackbacks and Pingbacks:

  1. bugs module - list view - December 12, 2012

    […] […]

  2. SugarCRM Developer Blog » Blog Archive » Using the after_ui_frame logic hook to add a custom subpanel - February 6, 2013

    […] had a post here a while back on the interesting use of the after_ui_frame logic hook, as it’s the only logic hook designed […]

  3. Showing Total Amount for a column in listview | Nerdy Goodness - Mark Morgan's Web Development Blog - php, jquery, css - March 14, 2013

    […]… Like this:Like Loading… This entry was posted in SugarCRM. Bookmark the permalink. […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s