This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathindex.js
55 lines (50 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Receives a payload containing account details, and creates the record.
* It then uses a SOQL query to return the newly created Account.
*
* The exported method is the entry point for your code when the function is invoked.
*
* Following parameters are pre-configured and provided to your function on execution:
* @param event: represents the data associated with the occurrence of an event, and
* supporting metadata about the source of that occurrence.
* @param context: represents the connection to Functions and your Salesforce org.
* @param logger: logging handler used to capture application logs and trace specifically
* to a given execution of a function.
*/
export default async function (event, context, logger) {
logger.info(
`Invoking salesforcesdkjs Function with payload ${JSON.stringify(
event.data || {}
)}`
);
// Extract Properties from Payload
const { name, accountNumber, industry, type, website } = event.data;
// Validate the payload params
if (!name) {
throw new Error(`Please provide account name`);
}
// Define a record using the RecordForCreate type and providing the Developer Name
const account = {
type: "Account",
fields: {
Name: `${name}-${Date.now()}`,
AccountNumber: accountNumber,
Industry: industry,
Type: type,
Website: website
}
};
try {
// Insert the record using the SalesforceSDK DataApi and get the new Record Id from the result
const { id: recordId } = await context.org.dataApi.create(account);
// Query Accounts using the SalesforceSDK DataApi to verify that our new Account was created.
const soql = `SELECT Fields(STANDARD) FROM Account WHERE Id = '${recordId}'`;
const queryResults = await context.org.dataApi.query(soql);
return queryResults;
} catch (err) {
// Catch any DML errors and pass the throw an error with the message
const errorMessage = `Failed to insert record. Root Cause: ${err.message}`;
logger.error(errorMessage);
throw new Error(errorMessage);
}
}