Sugarcrm 8.0中的自定义API

时间:2019-02-19 06:51:50

标签: php rest api sugarcrm

我的代码的功能是,当他们单击“姓氏”按钮时,我希望它使用大名和姓氏的首字母大写。

代码如下:

<?php

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

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

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

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

    /**
     * Method to be used for my MyEndpoint/GetExample endpoint
     */
    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';

    }

}

当我单击“姓氏按钮”时,它不起作用:

When i click the "Upper name button" it doesn't work:

我也尝试使用邮递员,但出现了:“ error_message”:“找不到包含3个元素的路线”

pic2

已更新

ive在邮递员中使用此值提出了另一个请求,但显示:“您请求中的参数无效。”

pic3

再次更新

问题已解决。

1 个答案:

答案 0 :(得分:0)

此解决方案非常简单,但花了我几个小时才弄清楚。无论如何,答案是'reqType' => 'GET'行应该为'reqType' => 'POST'