Custom Object Function
The example shows you how to:
- Create a class that extends
AbstractFunctionand implementsIFunction. - Implement the
writeXmlfunction to generate XML for the API function for the request. - Call execute on a client and pass in the API function instance to send the request to the gateway.
Prerequisites
- You have downloaded or cloned the Sage Intacct SDK for Node.js examples.
- You successfully ran the getting started example—in particular, you set up the
credentials.inifile required bybootstrap.js. - The company you are using has Platform Services enabled and an object definition for your custom object (the example uses
test_object). - The user you are using has add, edit, delete, list, and view permissions for the custom object.
Set up
-
Open
TestObject.jsand note howAbstractTestObjectextendsIntacct/SDK/Functions/AbstractFunction.AbstractFunctionis a helper class provided mainly to generate random control IDs.AbstractFunctionimplementsIntacct/SDK/Functions/IFunction, and any class whose instances will be executed by a client must implementIFunction. If usingexecuteBatch, the parameter must be anIFunction[]array. -
In
TestObject.jsnote howTestObjectCreateextendsAbstractTestObject.The
IFunctioninterface requires that the class implement thewriteXml(xml)function. This implementation is where you provide the XML that will be processed at the gateway endpoint. -
Back in the
AbstractTestObjectclass, replacetest_objectwith the integration name for your custom object.this.integrationName = "test_object";
Run the example
-
Run the
custom-object-function.jsfile:node custom-object-function.js -
Observe the terminal output, which should look similar to this:
Created record ID 10153Note the record ID, which will be used to delete the object later.
-
Open the generated
logs/intacct.logfile in a text editor and review the entries.The file provides info and debug entries. The SDK provides a debug entry for each HTTP request/response (with the Sage Intacct endpoint).
Note that the log file was created by the logger set in the client config.
Extra credit
Get the record by ID
-
Update
custom-object-function.jsto execute aReadfunction using the record ID returned byTestObjectCreateas the key. For example:let read = new IA.Functions.Common.Read(); read.objectName = "test_object"; read.keys = [ 10174, ]; const readResponse = await client.execute(read); const readResult = readResponse.getResult(); const recordNo = parseInt(readResult.data[0]["id"]); console.log("Read record ID " + recordNo.toString());
Delete the record
-
Create a
TestObjectDeleteclass inTestObject.jsand execute an instance of it that uses the same record ID returned in theTestObjectCreateresult.If you need help, see the example class.
class TestObjectDelete extends AbstractTestObject { constructor(recordId) { super(); this.id = recordId; } writeXml(xml) { xml.writeStartElement("function"); xml.writeAttribute("controlid", this.controlId, true); xml.writeStartElement("delete"); xml.writeElement("object", this.integrationName, true); xml.writeElement("keys", this.id, true); xml.writeEndElement(); // delete xml.writeEndElement(); // function } } exports.TestObjectDelete = TestObjectDelete;
What’s next?
For more complex examples, you can use the functions in the SDK as models. For example, AbstractBill extends AbstractFunction, then BillCreate and BillDelete extend AbstractBill and write the XML for creating or deleting bills.