Consume API from within Sugar

Hi, I would like to add a custom button to the opportunities module that makes a call to a custom endpoint that I created inside Sugar

The endpoint requires authentication, so I was wondering what would be the best way to call this endpoint from within the record.js file?

Thanks in advance  

  • You could use a rowaction on Opportunities, adding an Action to the blue "Edit" dropdown in Record View.

    For example, in 

    custom/modules/Opportunities/clients/base/views/record/record.php

    $viewdefs['Opportunities']['base']['view']['record'] = array (
      'buttons' =>
      array (
        0 =>
        array (
          'type' => 'button',
          'name' => 'cancel_button',
          'label' => 'LBL_CANCEL_BUTTON_LABEL',
          'css_class' => 'btn-invisible btn-link',
          'showOn' => 'edit',
          'events' =>
          array (
            'click' => 'button:cancel_button:click',
          ),
        ),
        1 =>
        array (
          'type' => 'rowaction',
          'event' => 'button:save_button:click',
          'name' => 'save_button',
          'label' => 'LBL_SAVE_BUTTON_LABEL',
          'css_class' => 'btn btn-primary',
          'showOn' => 'edit',
          'acl_action' => 'edit',
        ),
        2 =>
        array (
          'type' => 'actiondropdown',
          'name' => 'main_dropdown',
          'primary' => true,
          'showOn' => 'view',
          'buttons' =>
          array (
            0 =>
            array (
              'type' => 'rowaction',
              'event' => 'button:edit_button:click',
              'name' => 'edit_button',
              'label' => 'LBL_EDIT_BUTTON_LABEL',
              'acl_action' => 'edit',
            ),
    //add your button to the list in the desired position
            1 =>
                  array (
                    'type' => 'rowaction',
                    'event' => 'button:do_your_thing_button:click',
                    'name' => 'do_your_thing',
                    'label' => 'LBL_DO_YOUR_THING',
                    'acl_action' => 'edit',
                  ),
    ...

     

    Then you can use your record controller custom/modules/Opportunities/clients/base/views/record/record.js

    to take the action when the button is clicked:

    ({
      extendsFrom: 'RecordView',

      initialize: function(options){
        this._super('initialize', [options]);
        this.context.on('button:do_your_thing_button:click', this.doYourThingClicked, this);
      },
      doYourThingClicked: function(){
        //call your API here
      }
    )}

    To use the API build your url using app.api.buildURL, and then use app.api.call to execute it and determine your response for success or failure.


    For example this one is for an Action from record view that would resync that particular record to our Oracle system.

    It asks the user for confirmation that they want to resync and when the user confirms it executes the custom API passing the bean_id.

      resyncOracle: function (){
        //queues up scheduler job to resync the recor
        //Admin only
        var self = this;
        app.alert.show('confirm-resync', {
          level: 'confirmation',
          messages: 'Confirm to Sync to Oracle',
          autoClose: false,
          onConfirm: function(){
            var bean_id = self.model.get('id');
                url = app.api.buildURL('syncContractToOracle/'+bean_id),
            app.api.call('GET', url, null, {
              success: _.bind( function (o){
                app.alert.show('resync',{
                  level: 'success',
                  messages: 'Sync to Oracle Added to Scheduler',
                  autoClose: 'false'
                });
              }),
              error: _.bind( function(e){
                app.alert.show('resync-failed',{
                  level: 'error',
                  messages: 'Sync to Oracle Failed, contact Sugar Administrator',
                  autoClose: 'false'
                });
              })
            });
          },
          onCancel: function(){
            alert("Cancelled!");
          }
        });
      },

     

    HTH

    FrancescaS