Typically when custom code is getting executed within the Sugar infrastructure, the BeanFactory class is autoloaded. Since the PHP error you are receiving is that the class was not found, you can manually require it in your custom code. The BeanFactory class is located in ./data/BeanFactory.php and can be included for use in your code by adding the require_once line at the top of your custom file: