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
  • Yes sure . Please find it below :

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

    //set authentication
    'noLoginRequired' => false,

    //endpoint path
    'path' => array('Accounts', 'mycustomeAPI'),

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

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

    //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' => '',
    )
    );
    }

    Thanks.

Children
  • was right to ask you for the definition, you are missing a variable in the path for the id taht you are passing.

    in general, the number of elements in your path need to equal the number of elements int he path vars.

    your path should be:

    'path' => array('Accounts', 'mycustomeAPI', ?),

  • Yeah  and the code the api.call must be change from 'update' to 'read', because is a action type 'GET'

    app.api.call('read', app.api.buildURL('Accounts/mycustomAPI/' + this.model.get('id')), null {

  • What should be the  'path' then ?

    Can i make it like this : 

    path' => array('Accounts', 'mycustomeAPI', id),

  • No it should have the question mark, as I stated above.

    See:
    support.sugarcrm.com/.../

  • app.api.call('update',url,params,{...}); 

    what is the params here ? What is the use of this ? 

    I am new to it so please spare me..

  • The question mark indicates a placeholder for the input, the path_vars then name that placeholder.

    Path_vars are in the same order as the path

    So the first two in your path do not have an associated variable, but the third one does: id.

    if you had two varialbles, you would have two placeholders.

    This is for a GET method of course.

    Hope this helps clarify. 

  • Thanks Francesca, for the information and I have some better  understanding now.

    <<Question>>

    app.api.call('update',url,params,{...});

    What is param here ?  What is the use of this ? 

  • There are essentially two ways to pass parameters in an API, one is in the url itself, and one is in the request body.

    The "param" in the example above are the parameters which are passed in the request body, and received by the custom api accordingly.

    See if you can find a primer on APIs that you can read through to familiarize yourself with the different methods and parameters. It will help. 

  • Hi Francesca,

    I am not getting any response on click of button. No alert message. Also not sure whether data is coming back or not .  Please refer codes below: 

    Path modified :
    path' => array('Accounts', 'mycustomeAPI', '?')

    (Used in Postman) [checked in postman I am getting a response]
    API URL : http://{{CRM_URL}}/sugarcrm/rest/v10/Accounts/mycustomAPI/?id=62aace7c-8729-11eb-a0e4-0e1a95bf95b1

    record.js
    custom_button:function(){
    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',
    title: app.lang.getAppString('ERR_INTERNAL_ERR_MSG'),
    messages: err,
    autoClose: false
    });
    },
    });
    }

    Thanks

  • In postman the url must looks:

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

    have a issue in the code, are missing one comma after position of "params"

    app.api.call('read', app.api.buildURL('Accounts/mycustomAPI/'+model.get('id')), null, {...});