【问题标题】:Update records using Salesforce REST API - Patch method not working使用 Salesforce REST API 更新记录 - 补丁方法不起作用
【发布时间】:2013-06-24 05:45:51
【问题描述】:

我正在尝试更新驻留在 salesforce 表中的记录。我使用 Java HttpClient REST API 来做同样的事情。当我们使用 PATCH 更新 Salesforce 中的记录时出现错误。

PostMethod post = new PostMethod(
    instanceUrl + "/services/data/v20.0/sobjects/" +
    objectName + "/" + Id + "?_HttpMethod=PATCH"
);

[{"message":"HTTP Method 'PATCH' not allowed. Allowed are HEAD,GET,POST","errorCode":"METHOD_NOT_ALLOWED"}]

还尝试执行以下操作:

PostMethod post = new PostMethod(
    instanceUrl + "/services/data/v20.0/sobjects/" + objectName + "/" + Id)
    {
        public String getName() { return "PATCH"; 
    }
};

这也会返回相同的错误。我们正在使用带有 commons-httpclient-3.1.jar 库的 apache tomcat。请告知如何做到这一点。

【问题讨论】:

    标签: rest salesforce updates


    【解决方案1】:

    请检查您是否使用了正确的 PATCH 方法实现,请参阅:Insert or Update (Upsert) a Record Using an External ID

    还要检查你的 REST URL 是否正确,可能你的 objectId 没有从 Javascript 中正确传入。

    ObjectName 是 Salesforce 表的名称,即“联系人”。而 Id 是您要在表中更新的特定记录的 Id。

    类似的:

    【讨论】:

      【解决方案2】:

      我想您知道,commons httpclient 3.1 没有 PATCH 方法,并且该库已终止使用。在上面的代码中,您尝试将 HTTP 方法添加为查询参数,这实际上没有任何意义。

      正如在SalesForce Developer Board 上看到的,您可以这样做:

      HttpClient httpclient = new HttpClient();
      PostMethod patch = new PostMethod(url) {
        @Override
        public String getName() {
          return "PATCH";
        }
      };
      ObjectMapper mapper = new ObjectMapper();
      StringRequestEntity sre = new StringRequestEntity(mapper.writeValueAsString(data), "application/json", "UTF-8");
      patch.setRequestEntity(sre);
      httpclient.executeMethod(patch);
      

      这允许您在不关闭 httpclient 库的情况下进行 PATCH。

      【讨论】:

        【解决方案3】:

        我创建了这个方法来通过 Java HttpClient 类发送补丁请求。 我正在使用 JDK V.13

        private static VarHandle Modifiers; // This method and var handler are for patch method
            private static void allowMethods(){
                // This is the setup for patch method
                System.out.println("Ignore following warnings, they showed up cause we are changing some basic variables.");
        
                try {
        
                    var lookUp = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
                    Modifiers = lookUp.findVarHandle(Field.class, "modifiers", int.class);
        
                } catch (IllegalAccessException | NoSuchFieldException e) {
                    e.printStackTrace();
        
                }
                try {
        
                    Field methodField = HttpURLConnection.class.getDeclaredField("methods");
                    methodField.setAccessible(true);
                    int mods = methodField.getModifiers();
        
                    if (Modifier.isFinal(mods)) {
                        Modifiers.set(methodField, mods & ~Modifier.FINAL);
                    }
        
                    String[] oldMethods = (String[])methodField.get(null);
        
                    Set<String> methodsSet = new LinkedHashSet<String>(Arrays.asList(oldMethods));
                    methodsSet.addAll(Collections.singletonList("PATCH"));
                    String[] newMethods = methodsSet.toArray(new String[0]);
                    methodField.set(null, newMethods);
        
                } catch (NoSuchFieldException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-24
          • 2022-11-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多