|
| 1 | +import { |
| 2 | + convertFieldsToProps, getAdditionalFields, |
| 3 | +} from "../../common/props-utils.mjs"; |
| 4 | +import salesforce from "../../salesforce_rest_api.app.mjs"; |
| 5 | +import { additionalFields } from "../common/base-create-update.mjs"; |
| 6 | + |
| 7 | +export default { |
| 8 | + key: "salesforce_rest_api-upsert-record", |
| 9 | + name: "Upsert Record", |
| 10 | + 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)", |
| 11 | + version: "0.0.1", |
| 12 | + type: "action", |
| 13 | + props: { |
| 14 | + salesforce, |
| 15 | + objectType: { |
| 16 | + propDefinition: [ |
| 17 | + salesforce, |
| 18 | + "objectType", |
| 19 | + ], |
| 20 | + description: "The type of object to create a record of", |
| 21 | + reloadProps: true, |
| 22 | + }, |
| 23 | + externalIdFieldName: { |
| 24 | + type: "string", |
| 25 | + label: "External ID Field", |
| 26 | + description: "The name of the field used as the external ID. If a record exists with this field having the value specified in `External ID Value`, it will be updated, otherwise a new record will be created.", |
| 27 | + }, |
| 28 | + externalIdValue: { |
| 29 | + type: "string", |
| 30 | + label: "External ID Value", |
| 31 | + description: "The value of the external ID field specified above. If a record exists with this value, it will be updated, otherwise a new record will be created.", |
| 32 | + } |
| 33 | + }, |
| 34 | + methods: { |
| 35 | + getAdditionalFields, |
| 36 | + convertFieldsToProps, |
| 37 | + }, |
| 38 | + async additionalProps() { |
| 39 | + const { objectType } = this; |
| 40 | + const fields = await this.salesforce.getFieldsForObjectType(objectType); |
| 41 | + |
| 42 | + const requiredFields = fields.filter((field) => { |
| 43 | + return field.createable && field.updateable && !field.nillable && !field.defaultedOnCreate; |
| 44 | + }); |
| 45 | + |
| 46 | + const requiredFieldProps = this.convertFieldsToProps(requiredFields); |
| 47 | + |
| 48 | + return { |
| 49 | + docsInfo: { |
| 50 | + type: "alert", |
| 51 | + alertType: "info", |
| 52 | + 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.`, |
| 53 | + }, |
| 54 | + ...requiredFieldProps, |
| 55 | + additionalFields, |
| 56 | + }; |
| 57 | + }, |
| 58 | + async run({ $ }) { |
| 59 | + /* eslint-disable no-unused-vars */ |
| 60 | + const { |
| 61 | + salesforce, |
| 62 | + objectType, |
| 63 | + getAdditionalFields: getData, |
| 64 | + convertFieldsToProps, |
| 65 | + docsInfo, |
| 66 | + additionalFields, |
| 67 | + externalIdFieldName, |
| 68 | + externalIdValue, |
| 69 | + ...data |
| 70 | + } = this; |
| 71 | + /* eslint-enable no-unused-vars */ |
| 72 | + const response = await salesforce.upsertRecord(objectType, { |
| 73 | + $, |
| 74 | + externalIdFieldName, |
| 75 | + externalIdValue, |
| 76 | + data: { |
| 77 | + ...data, |
| 78 | + ...getData(), |
| 79 | + }, |
| 80 | + }); |
| 81 | + $.export("$summary", `Successfully ${response.created ? 'created' : 'updated'} ${objectType} record (ID: ${response.id})`); |
| 82 | + return response; |
| 83 | + }, |
| 84 | +}; |
0 commit comments