-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_customers.js
More file actions
68 lines (61 loc) · 2.22 KB
/
Copy pathget_customers.js
File metadata and controls
68 lines (61 loc) · 2.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { NetsuiteApiClient } from "netsuite-api-client";
export default {
name: "Get Netsuite Customers",
description: "This action gets all netsuite customers from a list of externalids.",
key: "netsuite_get_customers",
version: "0.1.0",
type: "action",
props: {
config: {
type: "string",
label: "NetSuite Config",
description: `NetSuite Configuration JSON object. In format, {"consumer_key":"...","consumer_secret_key":"...","token":"...","token_secret":"...","realm":"..."}`,
secret: true,
},
externalids: {
type: "string[]",
label: "List of External Ids",
description: "You may need to run `data.map((d) => this.trimPrefix(d.Customer, \"INT\"))` to get it into the correct format.",
},
fields: {
type: "string[]",
label: "List of Fields to Retrieve",
description: "List of fields to retrieve from customer table",
optional: true,
}
},
async run({ steps, $ }) {
try {
// Dedupe external ids
const uniqueExternalIDs = [...new Set(this.externalids)]
const config = JSON.parse(this.config)
console.log("Realm:", config.realm)
const client = new NetsuiteApiClient(config);
const fields = this.fields || ["*"]
const q = `SELECT ${fields.join(",")} FROM customer WHERE externalid IN ('${uniqueExternalIDs.join("','")}')`
console.log(q)
let limit = 1000
let offset = 0
let response = {}
let items = []
do {
response = await client.query(q, limit, offset)
items = items.concat(response.items)
offset += limit
} while (response.hasMore)
$.export("customers", items)
if (items.length != uniqueExternalIDs.length) {
const notFound = uniqueExternalIDs.filter((id) => !items.some((item) => item.externalid == id))
$.export("error", `The following externalids were not found: ${notFound.join(",")}`)
$.export("notFoundExternalIds", notFound)
}
} catch (error) {
console.error("NetSuite API Error:", error.response?.data || error.message);
throw new Error(
`Failed to execute SuiteQL query: ${
error.response?.data?.detail || error.message
}`
);
}
},
};