Let the platform do the work

BeanFactory

Overview

The BeanFactory class, located in ./data/BeanFactory.php, is used for loading an instance of a SugarBean. This class should be used any time you are creating or retrieving bean objects. It will automatically handle any classes required for the bean.

Creating a SugarBean Object

newBean()

To create a new, empty SugarBean, use the newBean() method. This method is typically used when creating a new record for a module or to call properties of the module's bean object.

  $bean = BeanFactory::newBean($module);

newBeanByName()

Used to fetch a bean by its beanList name.

  $bean = BeanFactory::newBeanByName($name);

Retrieving a SugarBean Object

getBean()

The getBean() method can be used to retrieve a specific record from the database. If a record id is not passed, a new bean object will be created.

  $bean = BeanFactory::getBean($module, $record_id);

Note: Disabling row-level security when accessing a bean should be set to true only when it is absolutely necessary to bypass security, for example when updating a Lead record from a custom Entry Point. An example of accessing the bean while bypassing row security is:

  $bean = BeanFactory::getBean($module, $record_id, array('disable_row_level_security' => true));

retrieveBean()

The retrieveBean() method can also be used to retrieve a specific record from the database. The difference between this method and getBean() is that null will be returned instead of an empty bean object if the retrieve fails.

  $bean = BeanFactory::retrieveBean($module, $record_id);

Note: Disabling row-level security when accessing a bean should be set to true only when it is absolutely necessary to bypass security, for example, when updating a Lead record from a custom Entry Point. An example of accessing the bean while bypassing row security is:

  $bean = BeanFactory::retrieveBean($module, $record_id, array('disable_row_level_security' => true));

Retrieving Module Keys

getObjectName()

The getObjectName() method will return the object name / dictionary key for a given module. This is normally the same as the bean name, but may not be for some modules such as Cases which has a key of 'aCase' and a name of 'Case'.

  $moduleKey = BeanFactory::getObjectName($moduleName);

getBeanName()

The getBeanName() method will retrieve the bean class name given a module name.

  $moduleClass = BeanFactory::getBeanName($module);