1export default async function (event, context, logger) {
2 logger.info(`Invoking salesforcesdkjs function with payload ${JSON.stringify(event.data || {})}`);
3
4 // Extract properties from payload
5 const { name, accountNumber, industry, type, website } = event.data;
6
7 // Validate the payload params
8 if (!name) {
9 throw new Error(`Please provide account name`);
10 }
11
12 // Define a record using the RecordForCreate type and providing the Developer Name
13 const account = {
14 type: "Account",
15 fields: {
16 Name: `${name}-${Date.now()}`,
17 AccountNumber: accountNumber,
18 Industry: industry,
19 Type: type,
20 Website: website,
21 },
22 };
23
24 try {
25 // Insert the record using the SalesforceSDK DataApi and get the new Record Id from the result
26 const { id: recordId } = await context.org.dataApi.create(account);
27
28 // Query Accounts using the SalesforceSDK DataApi to verify that your new Account was created.
29 const soql = `SELECT Fields(STANDARD) FROM Account WHERE Id = '${recordId}'`;
30 const queryResults = await context.org.dataApi.query(soql);
31 return queryResults;
32 } catch (err) {
33 // Catch any DML errors and pass the throw an error with the message
34 const errorMessage = `Failed to insert record. Root Cause: ${err.message}`;
35 logger.error(errorMessage);
36 throw new Error(errorMessage);
37 }
38}