Skip to content

Commit eb75f76

Browse files
authored
Salesforce Upsert Record (#14862)
* Adding upsert record action * Upsert Record update with external id field * Adjustments * Adjustments + ESLint * Adding alert prop with external id info * component method adjustment * ESLint fix
1 parent 04fd204 commit eb75f76

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
},
24+
methods: {
25+
getAdditionalFields,
26+
convertFieldsToProps,
27+
async upsertRecord(sobjectName, {
28+
externalIdFieldName, externalIdValue, ...args
29+
}) {
30+
const url = `${this.salesforce._sObjectTypeApiUrl(sobjectName)}/${externalIdFieldName}/${externalIdValue}`;
31+
return this.salesforce._makeRequest({
32+
url,
33+
method: "PATCH",
34+
...args,
35+
});
36+
},
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 externalIdFieldOptions = fields.filter((field) => field.externalId).map(({
47+
label, name,
48+
}) => ({
49+
label,
50+
value: name,
51+
}));
52+
53+
const requiredFieldProps = this.convertFieldsToProps(requiredFields);
54+
55+
return {
56+
docsInfo: {
57+
type: "alert",
58+
alertType: "info",
59+
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.`,
60+
},
61+
externalIdFieldName: {
62+
type: "string",
63+
label: "External ID Field",
64+
description: "The field to use as the external ID to identify the record.",
65+
options: externalIdFieldOptions,
66+
},
67+
docsInfoExtId: {
68+
type: "alert",
69+
alertType: "info",
70+
content: "If you don't see any fields in the above list, you probably need to create one in Salesforce's Object Manager. Only a field marked as an external id field can be used to identify a record.",
71+
},
72+
externalIdValue: {
73+
type: "string",
74+
label: "External ID Value",
75+
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.",
76+
},
77+
updateOnly: {
78+
type: "boolean",
79+
label: "Update Only",
80+
description: "If enabled, the action will only update an existing record, but not create one.",
81+
optional: true,
82+
},
83+
...requiredFieldProps,
84+
additionalFields,
85+
};
86+
},
87+
async run({ $ }) {
88+
/* eslint-disable no-unused-vars */
89+
const {
90+
salesforce,
91+
objectType,
92+
getAdditionalFields: getData,
93+
convertFieldsToProps,
94+
docsInfo,
95+
docsInfoExtId,
96+
additionalFields,
97+
externalIdFieldName,
98+
externalIdValue,
99+
updateOnly,
100+
...data
101+
} = this;
102+
/* eslint-enable no-unused-vars */
103+
const response = await this.upsertRecord(objectType, {
104+
$,
105+
externalIdFieldName,
106+
externalIdValue,
107+
params: {
108+
updateOnly,
109+
},
110+
data: {
111+
...data,
112+
...getData(),
113+
},
114+
});
115+
$.export("$summary", `Successfully ${response.created
116+
? "created"
117+
: "updated"} ${objectType} record (ID: ${response.id})`);
118+
return response;
119+
},
120+
};

components/salesforce_rest_api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/salesforce_rest_api",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"description": "Pipedream Salesforce (REST API) Components",
55
"main": "salesforce_rest_api.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)