anthonyreilly / NetCoreForce

Salesforce REST API toolkit for .NET Standard and .NET Core
MIT License
111 stars 63 forks source link

JsonClient does not handle 300 response during Patch request #8

Closed ts46235 closed 4 years ago

ts46235 commented 5 years ago

When there are 2 or more records in the destination table that contain the same external reference Id as the record being sent in the Patch operation, Salesforce returns a 300 response with a body containing a Json array containing all the duplicate matched records. So if record x and y have the same ext ref id and you try to Patch for x then you will receive the record info for y.

see documentation: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_upsert.htm

If the value is not unique, the REST API returns a 300 response with the list of matching records.

sample response:

HTTP/1.1 300 Multiple Choices
Date: Wed, 14 Aug 2019 22:28:26 GMT
Strict-Transport-Security: max-age=31536002; includeSubDomains
Public-Key-Pins-Report-Only: pin-sha256="9n0izTnSRF+W4W4JTq51qwSXkWhQB8duS2bxVLfzXsY="; pin-sha256="5kJvNEMw0Kjrsou7eXY5HZdvyCS13BbA0VJG1RSP91w="; pin-sha256="njN4rRG+22dNXAi+yb8e3UMypgzPUpalv4+foULwl1g="; max-age=86400; includeSubDomains; report-uri="https://a.forcesslreports.com/hpkp-report/00D1800000095gPm";
Expect-CT: max-age=86400; report-uri="https://a.forcesslreports.com/Expect-CT-report/00D1800000095gPm";
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Robots-Tag: none
Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private
Sforce-Limit-Info: api-usage=1990/5000000
Content-Type: application/json;charset=UTF-8
Content-Length: 211

["/services/data/v44.0/sobjects/Integration_DTO__c/a0w18000001OnMVAA0","/services/data/v44.0/sobjects/Integration_DTO__c/a0w18000001OpzFAAS","/services/data/v44.0/sobjects/Integration_DTO__c/a0w18000001PHjGAAW"]

JsonClient is trying to Deserialize the response for all non-success statuses as a List of ErrorResponses:

List<ErrorResponse> errors = (List<ErrorResponse>) null;
        try
        {
          errors = JsonConvert.DeserializeObject<List<ErrorResponse>>(str);
          ...

Of course when it tries to do so for this kind of content it blows up:

Newtonsoft.Json.JsonSerializationException: 'Error converting value "/services/data/v44.0/sobjects/Integration_DTO__c/a0w6C000000hqEmQAI" to type 'NetCoreForce.Client.Models.ErrorResponse'. Path '[0]', line 1, position 70.'

inner exception ArgumentException: Could not cast or convert from System.String to NetCoreForce.Client.Models.ErrorResponse.

System.FormatException: 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list.'

anthonyreilly commented 5 years ago

Thanks for the details, I'll get this patched.

anthonyreilly commented 4 years ago

Fixed in v2.6.0 745c573f18c9ba1a1605ace3e61f87199c4bae91