The example shows the recommended approach for creating custom objects, as follows:

Prerequisites

Project structure

The example has a src directory that contains the sources for the custom object. Assuming you completed the quick start example, you also have a vendor directory at that same level. The CreateCustomObjectExample.php program sends the request to create the custom object to the gateway.

Understand the code

  1. Open src/CustomObjects/AbstractMyCustomObject.php. Note that the class extends AbstractFunction, provides the fields for the custom object, and includes the getters and setters:

    <?php
    abstract class AbstractMyCustomObject extends AbstractFunction
    {
         protected $name;
         protected $description;
          
         public function getName() {
             return $this->name;
         }               
         public function setName($name) {
             $this->name = $name;
         }              
          public function getDescription() {
             return $this->description;
         }                
         public function setDescription($description) {
            $this->description = $description;
         }
    }
    
  2. In the same directory, open MyCustomObjectCreate. Note that the class extends the AbstractMyCustomObject class and provides an implementation of writeXml to write the XML for the custom object.

    <?php
    class MyCustomObjectCreate extends AbstractMyCustomObject
    {
        public function writeXml(XMLWriter &$xml)
        {
             $xml->startElement('function');
             $xml->writeAttribute('controlid', $this->getControlId());   
             $xml->startElement('create');
             $xml->startElement('test_object'); // Integration name in the Intacct system.
         
             if (!$this->getName()) {
                throw new InvalidArgumentException('Name is required for create');
             }
         
             $xml->writeElement('name', $this->getName(), true);
             $xml->writeElement('description', $this->getDescription(), true);
         
             $xml->endElement(); // test_object
             $xml->endElement(); // create
             $xml->endElement(); // function
        }
    }
    
  3. Open CreateCustomObjectExample.php in the project root. Note the code that creates the custom object and parses results:

     <?php
     try {
         $client = new IntacctClient([
             'profile_file' => __DIR__ . '\.intacct\credentials.ini'
         ]);
            
         $customObject = new MyCustomObjectCreate();
         $customObject->setName("Test name");
         $customObject->setDescription("Test description");
            
         $content = new Content([$customObject]);      
         $response = $client->execute($content);
         $results = $response->getOperation()->getResults();
            
         foreach ($results as $data) {
             var_dump($data);
         }
     // ...
    

It’s highly recommended that you create a unit test for your custom object too.

Run the code

Assuming you met the prerequisites (including creating the object definition in the Intacct system via Platform Services), you can run CreateCustomObjectExample.php and the custom object will be created in the Intacct system.

If you have other custom objects already defined in the Intacct system, you can copy the class files in this example and modify them to create instances of your custom objects.

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 realize AbstractBill and write the XML for creating or deleting bills.

Provide feedback