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 .NET examples.
- You successfully ran the getting started example—in particular, you set up the
credentials.inifile required byBootstrap.cs. - 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
AbstractTestObject.csand note how it extendsIntacct.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 aList<IFunction>. -
Open
TestObjectCreate.csand note how it extendsAbstractTestObject.The
IFunctioninterface requires that the class implement thevoid WriteXml(ref IaXmlWriter xml)function. This implementation is where you provide the XML that will be processed at the gateway endpoint. -
Back in
AbstractTestObject.cs, replacetest_objectwith the integration name for your custom object.protected const string IntegrationName = "test_object";
Run the example
-
Run the project, and enter
5when prompted.For example, the following runs the project from the command line:
cd Intacct.Examples "C:\Program Files\dotnet\dotnet.exe" bin/Debug/netcoreappX.X/Intacct.Examples.dll -
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.
-
Type
6to exit the program. -
Open the generated
Intacct.Examples/bin/Debug/netcoreappX.X/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
CustomObjectFunction.csto execute aReadfunction using the record ID returned byTestObjectCreateas the key. For example:using Intacct.SDK.Functions.Common; ... Read read = new Read() { ObjectName = "test_object", Keys = { 10054 } }; Task<OnlineResponse> readTask = client.Execute(read); readTask.Wait(); OnlineResponse readResponse = readTask.Result; Result readResult = readResponse.Results[0]; int recordNo = int.Parse(readResult.Data[0].Element("id").Value); Console.WriteLine("Read record ID " + recordNo.ToString());
Delete the record
-
Create a
TestObjectDeleteclass and execute an instance of it that uses the same record ID returned in theTestObjectCreateresult.If you need help, see the example class.
using Intacct.SDK.Xml; namespace Intacct.Examples { public class TestObjectDelete : AbstractTestObject { public TestObjectDelete(int recordId) { this.Id = recordId; } public override void WriteXml(ref IaXmlWriter xml) { xml.WriteStartElement("function"); xml.WriteAttribute("controlid", this.ControlId, true); xml.WriteStartElement("delete"); xml.WriteElement("object", IntegrationName); xml.WriteElement("keys", this.Id); xml.WriteEndElement(); // delete xml.WriteEndElement(); // function } } }
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.