【问题标题】:Creating multiple records with the v10 REST API in SugarCRM在 SugarCRM 中使用 v10 REST API 创建多条记录
【发布时间】:2014-03-04 15:23:52
【问题描述】:

在旧的 v4.x API 中,可以在一个 POST 请求中使用 create or update multiple records,但在新的 v10 REST API 中,这不再被记录。有谁知道该怎么做?或者是否有可能?

我尝试了一些方法,例如将记录作为 JSON 数组发布,但这只会创建一个空记录。

[{"name":"Case 2"},{"name":"Case 3"}]

或者,如果 SugarCRM 中存在创建或更新多个记录的用例,也可以。我可以轻松地使用 Fiddler 来阅读他们如何格式化 JSON,然后自己使用它。

【问题讨论】:

    标签: json api rest sugarcrm


    【解决方案1】:

    v10 API 有一个“批量”端点,允许您一次提交多个 API 调用。

    查看 {url}/rest/v10/help 并向下滚动到 /bulk 了解更多详情

    【讨论】:

      【解决方案2】:

      我最近自己遇到了这个问题,也没有找到关于这个或 v10 中可用的任何 API 函数的文档。我个人对此确实有需求,并发现最好的解决方案是创建一个custom API entry point

      这是我为了完成多条目提交而编写的代码。它不提供任何错误处理,并且是针对我的需要而编写的,但这应该涵盖大多数情况。创建这些文件后,您必须从管理仪表板“管理>>修复>>快速修复和重建”运行快速修复重建,这是注册新入口点所必需的。

      文件:custom/clients/base/api/SetEntriesApi.php

      <?php
      
      /*
       * Copyright (C) 2014 DirectPay
       *
       * This program is free software; you can redistribute it and/or
       * modify it under the terms of the GNU General Public License
       * as published by the Free Software Foundation; either version 2
       * of the License, or (at your option) any later version.
       *
       * This program is distributed in the hope that it will be useful,
       * but WITHOUT ANY WARRANTY; without even the implied warranty of
       * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       * GNU General Public License for more details.
       *
       * You should have received a copy of the GNU General Public License
       * along with this program; if not, write to the Free Software
       * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
       */
      
      if (!defined('sugarEntry') || !sugarEntry)
          die('Not A Valid Entry Point');
      
      class SetEntriesApi extends SugarApi {
      
          public function registerApiRest() {
              return array(
                  //POST
                  'SetEntries' => array(
                      //request type
                      'reqType' => 'POST',
                      //endpoint path
                      'path' => array('record', 'set_entries'),
                      //method to call
                      'method' => 'setEntriesMethod',
                      //short help string to be displayed in the help documentation
                      'shortHelp' => 'Provides functionality to create & update multiple records.',
                      //long help to be displayed in the help documentation
                      'longHelp' => 'custom/clients/base/api/help/SetEntriesApi_help.html',
                  ),
              );
          }
      
          public function setEntriesMethod($api, $args) {
              if (empty($args)) {
                  return false;
              }
              $results = array();
              foreach ($args as $module => $records) {
                  if (is_array($records)) {
                  foreach ($records as $fieldsArray) {
                          $sugarBean = $this->_processEntry($module, $fieldsArray);
                          $results[$module][] = $sugarBean->id;
                      }
                  }
              }
              return $results;
          }
      
          private function _processEntry($module, $fieldsArray) {
              if (array_key_exists('id', $fieldsArray)) {
                  $sugarBean = BeanFactory::retrieveBean($module, $fieldsArray['id']);
              } else {
                  $sugarBean = BeanFactory::newBean($module);
              }
              if (is_null($sugarBean)) {
                  return null;
              }
              foreach ($fieldsArray as $field => $data) {
                  $sugarBean->$field = $data;
              }
              $sugarBean->save();
              return $sugarBean;
          }
      
      }
      
      ?>
      

      文件:custom/clients/base/api/help/SetEntriesApi_help.html

      <h2>Overview</h2>
      <span class="lead">
          This is a custom setEntries endpoint. This is used to create or update 
          multiple modules and records with one call. This was originally available in 
          the older versions of the API, but was removed in v10. This is not a ported
          version from v4.1 and rather a quick recreation of that functionality modified
          slightly to allow for multiple modules.
      </span>
      
      <h2>Path Variables</h2>
      <span class="lead">
          This endpoint does not accept any path variables.
      </span>
      
      <h2>Input Parameters</h2>
      <table class="table table-hover">
          <thead>
          <tr>
              <th>Name</th>
              <th>Type</th>
              <th>Description</th>
          </tr>
          </thead>
          <tbody>
          <tr>
              <td>
                  args
              </td>
              <td>
                  Array
              </td>
              <td>
                  Data array to pass to the endpoint.
              </td>
          </tr>
          </tbody>
      </table>
      
      <h2>Result</h2>
      <table class="table table-hover">
          <thead>
          <tr>
              <th>Name</th>
              <th>Type</th>
              <th>Description</th>
          </tr>
          </thead>
          <tbody>
          <tr>
              <td>
                  results
              </td>
              <td>
                  Array
              </td>
              <td>
                  Array of modules that contain a nested array of the IDs of the updated records and/or newly created record.
              </td>
          </tr>
          </tbody>
      </table>
      
      <h3>Output Example</h3>
      <pre class="pre-scrollable">
      {
        "Accounts": [
          "92e26a99-9e7a-3dca-9ab0-53c6d6833d5f",
          "991b8007-a517-0c8b-6b69-53c6d6fd70fb",
          "9a129144-0f61-e808-00c2-53c6d674bd04",
          "addc4404-ae4a-c031-586b-53c6d60f70dd"
        ]
      }
      </pre>
      
      <h2>Change Log</h2>
      <table class="table table-hover">
          <thead>
          <tr>
              <th>Version</th>
              <th>Change</th>
          </tr>
          </thead>
          <tbody>
          <tr>
              <td>
                  v10
              </td>
              <td>
                  Added <code>/record/set_entries</code> POST endpoint.
              </td>
          </tr>
          </tbody>
      </table>
      

      自定义入口点遍历多维数组并使用CRUD handling in BeanFactory 单独创建或更新记录。为了更新现有记录,必须将 id 与记录一起传递。这是传递给端点的 json 代码示例。

      JSON 代码:

      {
        "Contacts": {
          "id": "9a129144-0f61-e808-00c2-53c6d674bd04",
          "name": "Contact Name"
        },
        "Accounts": {
          "name": "Account Name"
        }
      }
      

      我希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        @TO_web的回复确实帮了我很大的忙,但是传给endpoint的json肯定是这样的:

        {"Contacts":
            [
                {
                    "id":"89905d08-5c98-604e-9f49-55d5e670161b",
                    "custom_field":"Testing"
                }
            ],
        "Accounts":
            [
                {
                    "name":"Testing"
                }
            ]
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多