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
  • Hi Sanchez ,

    Link which you have shared helped me to create a button and do some action on click of it. But I want to call an API on click of that button.

  • Hi.

    can you show me the function registerApiRest of your endpoint?

  • 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.

  • 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, {...});

  • 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.

  • I tray the code and the results in local instance is:

    did you repair the instance after changes?

  • Thanks alot Sanchez. This really works. I got to learn many things here. Highly Appreciated. 

  • Issue Solved : Able to call endpoint on click of  button.

    Casue Of Error : After all the changes suggested above , i missed to do repair and rebuild.(MY BAD)

    <<Question>>

    Response of the Endpoint: 

    {
        "status""something",
        "name""something",
    }
    How can I access the response in record.js ?
    For  Example : I want to retrieve the status from response and set that value in some other field.
    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
    });
    },
    });
    I am going one step at a time so please dn't mind my multiple queries.
    Thanks.
  • Ok I found out the way to access response.

    This is an error when setting the value to field in module.

    Error : Uncaught TypeError: Cannot read property 'get' of undefined

    Response of the Endpoint: 

    {
        "status""something",
        "name""something",
    }
     I  retrieve the status from response and trying to set that value in some other field.
    record.js
    app.api.call('read', app.api.buildURL('Accounts/mycustomAPI/' + this.model.get('id')), null,{
    success:function(fdata)
    {
    console.log("Response", fdata.account_type);
    var middleman = this.model.get('middlename_c');
    middleman = fdata.account_type;
    console.log("Account Type", middleman);

    app.alert.show('Activation-Initiated', {
    level: 'success',
    messages: 'Customer Is Activated',
    autoClose: false
    });
    },
    error: function(error){
    app.alert.show('err', {
    level:'error',
    messages: 'Customer Is Not Activated',
    autoClose: false
    });
    },
    });

    Please suggest some changes here.

  • I understand that you need set a value into current model.
    For this task i suggest made this:

    var self = this;
    app.api.call('read', app.api.buildURL('Accounts/mycustomAPI/' + this.model.get('id')), null,{
    success:function(fdata)
    {
    if (fdata){
    console.log("Response", fdata.account_type);
    self.model.set('middlename_c', fdata.account_type); // this line set value into current record
    console.log("Account Type", middleman);

    app.alert.show('Activation-Initiated', {
    level: 'success',
    messages: 'Customer Is Activated',
    autoClose: false
    });
    }
    },
    error: function(error){
    app.alert.show('err', {
    level:'error',
    messages: 'Customer Is Not Activated',
    autoClose: false
    });
    },
    });

  • Thanks Sanchez , now am able to set the value into current model. But this value is not saving in database. Once reload ,it revert back to same old value. How to overcome this situation ? 

  • I think you never click the save action button, therefore this value is never saved in the database.

  • I don't know the logic in the endpoint function, but, i suggest is better that save this value "middlename_c" there in this function and only return the message notify this action.

    $a = BeanFactory::getBean("Accounts", $args['id'], array('disable_row_level_security' => true));
    $a->middlename_c = <<Your value>>;
    $a->save(false);
    return array("code" => 200, "msg" => "Customer Is Activated");

Reply
  • I don't know the logic in the endpoint function, but, i suggest is better that save this value "middlename_c" there in this function and only return the message notify this action.

    $a = BeanFactory::getBean("Accounts", $args['id'], array('disable_row_level_security' => true));
    $a->middlename_c = <<Your value>>;
    $a->save(false);
    return array("code" => 200, "msg" => "Customer Is Activated");

Children
No Data