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.