From bf9ecb07ad1d593a8c521a8f76c43398fc9e7c06 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Wed, 27 May 2020 20:18:59 +0300 Subject: [PATCH 1/3] Fixed update during Upsert() Currently, v48.0 Salesforce API returns 200 on a successful update. Before this fix, Upsert() was returning "upsert response err: 200 200 OK" error during successful update. Docs - https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm --- sobject/dml.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sobject/dml.go b/sobject/dml.go index 9bb7c3b..07ff53b 100644 --- a/sobject/dml.go +++ b/sobject/dml.go @@ -262,7 +262,7 @@ func (d *dml) upsertResponse(request *http.Request) (UpsertValue, error) { var value UpsertValue switch response.StatusCode { - case http.StatusCreated: + case http.StatusCreated, http.StatusOK: defer response.Body.Close() isInsert = true err = decoder.Decode(&value) From bb98a592876d8fbcf7a524bd2edd8e7069753d29 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Wed, 27 May 2020 20:34:07 +0300 Subject: [PATCH 2/3] added test --- sobject/dml_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/sobject/dml_test.go b/sobject/dml_test.go index 650be22..252e2fc 100644 --- a/sobject/dml_test.go +++ b/sobject/dml_test.go @@ -528,6 +528,48 @@ func Test_dml_Upsert(t *testing.T) { }, wantErr: false, }, + { + name: "Upsert Response Updated Passing", + fields: fields{ + session: &mockSessionFormatter{ + url: "https://test.salesforce.com", + client: mockHTTPClient(func(req *http.Request) *http.Response { + resp := ` + { + "id" : "001D000000IqhSLIAZ", + "errors" : [ ], + "success" : true + }` + + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(strings.NewReader(resp)), + Header: make(http.Header), + } + }), + }, + }, + args: args{ + upserter: &mockUpsert{ + sobject: "Account", + id: "12345", + external: "external__c", + fields: map[string]interface{}{ + "Name": "Some Test Name", + "Active": false, + }, + }, + }, + want: UpsertValue{ + Inserted: false, + InsertValue: InsertValue{ + Success: true, + Errors: make([]sfdc.Error, 0), + ID: "001D000000IqhSLIAZ", + }, + }, + wantErr: false, + }, { name: "Upsert Response Passing", fields: fields{ From 0fcd133f22066b12c2eeb63d5b021355c11af401 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Wed, 27 May 2020 20:40:21 +0300 Subject: [PATCH 3/3] fixed Inserted field --- sobject/dml.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sobject/dml.go b/sobject/dml.go index 07ff53b..dbbaad2 100644 --- a/sobject/dml.go +++ b/sobject/dml.go @@ -258,19 +258,17 @@ func (d *dml) upsertResponse(request *http.Request) (UpsertValue, error) { decoder := json.NewDecoder(response.Body) - var isInsert bool var value UpsertValue switch response.StatusCode { case http.StatusCreated, http.StatusOK: defer response.Body.Close() - isInsert = true err = decoder.Decode(&value) if err != nil { return UpsertValue{}, err } case http.StatusNoContent: - isInsert = false + break // out of the switch default: defer response.Body.Close() var upsetErrs []sfdc.Error @@ -284,7 +282,9 @@ func (d *dml) upsertResponse(request *http.Request) (UpsertValue, error) { return UpsertValue{}, errMsg } - value.Inserted = isInsert + if response.StatusCode == http.StatusCreated { + value.Inserted = true + } return value, nil }