Sugar BeanFactory


Aug 07, 2017    Janaki Mahapatra, Sugar

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. Create Bean
$bean = BeanFactory::newBean($module);
$bean->name = 'Example Record'; //Populate bean fields
$bean->save(); //Save
$record_id = $bean->id; //Retrieve the bean id
Saving a Bean with a Specific ID: Saving a record with a specific id requires the id and new_with_id attribute of the bean to be set as follows:
$bean = BeanFactory::newBean($module); //Create bean
$bean->id = '38c90c70-7788-13a2-668d-513e2b8df5e1'; //Set the record id
$bean->new_with_id = true;
$bean->name = 'Example Record'; //Populate bean fields
$bean->save(); //Save
Setting new_with_id to true prevents the save method from creating a new id value and uses the assigned id attribute. If the id attribute is empty and the new_with_id attribute is set to true, the save will fail. Distinguishing New from Existing Records To identify whether or not a record is new or existing, you can check the fetched_rows property. If the $bean has a fetched_row, it was loaded from the database. An example is shown below:
if (!isset($bean->fetched_row['id']))
{
    //new record
}
else
{
}
Retrieving a Bean by Unique Field
require_once('include/SugarQuery/SugarQuery.php');
$sugarQuery = new SugarQuery();
$bean = BeanFactory::newBean(''); //fetch the bean of the module to query

$sql = new SugarQuery(); //create first query
$sql->select('id');
$sql->from($bean);
$sql->Where()->equals('', '');

$result = $sql->execute();
$count = count($result);

if ($count == 0) {
    //no results were found
}
elseif ($count == 1) {
    //one result was found
    $bean = BeanFactory::getBean('', $result[0]['id']);
}
else {
    //multiple results were found
}
Updating a Bean
$bean = BeanFactory::retrieveBean($module, $id);
$bean->name = 'Updated Name'; //Fields to update
$bean->save(); //Save
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));
Deleting a Bean Deleting a bean can be done by fetching it then calling the mark_deleted() method which makes sure any relationships with related records are removed as well:
//Retrieve bean
$bean = BeanFactory::retrieveBean($module, $id);
$bean->mark_deleted(); //Set deleted to true
$bean->save();
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));
 
Fetching Relationships This section explains how the SugarBean class can be used to fetch related information from the database. Fetching Related Records To fetch related records, load the relationship using the link:
//If relationship is loaded
if ($bean->load_relationship($link)) {
      $relatedBeans = $bean->$link->getBeans(); //Fetch related beans
}
An example of this is to load the contacts related to an account:
//Load Account
$bean = BeanFactory::getBean('Accounts', $id);
if ($bean->load_relationship('contacts')) {
     $relatedBeans = $bean->contacts->getBeans(); //Fetch related beans
}
Fetching a Parent Record Fetching a parent record is similar to fetching child records in that you will still need to load the relationship using the link:
//If relationship is loaded
if ($bean->load_relationship($link)) {
    $relatedBeans = $bean->$link->getBeans(); //Fetch related beans
    $parentBean = false;
    if (!empty($relatedBeans)) {
        reset($relatedBeans);  //order the results


        //first record in the list is the parent
        $parentBean = current($relatedBeans);
    }
}
An example of this is to load a contact and fetch its parent account:
//Load Contact
$bean = BeanFactory::getBean('Contacts', $id);

//If relationship is loaded
if ($bean->load_relationship('accounts'))
{
    //Fetch related beans
    $relatedBeans = $bean->accounts->getBeans();
    $parentBean = false;
    if (!empty($relatedBeans)) {
        //order the results
        reset($relatedBeans);

        //first record in the list is the parent
        $parentBean = current($relatedBeans);
    }
}