Let the platform do the work

before_save

Overview

The before_save logic hook executes before a record is saved.

Definition

  function before_save($bean, $event, $arguments){}

Arguments

Name Type Description
bean Object The bean object
event String The current event
arguments Array Additional information related to the event
arguments.check_notify Boolean Whether or not to send notifications
arguments.isUpdate Boolean

Whether or not the record is newly created

  • true = this is an update to an existing record
  • false = a newly created record

Considerations

  • For modules that contain a user-friendly record ID (e.g. the case_number field for the Cases module), the value of that field is not available for a before_save call. This is because this business logic has yet to be executed.
  • Calling save on the bean in this hook will cause an infinite loop if not handled correctly. (i.e: $bean->save())

Examples

Creating a Logic Hook using Extension Framework

./custom/Extension/modules/<module>/Ext/LogicHooks/<file>.php

  <?php

    $hook_array['before_save'][] = Array(
        //Processing index. For sorting the array.
        1,

        //Label. A string value to identify the hook.
        'before_save example',

        //The PHP file where your class is located.
        'custom/modules/<module>/before_save_class.php',

        //The class the method is in.
        'before_save_class',

        //The method to call.
        'before_save_method'
    );

?>

./custom/modules/<module>/before_save_class.php

  <?php

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

    class before_save_class
    {
        function before_save_method($bean, $event, $arguments)
        {
            //logic
        }
    }

?>

Creating a Core Logic Hook

Prior to Sugar 6.3.x, logic hooks could only be created using the following method. Please note that this approach is still valid but is not recommended when building plugins as it may conflict with existing customizations.

./custom/modules/<module>/logic_hooks.php

<?php

   $hook_version = 1;
   $hook_array = Array();

   $hook_array['before_save'] = Array();
   $hook_array['before_save'][] = Array(
      //Processing index. For sorting the array.
      1, 

      //Label. A string value to identify the hook.
      'before_save example', 

      //The PHP file where your class is located.
      'custom/modules/<module>/before_save_class.php', 

      //The class the method is in.
      'before_save_class', 

      //The method to call.
      'before_save_method' 
   );

?>

./custom/modules/<module>/before_save_class.php

<?php

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

   class before_save_class
   {
      function before_save_method($bean, $event, $arguments)
      {
         //logic
      }
   }

?>