Using SugarBeans outside of Sugar's framework

We need to be able to run PHP code that makes use of Sugar data in scripts that are outside of Sugar - not scheduler tasks or logic hooks or anything. Completely outside of the Sugar world. Based on what I found here I added the following to my script:

if(!defined('sugarEntry')) define('sugarEntry', true);

require_once('data/BeanFactory.php');

That led to a string of error messages and including various other files, and then I started by including the same files in the base index.php from my Sugar installation, but finally I came to an impasse. My error message is:

 

Notice: Undefined index: log in /var/www/html/include/SugarObjects/VardefManager.php on line 1054 Fatal error: Call to a member function debug() on null in /var/www/html/include/SugarObjects/VardefManager.php on line 1054

The code at line 1054 of VardefManager.php is:

$GLOBALS['log']->debug("VardefManager::loadVardef called for module: $module");

I can't find where "log" is being defined in the GLOBALS scope, and anyway, this is starting to look like a lot more than I should have to do to make this work - my current code draft has six require_once statements in it, not to mention several other very hacky details. Is there a better way to do this? Am I missing something?

Sugar 7.8 Professional.

  • Hi Michael Jones,

    My first thought on this was to use the API framework. I see that is also the answer to the thread you linked above.

    Is there a reason you are avoiding doing this with the API?

  • I'm not sure what you mean. The solution in the thread, as far as I can tell, is to use SugarBeans - that's what I'm bending over backward to try to accomplish. If you mean why am I trying to do it outside of Sugar itself - we are a SAAS business, so basically we use Sugar to maintain our customers, and our customers use our portal to do a number of things which we would like to make record of in Sugar. I can of course do direct SQL inserts into the Sugar tables, but I would rather leverage the infrastructure that exists within Sugar to help us maintain data integrity. For example: when I want to insert a record in a table, I would rather let Sugar generate a GUID than generate one myself.

  • Hi Michael Jones,

    This is marked as the Correct Answer to the thread you linked: 

    https://community.sugarcrm.com/thread/26624#comment-77885 

    The comment links to this documentation list:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.9/index.html 

    The REST API is used by many third-party developers to create applications run completely outside of a Sugar instance that create or sync records with Sugar. Using the API, for example, an external application can tell Sugar to create a new Account record, provide desired field content, and it will make that record, generating the unique ID if one is not defined in the API call. The API can also establish relationships between existing records and can pull existing data out of Sugar as well.

    Within the Sugar application, developers can also create custom endpoints if the stock endpoints (found at <instance>/rest/v10/help) cannot achieve what they need.

    If this is completely off base from what you are looking for, I am happy to step back and welcome anyone else in the community who might have a more appropriate response for you. Best of luck!

  • I don't think the original thread is about using the REST API - it is about using SugarBeans directly. I'm not sure I understand why anyone would want to do an http REST call from a machine to itself, which is what I would be doing if I used the REST API, and I'm positive my boss would rather have me do direct table queries than incur http traffic. The code I need to write will reside on the same Web server as the Sugar instance - it just would not be part of it. In other words, it needs to initialize any infrastructure that I need to have available in order to use SugarBeans, independently of the Sugar interface and scheduler. For example:

    HOWTO: Using the bean instead of SQL all the time « Sugar Developer Blog – SugarCRM 

    It is entirely possible that I'm misunderstanding something; I'm not by any means a beginner where Web technologies are concerned, but I am absolutely a beginner when it comes to Sugar!

  • Michael Jones

    I've done what you are suggesting via entry points in Sugar version 6.x, there you are allowed to run scripts even without authentication - with the obvious security caveats.

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Application_Framework/Entry_Points/C… 

    At present my entry points still work in 7.9.x but I'm told these will eventually not be allowed anymore and we need to move everything to REST APIs (which I'm gradually doing), and this appears to confirm that:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.10/Architecture/Entry_Points/ 


    Perhaps, you could look at creating custom layouts for your custom pages:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.11/User_Interface/Layouts/Creating_Lay… 

    Also, the HOWTO: Using the bean instead of SQL all the time « Sugar Developer Blog – SugarCRM was a very useful document for version 6.x, but you should follow the BeanFactory documentation when working with sugar beans in version 7.x:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.11/Data_Framework/Models/SugarBean/ind… 

    I realize it's not exactly what you asked for but perhaps it will give you some ideas to move forward.

    FrancescaS

  • That actually is helpful, if only to confirm that I'm not missing something obvious. I had seen information about the endpoints and that they were being deprecated. If I understand correctly, including the code "if(!defined('sugarEntry')) define('sugarEntry', true);" basically is sidestepping the endpoint functionality anyway, with, as you said, security ramifications that need to be addressed. My code won't be running on our Sugar domain, but it will be on the same server with access to the same PHP code, so using REST calls seems excessive to me. Like using a pool cue to change the channel from across the room instead of pressing a button on the remote. It would incur network traffic for the machine to talk to itself, instead of just running code locally.

    Maybe our configuration is unusual, and that's why I'm struggling. I just want to use the SugarBean ORM functionality to make minor changes so I can leverage business logic baked in by the Sugar developers. It's looking like doing that may unfortunately be more trouble than it's worth, and I'm going to wind up having to do direct database inserts and hope I address all of the relevant details when I do so.

  • Hi Michael Jones

    Have you looked in to the CLI functionality?  If you create a CLI in "InstanceMode" then you should have access to BeanFactory and SugarBeans for that instance.

    The code would be within the Sugar file system but you could execute it from anywhere as long as you have access to the <sugar_instance>/bin/sugarcrm executable.

    Hope it helps.

  • Wow, that's interesting stuff! So in order to perform CRUD-style operations, you would need to add your own custom commands which would then be executed from command line. It kind of feels like more infrastructure than I was hoping to have to deal with, but I could see some situations where it would be perfect. For example, if I for some reason wanted to bypass Sugar's scheduler and run jobs directly from CRON, this would be ideal. I'll have to think through some use cases and see what I can come up with.

    Trying to avoid direct database inserts/deletes, but I don't want to abstract things in a way that is difficult to maintain after the fact.

  • I had a conversation with a colleague yesterday, and some time ago he worked up some PHP code that utilizes the REST interface! I don't know whether it's being used in production - I think it's mostly an admin-level tool, which would have different efficiency requirements than something that is running many times every day. I need to have another conversation with him and see if he ever looked into accessing the SugarBeans directly through PHP without involving another interface along the way.

  • Hi, you just need to include include/entryPoint.php file in your script and you can do whatever you want with sugar after that  Don't forget to set global $current_user - without it you would not be able to retrieve anything due to the team security (or you would have to turn it off on every retrieve...).

    this is how your script should start:

    <?php

    define('sugarEntry', true);
    require_once 'include/entryPoint.php';
    global $current_user;
    $current_user = BeanFactory::retrieveBean('Users',1);
    _ppl("sugar magic!!");
1 2