force-rest-api icon indicating copy to clipboard operation
force-rest-api copied to clipboard

updateSObject: "The Id field should not be specified in the sobject data"

Open hf-kklein opened this issue 7 years ago • 1 comments

Hi, I'm using the example code to update an object. The object has an id and some properties, just like this:

@JsonIgnoreProperties(ignoreUnknown=true)
public static class MyObject {
	@JsonProperty(value="Id")
	private String id;
	@JsonProperty(value="some_property__c")
	private String some_property__c;

        public String getId() {return id;}
        public void setId(String Id) {this.id = Id;}
        public String getSome_property__c() {return some_property_c;}
        public void setSome_property__c(String some_propery__c) {this.some_property__c = some_property__c;}
	}

When I try to change the property, my program fails:

final String query = "SELECT id, some_property__c FROM sometable";
QueryResult<MyObject> result = api.query(query, MyObject.class);
for (MyObject mo : result.getRecords()) {
	mo.setSome_property__c("some value");
	api.updateSObject("sometable", mo.getId(), mo);
}

The error message of the API with HTTP status code 400 (bad request) ist:

[
	{
		"message": "The Id field should not be specified in the sobject data.",
		"errorCode": "INVALID_FIELD"
	}
]

The Java error is:

INFO com.force.api.http.Http - Bad response code: 400 on request: POST https://myinstance.my.salesforce.com/services/data/v39.0/sobjects/sometable/thealphanumerickey?_HttpMethod=PATCH Accept: application/json Content-Type: application/json Authorization: Bearer ... {"Id":"thealphanumerickey","some_property__c":"some value"}

And in fact, as soon as I modify the request body, so that it does not contain the id, it works fine.

API version is 39.0

hf-kklein avatar Apr 19 '18 13:04 hf-kklein

We just had the same issue.

As a workaround, we set the id to null before calling the updateSObject and it works fine: mo.setId(null);

FabienTaillon avatar Jan 21 '19 10:01 FabienTaillon