-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Salesforce Upsert Record #14862
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
Merged
Salesforce Upsert Record #14862
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b00986f
Adding upsert record action
GTFalcao 68ddc67
Upsert Record update with external id field
GTFalcao 4561487
Adjustments
GTFalcao 24a881a
Adjustments + ESLint
GTFalcao 3eb7011
Adding alert prop with external id info
GTFalcao f361042
component method adjustment
GTFalcao bc90a77
ESLint fix
GTFalcao dc202e6
Merge branch 'master' into 14812-salesforce-new-action
GTFalcao 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
114 changes: 114 additions & 0 deletions
114
components/salesforce_rest_api/actions/upsert-record/upsert-record.mjs
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,114 @@ | ||
| import { | ||
| convertFieldsToProps, getAdditionalFields, | ||
| } from "../../common/props-utils.mjs"; | ||
| import salesforce from "../../salesforce_rest_api.app.mjs"; | ||
| import { additionalFields } from "../common/base-create-update.mjs"; | ||
|
|
||
| export default { | ||
| key: "salesforce_rest_api-upsert-record", | ||
| name: "Upsert Record", | ||
| description: "Create or update a record of a given object. [See the documentation](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| salesforce, | ||
| objectType: { | ||
| propDefinition: [ | ||
| salesforce, | ||
| "objectType", | ||
| ], | ||
| description: "The type of object to create a record of", | ||
| reloadProps: true, | ||
| }, | ||
| }, | ||
| methods: { | ||
| getAdditionalFields, | ||
| convertFieldsToProps, | ||
| async upsertRecord(sobjectName, { | ||
| externalIdFieldName, externalIdValue, ...args | ||
| }) { | ||
| const url = `${this._sObjectTypeApiUrl(sobjectName)}/${externalIdFieldName}/${externalIdValue}`; | ||
| return this.salesforce._makeRequest({ | ||
| url, | ||
| method: "PATCH", | ||
| ...args, | ||
| }); | ||
| }, | ||
| }, | ||
| async additionalProps() { | ||
| const { objectType } = this; | ||
| const fields = await this.salesforce.getFieldsForObjectType(objectType); | ||
|
|
||
| const requiredFields = fields.filter((field) => { | ||
| return field.createable && field.updateable && !field.nillable && !field.defaultedOnCreate; | ||
| }); | ||
|
|
||
| const externalIdFieldOptions = fields.filter((field) => field.externalId).map(({ | ||
| label, name, | ||
| }) => ({ | ||
| label, | ||
| value: name, | ||
| })); | ||
|
|
||
| const requiredFieldProps = this.convertFieldsToProps(requiredFields); | ||
|
|
||
| return { | ||
| docsInfo: { | ||
| type: "alert", | ||
| alertType: "info", | ||
| content: `[See the documentation](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_${objectType.toLowerCase()}.htm) for information on all available fields.`, | ||
| }, | ||
| externalIdFieldName: { | ||
| type: "string", | ||
| label: "External ID Field", | ||
| description: "The field to use as the external ID to identify the record.", | ||
| options: externalIdFieldOptions, | ||
| }, | ||
| externalIdValue: { | ||
| type: "string", | ||
| label: "External ID Value", | ||
| description: "The value of the external ID field selected above. If a record with this value exists, it will be updated, otherwise a new one will be created.", | ||
| }, | ||
| updateOnly: { | ||
| type: "boolean", | ||
| label: "Update Only", | ||
| description: "If enabled, the action will only update an existing record, but not create one.", | ||
| optional: true, | ||
| }, | ||
| ...requiredFieldProps, | ||
| additionalFields, | ||
| }; | ||
| }, | ||
| async run({ $ }) { | ||
| /* eslint-disable no-unused-vars */ | ||
| const { | ||
| salesforce, | ||
| objectType, | ||
| getAdditionalFields: getData, | ||
| convertFieldsToProps, | ||
| docsInfo, | ||
| additionalFields, | ||
| externalIdFieldName, | ||
| externalIdValue, | ||
| updateOnly, | ||
| ...data | ||
| } = this; | ||
| /* eslint-enable no-unused-vars */ | ||
| const response = await this.upsertRecord(objectType, { | ||
| $, | ||
| externalIdFieldName, | ||
| externalIdValue, | ||
| params: { | ||
| updateOnly, | ||
| }, | ||
| data: { | ||
| ...data, | ||
| ...getData(), | ||
| }, | ||
| }); | ||
| $.export("$summary", `Successfully ${response.created | ||
| ? "created" | ||
| : "updated"} ${objectType} record (ID: ${response.id})`); | ||
| return response; | ||
| }, | ||
| }; | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.