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
  • 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

Children