Why doesn't BeanFactory("module name", id) return the record from the db?

Hello World, 

So I have an api call that is working.  That is I can put in the url and some argument and print out that argument. I want to get some data using beans,  I know that I am clearly doing something wrong because nothing is return what I expect it to.  For example I have this code:

$accountID = '1d53d7c0-5783-9a99-09a6-555e943c52cb';

$accountBean = BeanFactory::getBean("Accounts",$accountID);

//Shouldn't this return the record the db?

return $accountBean;

According to the documentation on the developer site when you add the ID of the bean you want the bean factory should return the record from the db. Correct?

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.5/API/Classes/BeanFactory/#Retrieving…

 

This is the value from the db:

So shouldn't I expect to see a bean representing that record with that data that have a name property of Internal testing Team?

When I test the code using postman I get this instead:

As you can see I can search for the name value but nothing is there. It appears to just be the definition of the object that I returned and not the record from the database.

Why is this?  Can someone please shed light on my problem.

Best Regards

  James Palmisano

Parents Reply Children
  • I am using my own user credentials to create an OAuth token

    Then I use that token to call the url in postman

    Angel Magana

  • The client_id is defined as such

    It appears that I am a sugar user when I use this client_id

    Is this the problem?

  • Why are you using the platform value of "custom" versus "base" or "api?"

  • I don't remember to be honest.  Would that make every response from the db return as null?

  • just created a token with base and then another with api and both of them produced the same result.  So my code still will just return null.  We are getting closer....

    Thank you for all the responses today!

  • Can you post the code for your endpoint? Also, have you tried the getBean() method outside the context of a custome endpoint, for example, in a logic hook?

  • Someone else here discovered that the api doesn't come with a db instance initiated so we had to define our own.  Here is the code that I was playing with before we figured that out. 

    <?php

    /**

    * Created by PhpStorm.

    * User: James

    * Date: 7/8/16

    * Time: 1:18 PM

    */

     

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

     

    class HubbleBubbleAPICall extends SugarApi

    {

       public function registerApiRest()

       {

           return array(

               //GET

               'HubbleBubbleAPICall' => array(

                   //request type

                   'reqType' => 'GET',

     

                   //set authentication

                   'noLoginRequired' => true,

     

                   //endpoint path

                   'path' => array('HubbleBubble', '?'),

     

                   //endpoint variables

                   'pathVars' => array('', 'data'),

     

                   //method to call

                   'method' => 'getHubbleBubbleData',

     

                   //short help string to be displayed in the help documentation

                   'shortHelp' => 'HubbleBubble/CustomerID_Num',

     

                   //long help to be displayed in the help documentation

                   //TODO:need to make a help doc page for this endpoint

                   'longHelp' => 'custom/clients/base/api/help/HubbleBubbleAPICall.html',

               ),

           );

       }



       /**

        * @param $api

        * @param $args

        *   customer ID from the in_customers table

        *  it will look like this : customer_someInt

        * @return mixed

        */

       public function getHubbleBubbleData($api, $args)

       {

     

           //$in_customer_bean = BeanFactory::getBean("IN_Customer",$args);

           //$in_customer_bean->id = $args;

           //$in_customer_bean->fetch();

    /*

           $sugarQuery = new SugarQuery();

     

           //add fields to select

           $sugarQuery->select(array('install_date'));

     

           //fetch the bean of the module to query

           $bean = BeanFactory::newBean('IN_Locations');

     

           //add from table of the module we are querying

           $sugarQuery->from($bean, array('team_security' => false));

     

           //add join

           $sugarQuery->join('in_customers_in_locations');

     

           //add where

           $sugarQuery->where()->equals('in_customers.id',$args);

     

           $result = $sugarQuery->execute();

    */

           //initialize db instance

           //global $db;



           //$query = "select min(in_l.install_date) as minInstallDate from in_locations in_l

    //join in_customers_in_locations_c relTable on relTable.in_customers_in_locationsin_locations_idb = in_l.id

    //join in_customers on relTable.in_customers_in_locationsin_customers_ida = in_customers.id

    //where in_l.install_date is not null and in_customers.id ='customers_117'; ";

     

    //        $result = $db->query($query);

    //        if (!$result) {

    //            die('Invalid query: ' . mysql_error());

    //        }

     

           $accountID = '1d53d7c0-5783-9a99-09a6-555e943c52cb';

           $accountBean = BeanFactory::retrieveBean("Accounts",$accountID);

    //Shouldn't this return the record the db?

           if(empty($accountBean)){

               return "The account bean is empty";

           }else{

               return "The account bean is not empty";

           }

     

           //$result = $db->query($sqlStartOfBusiness);

           //$returnedData["beginningDate"] = $db->fetchByAssoc($result);

     

          // return $returnedData;

           //return $accountBean;

       }

     

    }

     

    ?>

    As you can see I was playing with multiple ways to read data, but without a db instance none of them would have worked.

    Thanks for your comments!  Please let us know if we should know anything else in our api calls.

  • So this code ended up working:

    //create db instance

            $db = DBManagerFactory::getInstance();



            $query = "select min(in_l.install_date) install_date from in_locations in_l  ";

            $query .= " join in_customers_in_locations_c relTable on ";

            $query .= " relTable.in_customers_in_locationsin_locations_idb = in_l.id ";

            $query .= " join in_customers on relTable.in_customers_in_locationsin_customers_ida ";

            $query .= " =  in_customers.id where in_customers.id ='customers_117' ";

            $query .= " and in_l.install_date is not null ";

     

            $return_value = 'No exception';

            try {

                $result = $GLOBALS['db']->query($query);

                $result = $db->getOne($query);

                if ($result != null) {

                    $return_value = $result;

                } else {

                    $return_value = 'Result is null';

                }

     

            } catch (Exception $e) {

                $return_value = "Exception raised: " . $e->getMessage();

            }

     

            return $return_value;

  • Looking at the commented code it looks like you are not extracting your parameters from the api call.

    I also don't understand why you would reassign the id.

           //$in_customer_bean = BeanFactory::getBean("IN_Customer",$args);

           //$in_customer_bean->id = $args;

           //$in_customer_bean->fetch();

    When you call the API the resulting $args is an array.

    $args['data'] would contain the information you passed.

    So, assuming that your call looks something like

    HubbleBubble/1d53d7c0-5783-9a12345-555e943c52cb

    your customer id is not in $args but in $args['data'] so the retrieval of the record from the IN_Customer module should be :

    $in_customer_bean = BeanFactory:retrieveBean("IN_Customer", $args['data']);

    Note that the format is BeanFactory:retrieveBean(<module name>, <id>)

    I suspect from the name of your table that the module name is IN_Customers, plural, not IN_Customer. (in version 6.x we used to have to use the Class than which was often singular, with BeanFactory we use the Module name, not the Class name). So maybe the correct call is:

    BeanFactory:retrieveBean("IN_Customers", $args['data']);

    You don't need a fetch statement, once you have retrieved the bean the $in_customer_bean now contains all the data from that bean so, for example, the id will be: $in_customer_bean->id

    I find it useful, when debugging code, to include fatal logging just to see what the variables look like and make sure they are set correctly. Once you are happy with the results you can change your fatal to debug or remove the logging altogether.

    Additionally check your PHP logs when working with APIs, you may find more information than you expect especially if there are errors with the way methods are called.

    HTH

    FrancescaS