0

I have added an extra action to the recordlist view;

custom/modules/Opportunities/clients/base/views/recordlist/recordlist.js:

({
    extendsFrom: 'RecordlistView',

    initialize: function(options) {
        this._super("initialize", [options]);
        //add listener for custom button
        this.context.on('list:opportunitiesexport2:fire', this.export2, this);
    },
    export2: function() {
        //gets an array of ids of all selected opportunities
        var selected = this.context.get("mass_collection").pluck('id');
        if (selected) {
            return App.api.call('read', 
            App.api.buildURL('Opportunities/Export2'),
            {'selected_ids':selected},
            {
                success: function(response) {
                    console.log("SUCCESS");
                    console.log(response);
                },
                error: function(response) {
                    console.log('ERROR');
                    console.log(response);
                },
                complete: function(response){
                    console.log("COMPLETE");
                    console.log(response);
                },
                error: function(response){
                    console.log("ERROR");
                    console.log(response);
                }
            });
        }
    },
})

The tutorial here http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Integration/Web_Services/v10/Extending_Endpoints/ Explains how to create an endpoint.

However it doesn't explain how to get the json data (the stringified array of selected ids);

custom/modules/Opportunities/clients/base/api/OpportunitiesApi.php:

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

                //set authentication
                'noLoginRequired' => false,

                //endpoint path
                'path' => array('Opportunities', 'Export2'),

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

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

                //short help string to be displayed in the help documentation
                'shortHelp' => 'Export',

                //long help to be displayed in the help documentation
                'longHelp' => 'custom/clients/base/api/help/MyEndPoint_MyGetEndPoint_help.html',
            ),
        );
    }

    /**
     * Method to be used for my MyEndpoint/GetExample endpoint
     */
    public function Export2($api, $args)
    {
        //how to access $args['selected_ids']?
    }
}

$args contains

Array
(
    [__sugar_url] => v10/Opportunities/Export2
)

Is it possible to access the json data?

A G
  • 997
  • 2
  • 18
  • 36

2 Answers2

0

I did the same but my rest api was coded in java. I used java @Path annotation to annotate my get method. I then deployed above rest api code to a server(Tomcat in my case). Starting the server and then hitting the URL formed by the @Path will give you the json data on the browser.

Isha Agarwal
  • 420
  • 4
  • 12
0

The solution was to change the call method to create and the endpoint method to POST; $args now contains

Array
(
    [selected_ids] => Array
        (
            [0] => 0124a524-accc-11e6-96a8-005056897bc3
        )

    [__sugar_url] => v10/Opportunities/Export2
)

PUT vs POST in REST - I was using GET because I didn't plan to change anything, but the body is typically ignored in a GET request.

Community
  • 1
  • 1
A G
  • 997
  • 2
  • 18
  • 36