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
  • 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.
Children
  • 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");