On click of custom button , how can I call API ?

Query : I have an API in Acconts/clients/base/api/mycustomAPI.php. On click of the button (custom button created) , it should call that API. 

Sugar CRM version used : 10.3 

API  url :3.82.61.201/.../

My Code: (record.js)

({
extendsFrom:'RecordView',
initialize:function(options){
this._super('initialize',[options]);
//add listener for custom button
this.context.on('button:submit_button:click', this.submit_button, this);
},
submit_button:function(){
app.api.call('update',app.api.buildURL('Accounts/' + this.model.get('id') + '/mycustomAPI') ,null {
success:function(fdata)
{
app.alert.show('success', {
level: 'error',
messages: 'Activated.',
autoClose: false
});
},
error: function(error){
app.alert.show('err', {
level:'error',
title: app.lang.getAppString('ERR_INTERNAL_ERR_MSG'),
messages: err,
autoClose: false
});
},
});
}
})

Please help me with the code here. !!!

Parents Reply Children
  • <<Discussion>>

    our extended class will contain two methods. The first method is registerApiRest() and the second method can be named anything you choose but it is important to note that it will need to match the name of the method you specified for your endpoin.

    What if we have one more method and I want on click of button that third method should be called.

    Any Suggestions ??

    Example

    <?php

    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

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

    //set authentication
    'noLoginRequired' => false,

    //endpoint path
    'path' => array('Accounts', 'mycustomAPI', '?'),

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

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

    //short help string to be displayed in the help documentation
    'shortHelp' => 'get all Cusotmer By Customer ID in SugarCRM',

    //long help to be displayed in the help documentation
    'longHelp' => '',
    )
    );
    }

    public function mycustomMethod($api, $args)
    {
    //custom logic
    return $args;
    }
    public function mysecondcustomMethod()
    {
    //custom logic
    return something;
    }
    }