-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataverse_create_update_delete.js
More file actions
54 lines (47 loc) · 1.48 KB
/
dataverse_create_update_delete.js
File metadata and controls
54 lines (47 loc) · 1.48 KB
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
const environmentUrl = "https://yourorg.crm.dynamics.com";
const accessToken = "YOUR_ACCESS_TOKEN";
const headers = {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json",
"Content-Type": "application/json",
"OData-Version": "4.0",
"OData-MaxVersion": "4.0",
};
async function ensureSuccess(response) {
if (!response.ok) {
throw new Error(`Request failed: ${response.status} ${response.statusText}`);
}
}
async function run() {
const createResponse = await fetch(`${environmentUrl}/api/data/v9.2/accounts`, {
method: "POST",
headers,
body: JSON.stringify({
name: "Contoso Example Account",
telephone1: "03 9000 0000",
}),
});
await ensureSuccess(createResponse);
const entityId = createResponse.headers.get("OData-EntityId") || "";
const accountId = entityId.split("(").pop().replace(")", "");
console.log(`Created account: ${accountId}`);
const updateResponse = await fetch(`${environmentUrl}/api/data/v9.2/accounts(${accountId})`, {
method: "PATCH",
headers,
body: JSON.stringify({
telephone1: "03 9555 1234",
}),
});
await ensureSuccess(updateResponse);
console.log(`Updated account: ${accountId}`);
const deleteResponse = await fetch(`${environmentUrl}/api/data/v9.2/accounts(${accountId})`, {
method: "DELETE",
headers,
});
await ensureSuccess(deleteResponse);
console.log(`Deleted account: ${accountId}`);
}
run().catch((error) => {
console.error(error);
process.exitCode = 1;
});