Why am I getting HTTP: 500 Internal Server Error when I am trying to use after_retrieve?

I am trying to use logic hook 'after_retrieve', but I keep getting a 500 Internal Server Error when it is trying to load records into the subpanel.

It happens whether I have something called by after_retrieve or even just set it as an empty array.

I even tried 'after_fetch_query', but that never runs anything.

$hook_array{'after_retrieve'] = Array();
$hook_array['after_retrieve'] =  Array(11, "Check Whether to Highlight Title","custom/Resources/logic_hooks/Courses_Logic_Hooks.php", "Courses_Logic_Hooks", "setAlertField",);

  • Do you see any error into either apache or php error_log?

    Can you share content of your after_retrieve logic hook?

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • I don't see anything in the error log, sugarcrm or apache.  I now it has nothing to do with the code, because just putting the following in by itself makes it error.

    $hook_array{'after_retrieve'] = Array();

    Course Logic Hook

    <?php
    
    global $db, $sugar_config;
    
    class Courses_Logic_Hooks {
    
      //Set field: name
      public function Set_Course_Name(&$focus, $action, $args = array())
      {
        $focus->name = $focus->course_name_c;
      }
    
      //Set field: curriculum_type
      //No longer used, but kept for reference. Now replaced via /custom/extension/lev_courses/vardefs/sugarfield_curriculum_type.php
      public function Set_Curriculum_Type(&$focus, $action, $args = array())
      {
          require_once('modules/Lev_Course_Catalog/Lev_Course_Catalog.php');
          $course_catalog = new Lev_Course_Catalog();
          $ctype =$course_catalog->retrieve($focus->lev_course_catalog_id_c);
          $focus->curriculum_type =  preg_replace('/[_]/', ' ', $ctype->curriculum_type);
    
      }
    
      //Set field: start_date and start_day_of_week
      public function Set_Start_Date(&$focus, $action, $args = array())
      {
          $sql = <<<SQL
    SELECT coursedate.class_date AS first_class_date FROM lev_course_class_dates coursedate
          LEFT JOIN lev_courses_lev_course_class_dates_c connect ON coursedate.id=connect.lev_courses_lev_course_class_dateslev_course_class_dates_idb AND coursedate.deleted=0
          LEFT JOIN lev_courses course on connect.lev_courses_lev_course_class_dateslev_courses_ida=course.id and connect.deleted=0
          WHERE course.deleted=0 AND course.id='{$focus->id}' ORDER BY coursedate.class_date ASC LIMIT 1
    SQL;
    
          $result = $GLOBALS['db']->query($sql);
          while ($row = $GLOBALS['db']->fetchByAssoc($result))
          {
            $focus->start_date = $row['first_class_date'];
            $focus->start_day_of_week = date('l', strtotime($row['first_class_date']));
          }
      }
    
      //Set field: end_date and end_day_of_week
      public function Set_End_Date(&$focus, $action, $args = array())
      {
          $sql = <<<SQL
    SELECT coursedate.class_date AS last_class_date FROM lev_course_class_dates coursedate
          LEFT JOIN lev_courses_lev_course_class_dates_c connect ON coursedate.id=connect.lev_courses_lev_course_class_dateslev_course_class_dates_idb AND coursedate.deleted=0
          LEFT JOIN lev_courses course on connect.lev_courses_lev_course_class_dateslev_courses_ida=course.id and connect.deleted=0
          WHERE course.deleted=0 AND course.id='{$focus->id}' ORDER BY coursedate.class_date DESC LIMIT 1
    SQL;
    
          $result = $GLOBALS['db']->query($sql);
          while ($row = $GLOBALS['db']->fetchByAssoc($result))
          {
            $focus->end_date = $row['last_class_date'];
            $focus->end_day_of_week = date('l', strtotime($row['last_class_date']));
          }
      }
    
      //Set field: academic_period
      public function Set_Academic_Period(&$focus, $action, $args = array())
      {
          if (!empty($focus->start_date))
          {
            if (date('Y', strtotime($focus->start_date)) < '2013')
            {
              if (date('n', strtotime($focus->start_date)) < 5)
              {
                $affix = '21';
              }
              else if (date('n', strtotime($focus->start_date)) < 9)
              {
                $affix = '25';
              }
              else
              {
                $affix = '29';
              }
            }
            else
            {
              $affix = '00';
            }
            $focus->academic_period = date('Y', strtotime($focus->start_date)).$affix;
          }
      }
    
      //Set field: total_client_fee
      public function Set_Total_Fee(&$focus, $action, $args = array())
      {
          if ($focus->all_inclusive_fee == 1)
          {
            $focus->total_client_fee = $focus->targeted_fee;
          }
          else
          {
            //Get sum of "Total to Client" field from all linked Course Costs
            $sql = <<<SQL
    SELECT sum(cost_c.total_to_client_c) AS total_to_client 
    FROM lev_course_costs cost
    LEFT JOIN lev_course_costs_cstm cost_c
        ON cost.id = cost_c.id_c and cost.deleted =0
    LEFT JOIN lev_courses_lev_course_costs_c connect 
        ON cost.id=connect.lev_courses_lev_course_costslev_course_costs_idb 
        AND cost.deleted=0
     LEFT JOIN lev_courses course 
        ON connect.lev_courses_lev_course_costslev_courses_ida=course.id 
        AND connect.deleted=0
    WHERE course.deleted=0 AND course.id='{$focus->id}'
    SQL;
    
            $result = $GLOBALS['db']->query($sql);
            while ($row = $GLOBALS['db']->fetchByAssoc($result))
            {
              $total_to_client = $row['total_to_client'];
            }
    
            $focus->total_client_fee = $focus->targeted_fee + $total_to_client;
          }
      }
    
      //Set field: total_expense
      public function Set_Total_Course_Expense(&$focus, $action, $args = array())
      {
          //Get sum of "Total to CBT" field from all linked Course Costs
          $sql = <<<SQL
    SELECT sum(cost_c.total_expense_c) AS total_cbt 
    FROM lev_course_costs cost
    LEFT JOIN lev_course_costs_cstm cost_c
        ON cost.id = cost_c.id_c and cost.deleted = 0
    LEFT JOIN lev_courses_lev_course_costs_c connect 
        ON cost.id=connect.lev_courses_lev_course_costslev_course_costs_idb 
        AND cost.deleted=0
    LEFT JOIN lev_courses course 
        ON connect.lev_courses_lev_course_costslev_courses_ida=course.id 
        AND connect.deleted=0
    WHERE course.deleted=0 AND course.id='{$focus->id}'
    SQL;
    
          $result = $GLOBALS['db']->query($sql);
          while ($row = $GLOBALS['db']->fetchByAssoc($result))
          {
            $total_cbt = $row['total_cbt'];
          }
    
          $focus->total_expense = ((
          (!empty($focus->contact_hours) ? $focus->contact_hours : 0) 
          + 
          (!empty($focus->prep_hours) ? $focus->prep_hours : 0)
          ) * 
          (!empty($focus->hourly_fee_to_instructor) ? $focus->hourly_fee_to_instructor : 0) 
          ) + (
          (!empty($focus->lunch_vouchers) ? $focus->lunch_vouchers : 0) 
          * 
          (!empty($focus->lunch_voucher_amt) ? $focus->lunch_voucher_amt : 0)
          ) + 
          (!empty($total_cbt) ? $total_cbt : 0);
      }
    
      //Set field: course_net
      public function Set_Course_Net(&$focus, $action, $args = array())
      {
          $focus->course_net = $focus->total_client_fee - $focus->total_expense;
      }
    
    
      //Set field: gross_margin
      public function Set_Gross_Margin_Percent(&$focus, $action, $args = array())
      {
          if($focus->total_client_fee == '' || $focus->total_client_fee == 0){
              $focus->gross_margin = 0;
          } else {
              $focus->gross_margin = $focus->course_net / $focus->total_client_fee * 100;
          }
      }
    
      //Set a request variable for contract id (lev_contracts_lev_courseslev_contracts_ida)
      public function Set_Request_ContractID(&$focus, $action, $args = array())
      {
          //Get sum of "Total to CBT" field from all linked Course Costs
          $sql = <<<SQL
    SELECT  account_id_c, accounts.name as company_name FROM lev_contracts
    INNER JOIN accounts on accounts.id = lev_contracts.account_id_c
          WHERE lev_contracts.deleted=0 AND lev_contracts.id='{$focus->lev_contracts_lev_courseslev_contracts_ida}'
    SQL;
          $result = $GLOBALS['db']->query($sql);
          while ($row = $GLOBALS['db']->fetchByAssoc($result))
          {
            $_POST['lev_contract_name'] = $row['company_name'];
          }
      }
    
    
    
              function setAlertField($bean, $event, $arguments)
            {
                $sql = <<<SQL
                SELECT contract.contract_status AS status FROM lev_contracts contract
                      LEFT JOIN lev_contracts_lev_courses_c connect ON contract.id=connect.lev_contracts_lev_courseslev_contracts_ida AND contract.deleted=0 AND (contract.contract_status = 'Proposal_Active' OR contract.contract_status = 'Proposal_Rejected')
                      LEFT JOIN lev_courses course on connect.lev_contracts_lev_courseslev_courses_idb=course.id and connect.deleted=0
                      WHERE course.deleted=0 AND course.id='{$bean->id}'
    SQL;
                      $result = $GLOBALS['db']->query($sql);
                      $bean->alertfield_c = '';
                      while ($row = $GLOBALS['db']->fetchByAssoc($result))
                      {
                        $bean->alertfield_c = '<pre style="border:2px solid #c00; padding: 5px;margin: 5px; color:#0000CC; text-align: center; width: 100%; text-decoration: bold;"> COURSE HAS BEEN BILLED - PLEASE BE CAUTIOUS WHEN MAKING CHANGES</pre>';
                      }
            }
    
    
      //check for completed courses and change CSS
      public function Completed_Course($bean, $action, $args = array()){
        //fire on detail view and edit view
        if (($_REQUEST['action'] == 'DetailView' || $_REQUEST['action'] == 'EditView' && $_REQUEST['module'] == 'Lev_Courses') && $_POST['lev_course_completed'] == 'yes')
        {
            echo '<script src="custom/Resources/javascript/Lev_Courses/completedCourse.js" type="text/javascript"></script>';
        }
    
    
       }
    
      //save parent contract to finish the calculations and auto populate fields
        public function saveParentCourse($bean, $event, $args){
            //check if any total field is updated
            if(($bean->total_client_fee !=$bean->fetched_row['total_client_fee'] ) || ($bean->total_expense !=$bean->fetched_row['total_expense'] ) ){
                $parent_contract = $bean->get_linked_beans('lev_contracts_lev_courses','Lev_Contracts');
    
                if($parent_contract[0]->id !=''){
    
                        require_once('modules/Lev_Contracts/Lev_Contracts.php');
                        $lev_contract = new Lev_Contracts();
                        $lev_contract->retrieve($parent_contract[0]->id);
                        $lev_contract->save();
                        //$parent_contract = $lev_course->get_linked_beans('lev_courses_lev_course_costs','Lev_Course_Costs');
    
                }
    
            }
    
        }
    
      //calculate all totals and date after related bean is deleted
      public function update_Course_Data($bean, $event, $args){
          //save bean to calauclate all auto populated fields
          $bean->save();
      }
    
    
    
        function Check_Hourly_Fee(&$bean, $event, $arguments)
        {
    
            $result = true;
            if (!empty($bean->contact_id_c))
            {
                $getInstructor = new Contact();
                $getInstructor->retrieve($bean->contact_id_c);
    
    	    if (!empty($getInstructor->instructor_pay_rate_max_c))  // Added this check because it was returning false when there was no max pay rate set - 9/5/2012 Adam Canova
    	    {
                    if ($bean->hourly_fee_to_instructor > $getInstructor->instructor_pay_rate_max_c)
                    {
                        $result = false;
                    }
    	    }
            }
    
    
            if($result) {
                return true; //let save proceed as normal
            }
    
            $_SESSION['myError'] = 'Hourly Fee to Instructor must not exceed the Instructor\'s Maximum Pay Rate';
            //build the URL to return to
            $module = $_REQUEST['module'];
            $action = "&action=EditView"; //we need to have EditView b/c that is the action we are taking
            $returnModule = "&return_module=" . $_REQUEST['return_module'];
    
            $offset = $_REQUEST['offset'];
            if ($offset == "") {
            } else {
                $offset = "&offset=$offset";
            }
    
            $stamp = $_REQUEST['stamp'];
            if ($stamp == "") {
            } else {
                $stamp = "&stamp=$stamp";
            }
    
            if ($recordId == "") {
                $returnAction = "&return_action=DetailView";
            } else {
                $recordId = "&record=" . $recordId;
            }
    
            $url = "index.php?module=" . $module . $offset . $stamp . $action . $returnModule . $returnAction . $recordId;
            header("Location: $url");
            $_SESSION['Lev_CoursesBean'] = $bean; //store the bean in session so we can retrieve the values later
            exit; //goto the location contained in header
        }
    
        function Check_Course_Status(&$bean, $event, $arguments)
        {
    
        }
    }
    
    ?>
    

  • 500 errors are almost always syntax errors, you have an error on line 1, you are opening a curly bracket instead of a square bracket :)

    $hook_array{'after_retrieve'] = Array();

    should be 

    $hook_array['after_retrieve'] = Array();

    FrancescaS