Skip to content

Commit 87ccc95

Browse files
committed
Create/Update Custom Object + improvements
1 parent 7770034 commit 87ccc95

File tree

3 files changed

+89
-29
lines changed

3 files changed

+89
-29
lines changed

components/gainsight_nxt/actions/create-or-update-company/create-or-update-company.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default {
133133
result = createReq;
134134
summary = createReq.result === true
135135
? `Successfully created company '${this.name}'`
136-
: `Error when creating company '${this.name}'`;
136+
: `Error creating company '${this.name}'`;
137137
}
138138

139139
$.export(
Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,68 @@
1-
import gainsight_nxt from "../../gainsight_nxt.app.mjs";
1+
import app from "../../gainsight_nxt.app.mjs";
22

33
export default {
44
key: "gainsight_nxt-create-or-update-custom-object",
55
name: "Create or Update Custom Object",
6-
description: "Creates or updates a custom object in Gainsight NXT. [See the documentation]()",
6+
description: "Create or update a custom object record. [See the documentation](https://support.gainsight.com/gainsight_nxt/API_and_Developer_Docs/Custom_Object_API/Gainsight_Custom_Object_API_Documentation#Insert_API)",
77
version: "0.0.{{ts}}",
88
type: "action",
99
props: {
10-
gainsight_nxt,
11-
customObjectFields: {
10+
app,
11+
objectName: {
1212
propDefinition: [
13-
"gainsight_nxt",
14-
"customObjectFields",
13+
app,
14+
"objectName",
1515
],
1616
},
17+
fields: {
18+
type: "string[]",
19+
label: "Key Field(s)",
20+
description: "The field(s) which identify this object (max 3), e.g. `fieldName1`. If a record with the same key field(s) exists, it will be updated, otherwise a new one will be created.",
21+
},
22+
fieldValues: {
23+
type: "object",
24+
label: "Field Values",
25+
description: "The record data to create or update, as key-value pairs.",
26+
},
1727
},
1828
async run({ $ }) {
19-
try {
20-
const customObjectData = this.customObjectFields.map(JSON.parse);
21-
const results = [];
22-
23-
for (const fields of customObjectData) {
24-
const result = await this.gainsight_nxt.createOrUpdateCustomObject(fields);
25-
results.push(result);
26-
}
29+
const { objectName } = this;
30+
const data = {
31+
records: [
32+
this.fieldValues,
33+
],
34+
};
2735

28-
$.export("$summary", `Processed ${results.length} custom object(s) successfully.`);
29-
return results;
30-
} catch (error) {
31-
$.export("$summary", `Failed to create or update custom objects: ${error.message}`);
32-
throw error;
36+
let summary = "";
37+
let result;
38+
try {
39+
result = await this.app.updateCustomObject({
40+
$,
41+
objectName,
42+
data,
43+
params: {
44+
keys: this.fields.join?.() ?? this.fields,
45+
},
46+
});
47+
summary = result.result === true
48+
? `Successfully updated custom object ${objectName}`
49+
: `Error updating custom object ${objectName}`;
3350
}
51+
catch (err) {
52+
result = await this.app.createCustomObject({
53+
$,
54+
objectName,
55+
data,
56+
});
57+
summary = result.result === true
58+
? `Successfully created custom object ${objectName}`
59+
: `Error creating custom object ${objectName}`;
60+
}
61+
62+
$.export(
63+
"$summary",
64+
summary,
65+
);
66+
return result;
3467
},
3568
};

components/gainsight_nxt/gainsight_nxt.app.mjs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ export default {
44
type: "app",
55
app: "gainsight_nxt",
66
propDefinitions: {
7-
customObjectFields: {
8-
type: "string[]",
9-
label: "Custom Object Fields",
10-
description: "An array of JSON strings representing custom object elements. Include a 'name' field to identify the custom object.",
11-
},
12-
personFields: {
13-
type: "string[]",
14-
label: "Person Fields",
15-
description: "An array of JSON strings representing person information. Include an 'email' field to identify the person.",
7+
objectName: {
8+
type: "string",
9+
label: "Custom Object",
10+
description: "The name of the custom object.",
11+
async options() {
12+
const { data } = await this.listCustomObjects();
13+
return data/*?.filter?.((obj) => obj.objectType === "CUSTOM")*/.map(( {
14+
label, objectName,
15+
}) => ({
16+
label,
17+
value: objectName,
18+
}));
19+
},
1620
},
1721
},
1822
methods: {
@@ -61,5 +65,28 @@ export default {
6165
...args,
6266
});
6367
},
68+
async listCustomObjects() {
69+
return this._makeRequest({
70+
path: "/meta/services/objects/list",
71+
});
72+
},
73+
async updateCustomObject({
74+
objectName, ...args
75+
}) {
76+
return this._makeRequest({
77+
path: `/data/objects/${objectName}`,
78+
method: "PUT",
79+
...args,
80+
});
81+
},
82+
async createCustomObject({
83+
objectName, ...args
84+
}) {
85+
return this._makeRequest({
86+
path: `/data/objects/${objectName}`,
87+
method: "POST",
88+
...args,
89+
});
90+
},
6491
},
6592
};

0 commit comments

Comments
 (0)