How to retrieve or get bean in custom end point

This is the code that I have but I kept getting null as the REST response.

but when I use raw SQL query I am able to get the results that I needed. However i'd prefer to use the Bean module instead of doing sql query.

What am I doing wrong?

<?php

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

class SdcImportApi extends SugarApi

{

  public function registerApiRest()

  {

  return array(

  'SdcEndPoint' => array(

  'reqType' => 'GET',

  'noLoginRequired' => true,

  'path' => array('SdcEndPoint','GetExample', '?'),

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

  'method' => 'MyGetMethod'),

  );

  }

  public function MyGetMethod($api, $args)

  {

  $contact = BeanFactory::retrieveBean('contacts','f3dc0892-d081-8b8e-dce8-571a703a3593',array('disable_row_level_security' => true));

  return $contact;

  //global $db;

  //$query ="select * from contacts c join contacts_cstm cc on cc.id_c = c.id where c.id = 'f3dc0892-d081-8b8e-dce8-571a703a3593'";

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

  //$row = $db->fetchByAssoc($result);

  //return $row;

  //return $args['contactId'];

  }

}

?>

Parents Reply Children
  • Hi Doddy,

    BeanFactory::retrieveBean('Contacts', 'f3dc0892-d081-8b8e-dce8-571a703a3593', array('disable_row_level_security' => true));

    or

    BeanFactory::getBean('Contacts', 'f3dc0892-d081-8b8e-dce8-571a703a3593', array('disable_row_level_security' => true));

    Should do the trick. If it does not, I would make sure that you are working with the correct module key. You can confirm this by verifying the records url https://instance.sugarondemand.com/#<module_key>/<id>

    I did want to mention that you should avoid returning the entire bean object in your api call as it will cause some php recursion issues. I would recommend doing as follows:

    $bean = BeanFactory::retrieveBean('Contacts', 'f3dc0892-d081-8b8e-dce8-571a703a3593', array('disable_row_level_security' => true));

    $api = new RestService();

    $fields = array('name');

    $api->user = $GLOBALS['current_user'];

    $helper = \ApiHelper::getHelper($api, $bean);

    return $helper->formatForApi($bean, $fields);

    Kind regards,

    Jerry Clark

    Sr. Manager, Developer Support & Support Applications

  • Hi Jerry,

    I finally able to make it work. All I did was to include the data/BeanFactory.php and set the requirelogin to true.

    I found out about the API formatting too when i dig up the code fore ModuleApi.php.

    so I followed that convention. thank you for bringing this up.

    Is it possible to create a custom end point in sugar but the parameter is an array? so instead of passing just the ID, I am passing more information to it?

  • Hi Doddy,

    Ah, I can't believe I didn't catch that! You shouldn't need to require ./data/BeanFactory.php. If you wanted to have a no-auth endpoint and still use most Sugar core functionality, you would have to set a user as the current user. An example would be:

    global $current_user;

    if (empty($current_user) || empty($current_user->id)) {

        $current_user = new User();

        $current_user->getSystemUser(); // or any other user bean

    }

    This is very risky and you should be aware that it could lead to a security breach depending on what the endpoint is doing.

    If you want to pass an array of information, I would suggest making the endpoint's request type "POST":

    'reqType' => 'POST',

    Then you can pass any data you like in the request body. An example is:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Integration/Web_Services/v10/Example…

    If you did this post to your custom endpoints url, you would see name and email1 in the $args parameter of your method.

    Hope this helps!

    Kind regards,

    Jerry Clark

    Sr. Manager, Developer Support & Support Applications

  • Fantastic this what I need!

    Thanks so much for your help!