Generating an ID with BeanFactory

Hello,

I am having trouble generating a new record with BeanFactory, i receive:

PHP Fatal error:  Uncaught Error: Class 'Sugarcrm\Sugarcrm\Util\Uuid' not found in /home/sugar8/public_html/include/utils.php:1428

my code looks like this

<?php

if (!defined('sugarEntry')) {
define('sugarEntry', true);
}
require_once '/home/sugar8/www/data/BeanFactory.php';
require_once '/home/sugar8/www/include/utils.php';


$testAccountBean = BeanFactory::newBean('Bugs');
$id = create_guid();

?>

Thanks,

  • Hi Web Team,

    Which location are you writing this file to?

    If you are writing this in sugar root directory or outside of sugar root directory, please include entryPoint.php file.

    require_once('include/entryPoint.php');

    Also, using BeanFactory, you do not need to create/specify ID explicitly until and unless you have any special requirement where you want to generate user defined IDs or wanted to pass ID from some other function. Below code is enough to generate a new Bug record in sugar.

    $bugBean = BeanFactory::newBean("Bugs");
    $bugBean->name = "Test Bug";
    $bugBean->save();

    Let us know if this helps.

    Regards.

  • Hi hat,

    My file was was in sugar_directory/process/testBean.php.

    I could not get it to work there every time a call was made it was redirecting me to the file install.php, so i just moved teastBean.php inside Sugar's root directory and it works now. I will go with this solution for now, but i am still interested why it was not working when it was nested a directory.

    Thanks,

  • I know I'm a bit late here, but if you really need a new uuid, Sugar does have some built in functionalities to help.

    Just above your class definition/just below your sugarEntry check, add 

    use Sugarcrm\Sugarcrm\Util\Uuid;

    And you will have access to the built in uuid functionalities.

    Then to create a Bean with a uuid that you can use the value of later on, do this:

    $bugBean = BeanFactory::newBean('Bugs');
    $bugBean->new_with_id = true;
    $bugBean->id = Uuid::uuid4();

    Also, I believe that the function 

    create_guid();

    has been deprecated some time ago.

  • BeanFactory interface provides a simple, yet flexible configuration mechanism to manage objects of any nature via the Spring IoC container. Let's have a look at some basics before diving deep into this central Spring API.