Skip to content

Commit b00986f

Browse files
committed
Adding upsert record action
1 parent 184654d commit b00986f

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
};

components/salesforce_rest_api/salesforce_rest_api.app.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@ export default {
292292
data,
293293
});
294294
},
295+
async upsertRecord(sobjectName, {
296+
$, data, externalIdFieldName, externalIdValue
297+
}) {
298+
const url = `${this._sObjectTypeApiUrl(sobjectName)}/${externalIdFieldName}/${externalIdValue}`;
299+
return this._makeRequest({
300+
$,
301+
url,
302+
method: "PATCH",
303+
data,
304+
});
305+
},
295306
async query({
296307
$, query,
297308
}) {

0 commit comments

Comments
 (0)