SugarCRM:如何在REST端点中获取json数据

时间:2016-12-07 10:30:06

标签: php json rest sugarcrm

我在记录列表视图中添加了一个额外的操作;

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

这里的教程 http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Integration/Web_Services/v10/Extending_Endpoints/ 解释如何创建端点。

然而,它并没有解释如何获取json数据(所选id的字符串化数组);

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包含

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

是否可以访问json数据?

2 个答案:

答案 0 :(得分:0)

我做了同样的事,但我的休息api是用java编写的。我使用java @Path注释来注释我的get方法。然后我将上面的rest api代码部署到服务器(在我的情况下是Tomcat)。启动服务器然后点击@Path形成的URL将在浏览器上显示json数据。

答案 1 :(得分:0)

解决方案是将调用方法更改为create,将端点方法更改为POST; $args现在包含

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

    [__sugar_url] => v10/Opportunities/Export2
)

PUT vs POST in REST - 我正在使用GET,因为我没有计划更改任何内容,但在GET请求中通常会忽略正文。