dsm1998 2019-02-20 01:12
浏览 83
已采纳

不能在sugarcrm 8.0中调用自定义api

Hello im trying to call a custom api through this code in sugarcrm:

({
    extendsFrom: 'RowactionField',

    defaultFirsName: 'first_name',
    defaultLastName: 'last_name',

    initialize: function (options) {
        this._super('initialize', [options]);

        this.def.first_name = _.isEmpty(this.def.first_name) ? this.defaultFirsName : this.def.first_name;
        this.def.last_name = _.isEmpty(this.def.last_name) ? this.defaultLastName : this.def.last_name;
    },
    /**     * Rowaction fields have a default event which calls rowActionSelect     */
    rowActionSelect: function () {
        this.upper_name();
    },

    upper_name: function () {
        var first = this.model.get(this.def.first_name);
        var last = this.model.get(this.def.last_name);
        var fullName = first + last;

        if (fullName) {
            app.alert.show('name-check-msg', {
                level: 'success',
                messages: 'Firstname and Lastname filled.',
                autoClose: true
            });
        }
        else {
            app.alert.show('name-check-msg', {
                level: 'error',
                messages: 'First name and last name must be filled.',
                autoClose: false
            });


        }

        var self = this;
        url = app.api.buildURL('Leads', 'UpperName', null, {
            record: this.model.get('id')
        });

        app.api.call('GET', url, {
            success: function (data) {
                app.alert.show('itsdone', {
                    level: 'success',
                    messages: 'Confirmed to uppercase name.',
                    autoClose: true
                });
            },
            error: function (error) {
                app.alert.show('err', {
                    level: 'error',
                    title: app.lang.getAppString('ERR_INTERNAL_ERR_MSG'),
                    messages: err
                });
            },
        });
    }
})

the name is "uppernamebutton.js" its functions is, it checks if the firstname and lastname is blank and will show an error message to fill up the fields then calls the api to Uppercase the first letters of the names.

Here's the code for the custom api, i named it "UpperNameApi.php":

<?php

class UpperNameApi extends SugarApi
{
    public function registerApiRest()
    {
        return array(
            'UpperNameRequest' => array(
                //request type
                'reqType' => 'POST',

                //endpoint path
                'path' => array('Leads', 'UpperName'),

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

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

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

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

    public function UpperNameMethod($api, $args)
    {
        if (isset($args['record']) && !empty($args['record'])) {
            $bean = BeanFactory::getBean('Leads', $args['record']);

            if (!empty($bean->id)) {
                $first = $bean->first_name;
                $first = ucwords($first);
                $bean->first_name = $first;

                $last = $bean->last_name;
                $last = ucwords($last);
                $bean->last_name = $last;
                $bean->save();
            }

            return 'success';
        }


        return 'failed';

    }

}

pls help to those genius coder out there.

  • 写回答

1条回答 默认 最新

  • dongyin0628 2019-02-20 08:03
    关注

    From what I can see there are 2 problems with your app.api.call:

    • You got the first argument wrong:
      It should never be 'GET', but instead it should be
      • 'read' for GET requests,
      • 'update' for PUT requests,
      • 'delete' for DELETE requests and
      • 'create' for POST requests.
      As you specified reqType => 'POST' you should be using app.api.call('create', url,
    • If I'm not mistaken the callbacks go in the forth argument, not the third one (that one is for payload data), so you should add an empty object as third argument and pass the callbacks in the 4th, your resulting line should look like this: app.api.call('create', url, {}, {

    EDIT:

    Also I noticed that you use $args['record'] in your function. You are currently using buildURL to pass that value, which means you set it via the query-string of the URL, which probably(?) works for requests other than GET, however usually one of the following 2 ways are used for non-GET calls, e.g. POST:

    passing the record id via the endpoint path:
    recommended way for single IDs

    'path' => array('Leads', '?' 'UpperName'),
    'pathVars' => array('module','record',''),
    

    Notes:

    • path contains a placeholder ? which will be filled by the caller with the record id.
    • pathVars has record at the same (second) position as the placeholder int he path, which causes that part of the URL to be saved into $args['record'] (similar to first part getting saved into $args['module'], which will always be 'Leads' for this API).

    In javascript you will have to adjust the API call URL accordingly:

    url = app.api.buildURL('/Leads/' + this.model.get('id') + '/UpperName');
    

    Notice how the ID goes into the second part of the URL (where the placeholder is defined in the API)

    passing the record id via the request payload
    recommended way for passing multiply IDs at once or for passing other arguments than record ID

    Putting the record ID inot the data object of app.api.call, so that it will be written into $args. app.api.call('create', url, {record: this.model.get('id')}, {

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。