Skip to content

Commit

Permalink
Create/Update Custom Object + improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GTFalcao committed Oct 28, 2024
1 parent 7770034 commit 87ccc95
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default {
result = createReq;
summary = createReq.result === true
? `Successfully created company '${this.name}'`
: `Error when creating company '${this.name}'`;
: `Error creating company '${this.name}'`;
}

$.export(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,68 @@
import gainsight_nxt from "../../gainsight_nxt.app.mjs";
import app from "../../gainsight_nxt.app.mjs";

export default {
key: "gainsight_nxt-create-or-update-custom-object",
name: "Create or Update Custom Object",
description: "Creates or updates a custom object in Gainsight NXT. [See the documentation]()",
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)",
version: "0.0.{{ts}}",
type: "action",
props: {
gainsight_nxt,
customObjectFields: {
app,
objectName: {
propDefinition: [
"gainsight_nxt",
"customObjectFields",
app,
"objectName",
],
},
fields: {
type: "string[]",
label: "Key Field(s)",
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.",
},
fieldValues: {
type: "object",
label: "Field Values",
description: "The record data to create or update, as key-value pairs.",
},
},
async run({ $ }) {
try {
const customObjectData = this.customObjectFields.map(JSON.parse);
const results = [];

for (const fields of customObjectData) {
const result = await this.gainsight_nxt.createOrUpdateCustomObject(fields);
results.push(result);
}
const { objectName } = this;
const data = {
records: [
this.fieldValues,
],
};

$.export("$summary", `Processed ${results.length} custom object(s) successfully.`);
return results;
} catch (error) {
$.export("$summary", `Failed to create or update custom objects: ${error.message}`);
throw error;
let summary = "";
let result;
try {
result = await this.app.updateCustomObject({
$,
objectName,
data,
params: {
keys: this.fields.join?.() ?? this.fields,
},
});
summary = result.result === true
? `Successfully updated custom object ${objectName}`
: `Error updating custom object ${objectName}`;
}
catch (err) {
result = await this.app.createCustomObject({
$,
objectName,
data,
});
summary = result.result === true
? `Successfully created custom object ${objectName}`
: `Error creating custom object ${objectName}`;
}

$.export(
"$summary",
summary,
);
return result;
},
};
45 changes: 36 additions & 9 deletions components/gainsight_nxt/gainsight_nxt.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ export default {
type: "app",
app: "gainsight_nxt",
propDefinitions: {
customObjectFields: {
type: "string[]",
label: "Custom Object Fields",
description: "An array of JSON strings representing custom object elements. Include a 'name' field to identify the custom object.",
},
personFields: {
type: "string[]",
label: "Person Fields",
description: "An array of JSON strings representing person information. Include an 'email' field to identify the person.",
objectName: {
type: "string",
label: "Custom Object",
description: "The name of the custom object.",
async options() {
const { data } = await this.listCustomObjects();
return data/*?.filter?.((obj) => obj.objectType === "CUSTOM")*/.map(( {
label, objectName,
}) => ({
label,
value: objectName,
}));
},
},
},
methods: {
Expand Down Expand Up @@ -61,5 +65,28 @@ export default {
...args,
});
},
async listCustomObjects() {
return this._makeRequest({
path: "/meta/services/objects/list",
});
},
async updateCustomObject({
objectName, ...args
}) {
return this._makeRequest({
path: `/data/objects/${objectName}`,
method: "PUT",
...args,
});
},
async createCustomObject({
objectName, ...args
}) {
return this._makeRequest({
path: `/data/objects/${objectName}`,
method: "POST",
...args,
});
},
},
};

0 comments on commit 87ccc95

Please sign in to comment.