Dan Ross

Custom button on Accounts detail view fails sometimes

Discussion created by Dan Ross on Dec 7, 2016
Latest reply on Dec 8, 2016 by Dan Ross

I have a strange problem that is easy to reproduce every time. I've added a custom button to the Accounts detail view as follows.

custom/modules/Accounts/clients/base/views/record/record.php:

$viewdefs['Accounts'] = array(
    'base' => array(
          'view' => array(
               'record' => array(
                    'buttons' => array(
                         ...
                         2 => array(
                              'type' => 'actiondropdown',
                              'name' => 'main_dropdown',
                              'primary' => true,
                              'showOn' => 'view',
                              'buttons' => array(
                                   ...
                                   array(
                                        'type' => 'rowaction',
                                        'event' => 'button:add_order:click',
                                        'name' => 'add_order',
                                        'label' => 'LBL_ADD_ORDER',
                                        'acl_action' => 'view',
                                   ),
...

custom/modules/Accounts/clients/base/views/record/record.js:

({
     extendsFrom: 'RecordView',
     initialize: function (options) {
          app.view.invokeParent(this, {
               type: 'view',
               name: 'record',
               method: 'initialize',
               args:[options]
          });
          //add listener for custom button
          this.context.on('button:add_order:click', this.add_order, this);
     },
     add_order: function() {
          var account_id = escape(this.model.id);
          var url = 'index.php?to_pdf=1&module=Accounts&action=add_order&id='+account_id;// to_pdf=1 disables the HTML template
          $.ajax({
               method: 'get',
               url: url,
               success: function(data){
                    console.log(data);// login to remote system
               }
          });
          return false;
     }
})

custom/modules/Accounts/add_order.php:

error_log('add_order.php LINE 1');
// prints json data with login credentials ...

When I login and go directly to Accounts > click on an account > click on LBL_ADD_ORDER The console.log in function add_order prints this:

<script>
if (parent.location == window.location) {
     window.location = "http:\/\/vmsugar.picbusiness.com\/sugar\/#Accounts";
}
else {
     try {
          window.top.SUGAR.App.bwc.login("index.php?to_pdf=1&module=Accounts&action=add_order&id=079cca8e-b8da-11e6-a2d2-005056a871c7");
     }
     catch (e) {
          try {
               parent.SUGAR.App.bwc.login("index.php?to_pdf=1&module=Accounts&action=add_order&id=079cca8e-b8da-11e6-a2d2-005056a871c7");
          }
          catch (e) {
               window.location = "http:\/\/vmsugar.picbusiness.com\/sugar\/#Accounts";
          }
     }
}
</script>

That javascript being returned by the server is being generated from here:

include/MVC/SugarApplication.php:  window.top.SUGAR.App.bwc.login(' . json_encode($loginRedirect) . ');

Which I suppose means SugarCRM thinks I'm not logged in yet?

I added an "error_log" to the first line of custom/modules/Accounts/add_order.php and it does NOT print in the error log.

The strangest part of this problem is if I click on Quotes or Admin, and then click on Accounts again, choose an account and click on LBL_ADD_ORDER it works!

It will fail over and over, every time, until I click on Quotes or Admin. If I click on Contacts or Opportunities, and then go directly back, it still won't work.

Outcomes