intacct / intacct-sdk-js

Official repository of the Sage Intacct SDK for JavaScript in Node.js
https://developer.intacct.com/tools/sdk-node-js/
Apache License 2.0
25 stars 32 forks source link

Can't use locationId when creating invoices #140

Open paltman opened 2 months ago

paltman commented 2 months ago

I've modified the node-js example code to test out creating an invoice:

/**
 * Copyright 2020 Sage Intacct, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "LICENSE" file accompanying this file. This file is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

gettingStarted();

async function gettingStarted() {
    const bootstrap = require("./bootstrap");
    const IA = require("@intacct/intacct-sdk");
    let logger = bootstrap.logger();

    try {
        const client = bootstrap.client(logger);

        let create = new IA.Functions.AccountsReceivable.InvoiceCreate();
        create.customerId = "CU01"
        create.transactionDate = new Date();
        create.dueDate = new Date(); 

        const line = new IA.Functions.AccountsReceivable.InvoiceLineCreate();
        line.glAccountNumber = "5220";
        line.amount = 76343.43;
        line.locationId = "01";
        line.departmentId = "00";
        line.employeeId = "EM01";
        create.lines = [line];

        logger.info("Executing read to Intacct API");

        const response = await client.execute(create);
        const result = response.getResult();

        logger.debug("Read successful", {
            "Company ID": response.authentication.companyId,
            "User ID": response.authentication.userId,
            "Request control ID": response.control.controlId,
            "Function control ID": result.controlId,
            "Data": result.data,
        });

        let json_data = result.data;

        console.log("Result:");
        console.log( JSON.stringify(json_data) );
    } catch (ex) {
        if (ex instanceof IA.Exceptions.ResponseException) {
            logger.error("An Intacct response exception was thrown", {
                "Class": ex.constructor.name,
                "Message": ex.message,
                "API Errors": ex.errors,
            });
            console.log("Failed! " + ex.message);
        } else {
            logger.error("An exception was thrown", {
                "Class": ex.constructor.name,
                "Message": ex.message,
            });
            console.log(ex.name)
        }
    }
}

This raises an exception:

{
  "API Errors":[
    "XL03000003 XML Parse schema error: Error 1871: Element 'locationid': This element is not expected. Expected is one of ( offsetglaccountno, amount ).. Line: 1, column: 0. [Support ID: Ygt_AWEB033%7EZutZFP1S0eT78kJ-fx8p7AAAAAA]"
  ],
  "Class":"ResponseException",
  "Message":"Response control status failure - XL03000003 XML Parse schema error: Error 1871: Element 'locationid': This element is not expected. Expected is one of ( offsetglaccountno, amount ).. Line: 1, column: 0. [Support ID: Ygt_AWEB033%7EZutZFP1S0eT78kJ-fx8p7AAAAAA]",
  "level":"error",
  "message":"An Intacct response exception was thrown"
}

I've received the same error when constructing the raw XML and hitting API without any SDK layer. When I set a locationId in my login block when creating my session and remove it here, it works (I have a multi-location account, but in that case the locationId has to be my top level entity).

Does the API not support creating invoices for a specific location under my top level entity?

paltman commented 2 months ago

The issue is because the XML produced is sensitive to the order of the sibling elements and I think this library is producing the elements out of order from what the documentation outlines.

I can reproduce by hand constructing and sending the XML to the API and then when I change the ordering of the sibling elements for the <lineitem /> group to match what the documentation has, it works.