Hello im trying to call a custom api through this code in sugarcrm:
({
extendsFrom: 'RowactionField',
defaultFirsName: 'first_name',
defaultLastName: 'last_name',
initialize: function (options) {
this._super('initialize', [options]);
this.def.first_name = _.isEmpty(this.def.first_name) ? this.defaultFirsName : this.def.first_name;
this.def.last_name = _.isEmpty(this.def.last_name) ? this.defaultLastName : this.def.last_name;
},
/** * Rowaction fields have a default event which calls rowActionSelect */
rowActionSelect: function () {
this.upper_name();
},
upper_name: function () {
var first = this.model.get(this.def.first_name);
var last = this.model.get(this.def.last_name);
var fullName = first + last;
if (fullName) {
app.alert.show('name-check-msg', {
level: 'success',
messages: 'Firstname and Lastname filled.',
autoClose: true
});
}
else {
app.alert.show('name-check-msg', {
level: 'error',
messages: 'First name and last name must be filled.',
autoClose: false
});
}
var self = this;
url = app.api.buildURL('Leads', 'UpperName', null, {
record: this.model.get('id')
});
app.api.call('GET', url, {
success: function (data) {
app.alert.show('itsdone', {
level: 'success',
messages: 'Confirmed to uppercase name.',
autoClose: true
});
},
error: function (error) {
app.alert.show('err', {
level: 'error',
title: app.lang.getAppString('ERR_INTERNAL_ERR_MSG'),
messages: err
});
},
});
}
})
the name is "uppernamebutton.js" its functions is, it checks if the firstname and lastname is blank and will show an error message to fill up the fields then calls the api to Uppercase the first letters of the names.
Here's the code for the custom api, i named it "UpperNameApi.php":
<?php
class UpperNameApi extends SugarApi
{
public function registerApiRest()
{
return array(
'UpperNameRequest' => array(
//request type
'reqType' => 'POST',
//endpoint path
'path' => array('Leads', 'UpperName'),
//endpoint variables
'pathVars' => array('module',''),
//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',
),
);
}
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';
}
}
pls help to those genius coder out there.
From what I can see there are 2 problems with your app.api.call:
'GET'
, but instead it should be'read'
for GET requests,'update'
for PUT requests,'delete'
for DELETE requests and'create'
for POST requests.reqType => 'POST'
you should be using app.api.call('create', url,
app.api.call('create', url, {}, {
EDIT:
Also I noticed that you use $args['record']
in your function. You are currently using buildURL to pass that value, which means you set it via the query-string of the URL, which probably(?) works for requests other than GET, however usually one of the following 2 ways are used for non-GET calls, e.g. POST:
passing the record id via the endpoint path:
recommended way for single IDs
'path' => array('Leads', '?' 'UpperName'),
'pathVars' => array('module','record',''),
Notes:
path
contains a placeholder ?
which will be filled by the caller with the record id.pathVars
has record
at the same (second) position as the placeholder int he path, which causes that part of the URL to be saved into $args['record']
(similar to first part getting saved into $args['module']
, which will always be 'Leads'
for this API).In javascript you will have to adjust the API call URL accordingly:
url = app.api.buildURL('/Leads/' + this.model.get('id') + '/UpperName');
Notice how the ID goes into the second part of the URL (where the placeholder is defined in the API)
passing the record id via the request payload
recommended way for passing multiply IDs at once or for passing other arguments than record ID
Putting the record ID inot the data object of app.api.call, so that it will be written into $args. app.api.call('create', url, {record: this.model.get('id')}, {