How to query db and sum the rows returned

Hello,

I have a need to query the DB for all products sold to a client then get the total. This then needs to be displayed in a field.

I am using SugarCRM CE 6.5.13

I would greatly appreciate any assistance.

Thanks.

Parents Reply
  • The query does run however no results are displayed. I used the diagnostic tool and obtained a complete list of tables and related tables. I think I need to load the bean with all related tables. In order to get this to work.

    I believe I saw something like this $bean->load_relationship(module name) or something. Then I should be able to run the query. What are your thoughts?

    -edit-

    I attempted the $bean->load_relationship(module name) and this did not work. The query just spit out a "1" Clearly this is not the sum of any of the product prices that it would encounter.

    I am still in need of assistance. Perhaps with loading the bean with all related data so that I could access the correct fields then apply the script.

    Does anyone know how to do this. I was looking and found HOWTO: Using the bean instead of SQL all the time. « Sugar Developer Blog – SugarCRM  however I was unsuccessful with that approach.

Children
  • Okay I figured it out!!!!

    <?php

    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    require_once('include/MVC/View/views/view.detail.php');

    class OpportunitiesViewDetail extends ViewDetail {

      function OpportunitiesViewDetail(){

      parent::ViewDetail();

      }

      function display() {

    $account = new Opportunity();                               //$var = new ModuleName() in singular form

    $account->retrieve($_REQUEST['record']);           //This grabs the record

    $contacts = $account->get_linked_beans('opportunities_op_ps_product_sales_1','Contact');         

                                                                                 // ^this uses the get_linked_beans(linked var name(found in the vardefs) , name of the                                                                                 //  ^object you are creating. The name can be anything you like.

                                                                               // then just loop through the created associations to get fields.

    foreach ( $contacts as $contact ) {

        $total += $contact->sales_price_c;         //add the value of each sale to the variable

    }

              //then populate the field with the value in the $total var

    echo "

           <script>

        var total = '$total';

           $(document).ready(function(){

        $('#total_sales_c').after(total); });

           </script>";

        

      parent::display();

      }

    }

    ?>

    Thank you for all your help this was a great learning experience. Hopefully this will help others.

  • Hello Dan A,

         I am very glad that you found the solution yourself. Keep going.