-
Notifications
You must be signed in to change notification settings - Fork 5
/
compositeApiExample.js
74 lines (68 loc) · 1.91 KB
/
compositeApiExample.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Create multiple accounts and related contacts in a single api call */
let accts = [],
cntcs = [];
accts.push({ body: { Name: "Amazon", Industry: "E-commerce" }, referenceId: "Amazon" });
accts.push({ body: { Name: "Facebook", Industry: "Social Media" }, referenceId: "Facebook" });
accts.push({ body: { Name: "Google", Industry: "Search" }, referenceId: "Google" });
accts.push({ body: { Name: "Netflix", Industry: "Entertainment" }, referenceId: "Netflix" });
/* create one contact for each Account */
cntcs.push({
body: {
LastName: "Bezos",
FirstName: "Jeff",
Email: "[email protected]",
Title: "CEO of Amazon",
AccountId: "@{Amazon.id}"
},
referenceId: "Jeff"
});
cntcs.push({
body: {
LastName: "Zuckerberg",
FirstName: "Marc",
Email: "[email protected]",
Title: "CEO of Facebook",
AccountId: "@{Facebook.id}"
},
referenceId: "Marc"
});
cntcs.push({
body: {
LastName: "Pichai",
FirstName: "Sundar",
Email: "[email protected]",
Title: "CEO of Google",
AccountId: "@{Google.id}"
},
referenceId: "Sundar"
});
cntcs.push({
body: {
LastName: "Hastings",
FirstName: "Reed",
Email: "[email protected]",
Title: "CEO of Netflix",
AccountId: "@{Netflix.id}"
},
referenceId: "Reed"
});
/* create subrequests for Account and Contact by adding `method` and `url` properties */
accts = accts.map((a) => ({
...a,
method: "POST",
url: "/services/data/v51.0/sobjects/Account"
}));
cntcs = cntcs.map((c) => ({
...c,
method: "POST",
url: "/services/data/v51.0/sobjects/Contact"
}));
/*setup and make composite api request */
let compositeReq = { allOrNone: true, compositeRequest: [...accts, ...cntcs] };
let response = await sfapi(
"/composite/" /*path excluding base url*/,
"POST" /*method*/,
{} /* additional headers */,
JSON.stringify(compositeReq) /* request body */
);
alert(JSON.stringify(response));