Custom api sugarcrm 8.0 not working

The function of my code is, i want it to capitalize the initial letters of the field firstname and last name when they click the Upper name button.

here's the code:

<?php

class UpperNameApi extends SugarApi
{
public function registerApiRest()
{
return array(
'UpperNameRequest' => array(
//request type
'reqType' => 'GET',

//endpoint path
'path' => array('Leads', 'UpperName'),

//endpoint variables
'pathVars' => array('module','record'),

//method to call
'method' => 'UpperNameMethod',

//short help string to be displayed in the help documentation
'shortHelp' => 'Example endpoint',

//long help to be displayed in the help documentation
'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
),
);
}

/**
* Method to be used for my MyEndpoint/GetExample endpoint
*/
public function UpperNameMethod($api, $args)
{
if (isset($args['record']) && !empty($args['record'])) {
$bean = BeanFactory::getBean('Leads', $args['record']);

if (!empty($bean->id)) {
$first = $bean->first_name;
$first = ucwords($first);
$bean->first_name = $first;

$last = $bean->last_name;
$last = ucwords($last);
$bean->last_name = $last;
$bean->save();
}

return 'success';
}


return 'failed';

}

}

When i click the "Upper name button" it doesn't work:

i also tried using postman but this showed up: "error_message": "Could not find a route with 3 elements"

UPDATED

ive made another request in postman with this values but it shows: "A parameter in your request was invalid."

UPDATED AGAIN

PROBLEM FIXED.

Parents
  • Good morning Karl,

    you have a problem with how you are defining your path and pathVars.

    Whenever you get a message like "Could not find a route with 3 elements" it is telling you that you are passing 3 elements, in your case "Leads" "UpperName" and the "module" (which I believe is the ID of the lead you want to process), but the end point is expecting a number of elements other than 3.

    Review:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.0/Integration/Web_Services/REST_API/E… 

    in your case your endpoint expects only 2 elements 'Leads' and 'UpperName', what you really want is 'Leads','UpperName', and the ID of the record you want to process.

    So instead of:

    //endpoint path
    'path' => array('Leads', 'UpperName'),

    //endpoint variables
    'pathVars' => array('module','record'),

    Use:

    //endpoint path
    'path' => array('Leads', 'UpperName','?'),

    //endpoint variables
    'pathVars' => array('','', 'record'),

    Notice that the third element in your path, the ?, is your variable placeholder, and the third element in your pathVars is the name that will be associated with that variable in your $args. So $args['record'] is the ID that you are passing.

    Now your API has three elements as does your call:

    /Leads/UpperName/<record_id>

    where record_id is the id of the lead you are wanting to update.

    e.g.

     /Leads/UpperName/a22a565a-8f90-11e8-b033-001a4a160170

    HTH,

    FrancescaS

Reply
  • Good morning Karl,

    you have a problem with how you are defining your path and pathVars.

    Whenever you get a message like "Could not find a route with 3 elements" it is telling you that you are passing 3 elements, in your case "Leads" "UpperName" and the "module" (which I believe is the ID of the lead you want to process), but the end point is expecting a number of elements other than 3.

    Review:

    https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_8.0/Integration/Web_Services/REST_API/E… 

    in your case your endpoint expects only 2 elements 'Leads' and 'UpperName', what you really want is 'Leads','UpperName', and the ID of the record you want to process.

    So instead of:

    //endpoint path
    'path' => array('Leads', 'UpperName'),

    //endpoint variables
    'pathVars' => array('module','record'),

    Use:

    //endpoint path
    'path' => array('Leads', 'UpperName','?'),

    //endpoint variables
    'pathVars' => array('','', 'record'),

    Notice that the third element in your path, the ?, is your variable placeholder, and the third element in your pathVars is the name that will be associated with that variable in your $args. So $args['record'] is the ID that you are passing.

    Now your API has three elements as does your call:

    /Leads/UpperName/<record_id>

    where record_id is the id of the lead you are wanting to update.

    e.g.

     /Leads/UpperName/a22a565a-8f90-11e8-b033-001a4a160170

    HTH,

    FrancescaS

Children