2

I'm trying to update a record residing in salesforce table. Im using the Java HttpClient REST API to do the same. Getting an error when we use PATCH to update a record in 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"}]

Also tried doing the following:

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

This also returns the same error. We are using apache tomcat with commons-httpclient-3.1.jar library. Please advise on how this can be done.

1

3 Answers 3

1

Please check if you you're using the right implementation of PATCH method, see: Insert or Update (Upsert) a Record Using an External ID.

Also check if your REST URL is correct, probably your objectId is not passed in correctly from Javascript.

The ObjectName is the Name of an Salesforce table i.e. 'Contact'. And Id is the Id of a specific record you want to update in the table.

Similar:

Sign up to request clarification or add additional context in comments.

Comments

0

As I think you're aware, commons httpclient 3.1 does not have the PATCH method, and the library has been end-of-lifed. In your code above, you're trying to add the HTTP method as a query parameter, which doesn't really make sense.

As seen on SalesForce Developer Board, you can do something like this instead:

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

This allows you to PATCH without switching out your httpclient library.

Comments

0

I created this method to send the patch request via the Java HttpClient class. I am using 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();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.