How retrieve record by custom code in record.js?

How use App.api.call in record.js to retrive record by custom field?

  • Hello Rodolfo,

    You need to create your custom end-point API for that please check below support link.

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/API/Web_Services/Extending_Web_Servi…

    Below is the API call you need to call through record.js file.

    var param = {
                id: "123456789"
            };
            var url = app.api.buildURL("<module_name>", "function_name", {}, param);
            app.api.call('GET', url, {}, {
                success: function (data) {
                    if (data == "Success")
                    {
                        //success
                    }    
                },
            });
    

    Hope it will help.

    -BPATEL

  • Thanks for your answer. I am still confused how to do this .

    What value function_name ?

     

    I can not use the functions of v10 REST API?

     

    Do you have an example of how to check duplicate value in a custom field ?

     

    Thank you

  • I'm create this endPpoint.

    custom/modules/Contacts/clients/base/api/CheckContaDuplicadaApi.php

    <?php
    
    
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    
    class CheckContaDuplicadaApi extends SugarApi
    {
        public function registerApiRest()
        {
            return array(
                //GET
                'CheckContaDuplicadaApi' => array(
                    //request type
                    'reqType' => 'GET',                
                    //set authentication
                    'noLoginRequired' => true,                
                    //endpoint path
                    'path' => array('CheckContaDuplicadaApi'),
                    //endpoint variables
                    //'pathVars' => array('', '', 'data'),
                    //method to call
                    'method' => 'checkConta',
                    //short help string to be displayed in the help documentation
                    'shortHelp' => 'Verifica conta corretora duplicada em contatos',
                    //long help to be displayed in the help documentation
                    'longHelp' => '',
                ),
            );
        }
    
    
        /**
         * Method to be used for my MyEndpoint/GetExample endpoint
         */
        public function checkConta($api, $args){
            // Get website value that comes from the form
            //$url = $args['numero_conta'];
            $resultado = $this->checkContaExistente($args['numero_conta']);
    
            return [
                // Checking is successful
                'status' => 'success',
                // Get verifying result
                'duplicado' => $resultado,
            ];
    
    
            //return $args;
        }
    
    
        public function checkContaExistente($numero_conta){
        global $db;
      $GLOBALS['log']->fatal('GEEK LOG API: checkContaExistente');
      $query =  "SELECT c.numero_conta_c FROM contacts_cstm c "; 
      $query.= "WHERE c.numero_conta_c = '$numero_conta' LIMIT 1";
      $GLOBALS['log']->fatal('GEEK LOG API: $query '.$query);
      $results = $db->query($query, true);
      $row = $db->fetchByAssoc($results);
      $num_rows = $results->num_rows;
      $GLOBALS['log']->fatal('GEEK LOG API: checkContaExistente fim');
    
    
    
    
      if($num_rows > 0)
      return true;
      else
      return false;
    
    
      }
    
    
    }
    
    
    ?>
    

    My call in .js file

    var url = app.api.buildURL('Contacts', 'CheckContaDuplicadaApi'),
            // Website value, that should be verified
                post_data = {numero_conta: '22103'};
            // Call verification mechanism
            app.api.call('GET', url, post_data, {
                success: _.bind(function (data) {
                    // Property "status" equal "success", if script ____________________// worked without errors
                    if (data.status === 'success') {
                        console.log("LOG success");
                        // Verification is successful
                        if (data.duplicado) {
                            // Continue saving
                            console.log("LOG success duplicado");
                        } 
                        else {
                            console.log("LOG success não duplicado");
                        }
                    }
                }, this)
            });
    

    But, this not work. This response:

    {"error":"not_found","error_message":"Could not find record: CheckContaDuplicadaApi in module: Contacts"}
    "NetworkError: 404 Not Found - https://bvcasr9010.trial.sugarcrm.com/rest/v10/Contacts/CheckContaDuplicadaApi"

    My endPoint in http://{site url}/rest/v10/help

    Please, help-me.

  • Hi Rodolfo,

    You should mention the module name in the api call.

    Please replace the line

    'path' => array('CheckContaDuplicadaApi'), 

    with

    'path' => array('Contacts','CheckContaDuplicadaApi'), 

    Hope it helps!