SugarCRM 7.7.0.0 recordList for modules API

I am calling this API http://{site_url}/SugarPro/rest/v10/Accounts

I need to sort accounts in ascending order with respect to their distance from current logged in user (I will calculate distance using address of logged in user and specific account). I tried to use {before_respond} API logic hook to calculate distances and sort the response. But it looks a bad practice to use this hook as it will be fired for every API request.

The other way around is to extend the API that is being used for this request. But I am unable to find the file that is being used in this request. Can someone please guide me to find which Api file is being used in this request?

Or any other better way to get the desired result.

Note: I want this behavior only for mobile view of SugarCRM and SugarCRM Mobile app

Thanks

  • I believe the API you are looking for is 

    clients/base/api/ModuleApi.php

    in particular the GET

     

    You probably don't want to extend that because it's used by all modules but rather create your own custom API for Accounts only where you can manipulate your data retrieval and more to get the result set you need.

     

    See:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/API/Web_Services/Extending_Web_Serv…

     

    HTH

    FrancescaS

  • Hi Francesca,

    Thanks for the time to respond.

    Actually the API is clients/base/api/FilterApi.php with request type GET.

    This API is also used by all modules to retrieve a list of records (either filtered or all).

    I have created a custom API which is extending to this API in directory custom/clients/base/api

    Here is my code in case some one have similar problem.

    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');  
    
    
    require_once("clients/base/api/FilterApi.php");
    
    
    class CustomFilterApi extends FilterApi  
    {  
       public function registerApiRest() { 
        return array(
        'filterModuleAll' => array(
             'reqType' => 'GET',
             'path' => array('<module>'),
             'pathVars' => array('module'),
             'method' => 'filterList',
             'jsonParams' => array('filter'),
             'shortHelp' => 'List of all records in this module',
             'longHelp' => 'include/api/help/module_filter_get_help.html',
             'exceptions' => array(
                 // Thrown in filterList
                 'SugarApiExceptionInvalidParameter',
                 // Thrown in filterListSetup and parseArguments
                 'SugarApiExceptionNotAuthorized',
             ),
             'extraScore' => 2, // This parameter will give this API prescedence over the original API 
         ),
      );  
       }  
       //override to modify this function of the FilterApi endpoint  
       public function filterList(ServiceBase $api, array $args, $acl = 'list')
        {
            if (!empty($args['q'])) {
                if (!empty($args['filter'])||!empty($args['deleted'])) {
                    // These flags can be used with the filter API, but not with the search API
                    throw new SugarApiExceptionInvalidParameter();
                }
                // We need to use unified search for this for compatibilty with Nomad
                require_once('clients/base/api/UnifiedSearchApi.php');
                $search = new UnifiedSearchApi();
                $args['module_list'] = $args['module'];
                return $search->globalSearch($api, $args);
            }
            
            $api->action = 'list';
            list($args, $q, $options, $seed) = $this->filterListSetup($api, $args, $acl);
    
            $result = $this->runQuery($api, $args, $q, $options, $seed);
            if ($args['module'] == "Accounts") {
                 // you can manipulate response for your specific module
            }
            return $result;
        }
    }