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
  • From Postman : 

    With this URL

    http://{{CRM_URL}}/sugarcrm/rest/v10/Accounts/mycustomAPI/62aace7c-8729-11eb-a0e4-0e1a95bf95b1

    404 Not Found
    {
        "error""no_method",
        "error_message""Could not find a route with 3 elements"
    }

    But with below URL , Response is coming.... 

    http://{{CRM_URL}}/sugarcrm/rest/v10/Accounts/mycustomAPI/?id=62aace7c-8729-11eb-a0e4-0e1a95bf95b1

    From Browser(Sugar UI)

    On click of button : Below Error

     GET18.232.89.200/.../5bb2d56a-b6f9-11eb-a74d-0e1a95bf95b1 404 (Not Found)

    registerApiRest()

    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;
        }

    record.js

    app.api.call('read', app.api.buildURL('Accounts/mycustomAPI/' + this.model.get('id')), null,{
    success:function(fdata)
    {
    app.alert.show('Activation-Initiated', {
    level: 'error',
    messages: 'Customer Is Activated',
    autoClose: false
    });
    },
    error: function(error){
    app.alert.show('err', {
    level:'error',
    messages: 'Customer Is Not Activated',
    autoClose: false
    });
    },
    });

    Please suggest the changes.

    Thanks.

Children