Update a Record
You use the sObject Rows resource to update records. Provide the updated record
information in your request data and use the PATCH method of the resource with a
specific record ID to update that record. Records in a single file must be of the same
object type.
In the following example, the Billing City within an Account is updated. The updated record information is provided in patchaccount.json.
- Example for updating an Account object
-
curl https://MyDomainName.my.salesforce.com/services/data/v63.0/sobjects/Account/001D000000INjVe -H "Authorization: Bearer token" -H "Content-Type: application/json" -d @patchaccount.json -X PATCH
- Example request body patchaccount.json file for updating fields in an Account object
-
{ "BillingCity" : "San Francisco" }
- Example response body for updating fields in an Account object
- none returned
- Error response
- See Status Codes and Error Responses.
The following example uses Java and HttpClient to update a record using REST API. Note
that there is no PatchMethod in HttpClient, so PostMethod is overridden to return
“PATCH” as its method name. This example assumes the resource URL has been
passed in and contains the object name and record
ID.
If
you use an HTTP library that doesn't allow overriding or setting an arbitrary HTTP
method name, you can send a POST request and provide an override to the HTTP method via
the query string parameter _HttpMethod. In the
PATCH example, you can replace the PostMethod line with one that doesn't use
override:
public static void patch(String url, String sid) throws IOException {
PostMethod m = new PostMethod(url) {
@Override public String getName() { return "PATCH"; }
};
m.setRequestHeader("Authorization", "OAuth " + sid);
Map<String, Object> accUpdate = new HashMap<String, Object>();
accUpdate.put("Name", "Patch test");
ObjectMapper mapper = new ObjectMapper();
m.setRequestEntity(new StringRequestEntity(mapper.writeValueAsString(accUpdate), "application/json", "UTF-8"));
HttpClient c = new HttpClient();
int sc = c.executeMethod(m);
System.out.println("PATCH call returned a status code of " + sc);
if (sc > 299) {
// deserialize the returned error message
List<ApiError> errors = mapper.readValue(m.getResponseBodyAsStream(), new TypeReference<List<ApiError>>() {} );
for (ApiError e : errors)
System.out.println(e.errorCode + " " + e.message);
}
}
private static class ApiError {
public String errorCode;
public String message;
public String [] fields;
}
PostMethod m = new PostMethod(url + "?_HttpMethod=PATCH");