forked from rudderlabs/rudder-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Handle Salesforce Bulk external IDs during transform #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
etsenake
merged 9 commits into
feat/salesforce-bulk-upload
from
codex/implement-salesforce-async-job-helper
Oct 18, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
674ab55
chore: using go-kit maxprocs (#6433)
fracasula f714b32
chore: use org-wide docker registry mirror configuration (#6444)
atzoum e3f4c49
Merge branch 'master' into feat/salesforce-bulk-upload
etsenake 24b6a98
Handle Salesforce Bulk external IDs during transform
etsenake 8346b3d
Respect default operation when external IDs lack ids
etsenake e64be61
Improve Salesforce Bulk externalId selection
etsenake d6315e4
Split Salesforce Bulk batches by object
etsenake 33c4f9e
Add transform test for multi-type externalId arrays
etsenake e890d83
Assert full message cloning in Salesforce Bulk transform tests
etsenake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
router/batchrouter/asyncdestinationmanager/salesforce-bulk/prepare.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package salesforcebulk | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/tidwall/gjson" | ||
|
|
||
| "github.com/rudderlabs/rudder-go-kit/jsonrs" | ||
| "github.com/rudderlabs/rudder-server/router/batchrouter/asyncdestinationmanager/common" | ||
| ) | ||
|
|
||
| func prepareAsyncJob(eventPayload []byte, jobID int64, defaultOperation string) (*common.AsyncJob, error) { | ||
| message, err := extractMessage(eventPayload) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| clonedMessage := cloneMessage(message) | ||
| metadata := map[string]interface{}{ | ||
| "job_id": jobID, | ||
| } | ||
|
|
||
| normalizedExternalIDs := collectNormalizedExternalIDs(message) | ||
| if len(normalizedExternalIDs) > 0 { | ||
| metadata["externalId"] = normalizedExternalIDs | ||
| } | ||
|
|
||
| operation := determineOperation(normalizedExternalIDs, defaultOperation) | ||
| clonedMessage["rudderOperation"] = operation | ||
|
|
||
| if field, value, ok := identifierColumn(normalizedExternalIDs); ok { | ||
| clonedMessage[field] = value | ||
| } | ||
|
|
||
| return &common.AsyncJob{ | ||
| Message: clonedMessage, | ||
| Metadata: metadata, | ||
| }, nil | ||
| } | ||
|
|
||
| func extractMessage(eventPayload []byte) (map[string]interface{}, error) { | ||
| body := gjson.GetBytes(eventPayload, "body.JSON") | ||
| if !body.Exists() || len(body.Raw) == 0 { | ||
| return map[string]interface{}{}, nil | ||
| } | ||
|
|
||
| var message map[string]interface{} | ||
| if err := jsonrs.Unmarshal([]byte(body.Raw), &message); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if message == nil { | ||
| message = map[string]interface{}{} | ||
| } | ||
|
|
||
| return message, nil | ||
| } | ||
|
|
||
| func cloneMessage(message map[string]interface{}) map[string]interface{} { | ||
| cloned := make(map[string]interface{}, len(message)) | ||
| for key, value := range message { | ||
| cloned[key] = value | ||
| } | ||
| return cloned | ||
| } | ||
|
|
||
| func collectNormalizedExternalIDs(message map[string]interface{}) []map[string]string { | ||
| var normalized []map[string]string | ||
|
|
||
| if contextRaw, ok := message["context"].(map[string]interface{}); ok { | ||
| normalized = append(normalized, normalizeExternalIDArray(contextRaw["externalId"])...) | ||
| } | ||
|
|
||
| normalized = append(normalized, normalizeExternalIDArray(message["externalId"])...) | ||
|
|
||
|
etsenake marked this conversation as resolved.
|
||
| return normalized | ||
| } | ||
|
|
||
| func normalizeExternalIDArray(raw interface{}) []map[string]string { | ||
| array, ok := raw.([]interface{}) | ||
| if !ok || len(array) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| result := make([]map[string]string, 0, len(array)) | ||
| for _, entry := range array { | ||
| if normalized, ok := normalizeExternalIDEntry(entry); ok { | ||
| result = append(result, normalized) | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| func normalizeExternalIDEntry(raw interface{}) (map[string]string, bool) { | ||
| entry, ok := raw.(map[string]interface{}) | ||
| if !ok { | ||
| return nil, false | ||
| } | ||
|
|
||
| normalized := map[string]string{ | ||
| "type": "", | ||
| "id": "", | ||
| "identifierType": "", | ||
| } | ||
|
|
||
| if typeVal, ok := entry["type"]; ok { | ||
| normalized["type"] = fmt.Sprint(typeVal) | ||
| } | ||
|
|
||
| if idVal, ok := entry["id"]; ok { | ||
| normalized["id"] = fmt.Sprint(idVal) | ||
| } | ||
|
|
||
| if identifierVal, ok := entry["identifierType"]; ok { | ||
| normalized["identifierType"] = fmt.Sprint(identifierVal) | ||
| } | ||
|
|
||
| if normalized["identifierType"] == "" && normalized["id"] != "" { | ||
| normalized["identifierType"] = "Id" | ||
| } | ||
|
|
||
| return normalized, true | ||
| } | ||
|
|
||
| func determineOperation(externalIDs []map[string]string, defaultOperation string) string { | ||
| if defaultOperation == "" { | ||
| defaultOperation = "insert" | ||
| } | ||
|
|
||
| if len(externalIDs) == 0 { | ||
| return defaultOperation | ||
| } | ||
|
|
||
| for _, externalID := range externalIDs { | ||
| if externalID["id"] != "" { | ||
| return "upsert" | ||
| } | ||
| } | ||
|
|
||
| for _, externalID := range externalIDs { | ||
| if externalID["identifierType"] != "" { | ||
| return "upsert" | ||
| } | ||
|
etsenake marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return defaultOperation | ||
| } | ||
|
|
||
| func identifierColumn(externalIDs []map[string]string) (string, string, bool) { | ||
| for _, externalID := range externalIDs { | ||
| if externalID["id"] == "" { | ||
| continue | ||
| } | ||
|
|
||
| field := externalID["identifierType"] | ||
| if field == "" { | ||
| field = "Id" | ||
| } | ||
|
|
||
| return field, externalID["id"], true | ||
| } | ||
|
|
||
| return "", "", false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new transform helper computes
operation := determineOperation(...)and unconditionally assigns it toclonedMessage["rudderOperation"]. This overwrites anyrudderOperationalready present in the incoming payload. Previously the transformer forwarded the payload as‑is, so callers could explicitly sendrudderOperation: "delete"(or any other supported value) and have it respected. With this change, a message that already specifies a non‑default operation will now be forcibly changed—e.g., a delete request containing anexternalIdwill be transformed into anupsertjob, causing records to be inserted or updated instead of deleted. Unless the intention is to ignore all user‑provided operations, this introduces incorrect behavior for any event that explicitly setsrudderOperation.Useful? React with 👍 / 👎.