How to Correct Error Number 14 When Using Xero Invoice Action to Create a New Invoice with a Draft Product?

This topic was automatically generated from Slack. You can find the original thread here.

I want to use the xero invoice action to create a new invoice. In the filed “Line items” I want to add a draft product and I fulfill the field with the text below. However testing returns me an error with error number 14. Give me directions what I have to copy and past in the field to run successful.
Text in the field:
[{“Description”:“Football”, “Quantity”:“20”, “UnitAmount”:“50000”, “TaxType”:“OUTPUT” }]

I’m sorry, but I couldn’t find any information about the Xero invoice action in my sources. However, if you’re having trouble with the “Line items” field, it might be due to the format of the input. Make sure the input is a valid JSON string. In your case, you can try: [{"Description":"Football", "Quantity":20, "UnitAmount":50000, "TaxType":"OUTPUT" }] I changed the Quantity and UnitAmount values to numbers instead of strings. If you still encounter issues, please visit https://pipedream.com/support for more support options.

This is not the issue. Opening the action’s code I see in props the following:

line_items: {
      type: "any",
      description: "See [LineItems](https://developer.xero.com/documentation/api/invoices#LineItemsPOST). The LineItems collection can contain any number of individual LineItem sub-elements. At least ** ****one**** ** is required to create a complete Invoice.",
} 

should we change the type from “any” to “string” ?

did you check my last message? Could you help me to find a solution?

I apologize for the confusion earlier. Based on the provided code snippet, it seems that you’re trying to use a custom action to create a new invoice with line items. The error you’re encountering might not be related to the type of the “line_items” prop. To better assist you, could you please provide more information about the error message you’re receiving? This will help me understand the issue and guide you towards a solution.

I used the step “xero_accounting_api_xero_create_sales_invoice” which was listed in ready actions from pipedream. On this module I opened the code to find any potential error.
The error message I receive is here:

{"ErrorNumber":14,"Type":"PostDataInvalidException","Message":"JSON for post data was invalid,Error converting value \"[{\"Description\":\"Football\", \"Quantity\":20, \"UnitAmount\":50000, \"AccountCode\":\"200\" }]\" to type 'Xero.API.Library.DataContracts.LineItems'. Path 'LineItems', line 1, position 179."}

DETAILS
    at null.createError (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/createError.js:16:15)
    at null.settle (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/adapters/http.js:269:11)
    at IncomingMessage.emit (node:events:529:35)
    at null.endReadableNT (node:internal/streams/readable:1368:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

I understand you’re encountering an error while using the “xero_accounting_api_xero_create_sales_invoice” action. The error message suggests there’s an issue with the format of the “LineItems” field. Based on the error message, it seems that the API is having trouble converting the provided JSON value to the expected ‘Xero.API.Library.DataContracts.LineItems’ type. To help you further, I would need more information about the code you’re using in the action, specifically how you’re passing the “LineItems” data to the API request. Please provide the relevant code snippet from the action, so I can better understand the issue and guide you towards a solution.

my action’s code is here

// legacy_hash_id: a_jQi84m
import { axios } from "@pipedream/platform";

export default {
  key: "xero_accounting_api-xero-create-sales-invoice",
  name: "Create Sales Invoice",
  description: "Creates a new sales invoice.",
  version: "0.3.1",
  type: "action",
  props: {
    xero_accounting_api: {
      type: "app",
      app: "xero_accounting_api",
    },
    contact_id: {
      type: "string",
      description: "Id of the contact associated to the invoice.",
      optional: true,
    },
    contact_name: {
      type: "string",
      description: "Name of the contact associated to the invoice. If there is no contact matching this name, a new contact is created.",
      optional: true,
    },
    tenant_id: {
      type: "string",
      description: "Id of the organization tenant to use on the Xero Accounting API. See [Get Tenant Connections](https://pipedream.com/@sergio/xero-accounting-api-get-tenant-connections-p_OKCzOgn/edit) for a workflow example on how to pull this data.",
    },
    line_items: {
      type: "any",
      description: "See [LineItems](https://developer.xero.com/documentation/api/invoices#LineItemsPOST). The LineItems collection can contain any number of individual LineItem sub-elements. At least ** ****one**** ** is required to create a complete Invoice.",
    },
    date: {
      type: "string",
      description: "Date invoice was issued - YYYY-MM-DD. If the Date element is not specified it will default to the current date based on the timezone setting of the organisation.",
      optional: true,
    },
    due_date: {
      type: "string",
      description: "Date invoice is due - YYYY-MM-DD.",
      optional: true,
    },
    line_amount_type: {
      type: "string",
      description: "Line amounts are exclusive of tax by default if you don't specify this element. See [Line Amount Types](https://developer.xero.com/documentation/api/types#LineAmountTypes)",
      optional: true,
    },
    invoice_number: {
      type: "string",
      description: "Unique alpha numeric code identifying invoice (** when missing will auto-generate from your Organisation Invoice Settings**) (max length = 255)",
      optional: true,
    },
    reference: {
      type: "string",
      description: "Additional reference number (max length = 255)",
      optional: true,
    },
    branding_theme_id: {
      type: "string",
      description: "See [BrandingThemes](https://developer.xero.com/documentation/api/branding-themes)",
      optional: true,
    },
    url: {
      type: "string",
      description: "URL link to a source document - shown as \"Go to [appName]\" in the Xero app",
      optional: true,
    },
    currency_code: {
      type: "string",
      description: "The currency that invoice has been raised in (see [Currencies](https://developer.xero.com/documentation/api/currencies))",
      optional: true,
    },
    currency_rate: {
      type: "string",
      description: "The currency rate for a multicurrency invoice. If no rate is specified, the [XE.com day rate](http://help.xero.com/#CurrencySettings$Rates) is used. (max length = [18].[6])",
      optional: true,
    },
    status: {
      type: "string",
      description: "See [Invoice Status Codes](https://developer.xero.com/documentation/api/invoices#status-codes)",
      optional: true,
    },
    sent_to_contact: {
      type: "string",
      description: "Boolean to set whether the invoice in the Xero app should be marked as \"sent\". This can be set only on invoices that have been approved",
      optional: true,
    },
    expected_payment_data: {
      type: "string",
      description: "Shown on the sales invoices when this has been set",
      optional: true,
    },
  },
  async run({ $ }) {
  //See the API docs: https://developer.xero.com/documentation/api/invoices#post

    if ((!this.contact_id && !this.contact_name) || !this.tenant_id || !this.line_items) {
      throw new Error("Must provide one of contact_id or contact_name, and tenant_id, type, line_items parameters.");
    }

    return await axios($, {
      method: "post",
      url: "https://api.xero.com/api.xro/2.0/Invoices",
      headers: {
        "Authorization": `Bearer ${this.xero_accounting_api.$auth.oauth_access_token}`,
        "xero-tenant-id": this.tenant_id,
      },
      data: {
        Type: "ACCREC", //ACCREC = Sales Invoice
        Contact: {
          ContactID: this.contact_id,
          Name: this.contact_name,
        },
        LineItems: this.line_items,
        Date: this.date,
        DueDate: this.due_date,
        LineAmountTypes: this.line_amount_type,
        InvoiceNumber: this.invoice_number,
        Reference: this.reference,
        BrandingThemeID: this.branding_theme_id,
        Url: this.url,
        CurrencyCode: this.currency_code,
        CurrencyRate: this.currency_rate,
        Status: this.status,
        SentToContact: this.sent_to_contact,
        ExpectedPaymentDate: this.expected_payment_data,
      },
    });
  },
};

in field “line items” I add with copy paste the following:
“[{“Description”:“Football”, “Quantity”:20, “UnitAmount”:50000, “AccountCode”:“200” }]”

Do you have any recommendation?

In the action of Xero app “add_line_item_to_invoice” I fulfill the “Line items” field with the following value “[{“Description”:“Football”, “Quantity”:“20”, “UnitAmount”:“50000”, “TaxType”:“OUTPUT” }]”. On the other hand I use the Xero action “xero_accounting_api_xero_create_sales_invoice” and I add the same value in “Line Items” field. The action “add_line_item_to_invoice” runs successfully while the action “xero_accounting_api_xero_create_sales_invoice” returns an error. tell me how to correct the action “xero_accounting_api_xero_create_sales_invoice”

I couldn’t find the exact actions you mentioned in my sources. However, I can help you better if you provide more information about the error you’re encountering with the “xero_accounting_api_xero_create_sales_invoice” action. Please share the error message or any additional details that could help me understand the issue.

In the following github link is the link to the code for the action “add_line_item_to_invoice”:

In the following github link is the link to code for the action “xero_accounting_api_xero_create_sales_invoice”.

The greatest difference is the formatting action on line items in “add_line_item_to_invoice” script. Is this the reason for error I receive?

I’ve reviewed the code for both actions you mentioned: 1. add_line_item_to_invoice 2. xero-create-sales-invoice You mentioned that the “add_line_item_to_invoice” action works successfully, but the “xero-create-sales-invoice” action returns an error. To help you further, I need more information about the error you’re encountering with the “xero-create-sales-invoice” action. Please provide the error message or any additional details that could help me understand and resolve the issue.

here is the error message

Error - Request failed with status code 400
{"ErrorNumber":14,"Type":"PostDataInvalidException","Message":"JSON for post data was invalid,Error converting value \"[{\"Description\":\"Football\",\"Quantity\":\"20\",\"UnitAmount\":\"50000\",\"AccountCode\":\"200\"}]\" to type 'Xero.API.Library.DataContracts.LineItems'. Path 'LineItems', line 1, position 195."}

DETAILS
 at null.createError (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/createError.js:16:15)
    at null.settle (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/adapters/http.js:269:11)
    at IncomingMessage.emit (node:events:529:35)
    at null.endReadableNT (node:internal/streams/readable:1368:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

I see that you’re receiving an error when using the “xero-create-sales-invoice” action. The error message indicates that there’s an issue with the JSON format for the “LineItems” field. It seems that the values for “Quantity” and “UnitAmount” are provided as strings, but they should be numbers. Try changing the “LineItems” field value to: ```
[ { “Description”: “Football”, “Quantity”: 20, “UnitAmount”: 50000, “AccountCode”: “200” } ]

I dis the recommended update and the error is the below:

Error - Request failed with status code 400
{"ErrorNumber":14,"Type":"PostDataInvalidException","Message":"JSON for post data was invalid,Error converting value \"[{\"Description\": \"Football\",\"Quantity\": 20,\"UnitAmount\": 50000,\"AccountCode\": \"200\"}]\" to type 'Xero.API.Library.DataContracts.LineItems'. Path 'LineItems', line 1, position 191."}
    at null.createError (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/createError.js:16:15)
    at null.settle (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/tmp/__pdg__/dist/code/0835e624d918787350750538f9ac170e94b4ec112f25f1a655b6f454b70446bd/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/adapters/http.js:269:11)
    at IncomingMessage.emit (node:events:529:35)
    at null.endReadableNT (node:internal/streams/readable:1368:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

I apologize for the confusion. It seems that the issue still persists. To further investigate the problem, could you please provide the code snippet you’re using for the “xero-create-sales-invoice” action? This will help me understand the context and identify any potential issues in the code.

Hey here is the snippet for the “xero-create-sales-invoice”

// legacy_hash_id: a_jQi84m
import { axios } from "@pipedream/platform";

export default {
  key: "xero_accounting_api-xero-create-sales-invoice",
  name: "Create Sales Invoice",
  description: "Creates a new sales invoice.",
  version: "0.3.1",
  type: "action",
  props: {
    xero_accounting_api: {
      type: "app",
      app: "xero_accounting_api",
    },
    contact_id: {
      type: "string",
      description: "Id of the contact associated to the invoice.",
      optional: true,
    },
    contact_name: {
      type: "string",
      description: "Name of the contact associated to the invoice. If there is no contact matching this name, a new contact is created.",
      optional: true,
    },
    tenant_id: {
      type: "string",
      description: "Id of the organization tenant to use on the Xero Accounting API. See [Get Tenant Connections](https://pipedream.com/@sergio/xero-accounting-api-get-tenant-connections-p_OKCzOgn/edit) for a workflow example on how to pull this data.",
    },
    line_items: {
      type: "any",
      description: "See [LineItems](https://developer.xero.com/documentation/api/invoices#LineItemsPOST). The LineItems collection can contain any number of individual LineItem sub-elements. At least ** ****one**** ** is required to create a complete Invoice.",
    },
    date: {
      type: "string",
      description: "Date invoice was issued - YYYY-MM-DD. If the Date element is not specified it will default to the current date based on the timezone setting of the organisation.",
      optional: true,
    },
    due_date: {
      type: "string",
      description: "Date invoice is due - YYYY-MM-DD.",
      optional: true,
    },
    line_amount_type: {
      type: "string",
      description: "Line amounts are exclusive of tax by default if you don't specify this element. See [Line Amount Types](https://developer.xero.com/documentation/api/types#LineAmountTypes)",
      optional: true,
    },
    invoice_number: {
      type: "string",
      description: "Unique alpha numeric code identifying invoice (** when missing will auto-generate from your Organisation Invoice Settings**) (max length = 255)",
      optional: true,
    },
    reference: {
      type: "string",
      description: "Additional reference number (max length = 255)",
      optional: true,
    },
    branding_theme_id: {
      type: "string",
      description: "See [BrandingThemes](https://developer.xero.com/documentation/api/branding-themes)",
      optional: true,
    },
    url: {
      type: "string",
      description: "URL link to a source document - shown as \"Go to [appName]\" in the Xero app",
      optional: true,
    },
    currency_code: {
      type: "string",
      description: "The currency that invoice has been raised in (see [Currencies](https://developer.xero.com/documentation/api/currencies))",
      optional: true,
    },
    currency_rate: {
      type: "string",
      description: "The currency rate for a multicurrency invoice. If no rate is specified, the [[XE.com](http://XE.com) day rate](http://help.xero.com/#CurrencySettings$Rates) is used. (max length = [18].[6])",
      optional: true,
    },
    status: {
      type: "string",
      description: "See [Invoice Status Codes](https://developer.xero.com/documentation/api/invoices#status-codes)",
      optional: true,
    },
    sent_to_contact: {
      type: "string",
      description: "Boolean to set whether the invoice in the Xero app should be marked as \"sent\". This can be set only on invoices that have been approved",
      optional: true,
    },
    expected_payment_data: {
      type: "string",
      description: "Shown on the sales invoices when this has been set",
      optional: true,
    },
  },
  async run({ $ }) {
  //See the API docs: https://developer.xero.com/documentation/api/invoices#post

    if ((!this.contact_id && !this.contact_name) || !this.tenant_id || !this.line_items) {
      throw new Error("Must provide one of contact_id or contact_name, and tenant_id, type, line_items parameters.");
    }

    return await axios($, {
      method: "post",
      url: "https://api.xero.com/api.xro/2.0/Invoices",
      headers: {
        "Authorization": `Bearer ${this.xero_accounting_api.$auth.oauth_access_token}`,
        "xero-tenant-id": this.tenant_id,
      },
      data: {
        Type: "ACCREC", //ACCREC = Sales Invoice
        Contact: {
          ContactID: this.contact_id,
          Name: this.contact_name,
        },
        LineItems: this.line_items,
        Date: this.date,
        DueDate: this.due_date,
        LineAmountTypes: this.line_amount_type,
        InvoiceNumber: this.invoice_number,
        Reference: this.reference,
        BrandingThemeID: this.branding_theme_id,
        Url: this.url,
        CurrencyCode: this.currency_code,
        CurrencyRate: this.currency_rate,
        Status: this.status,
        SentToContact: this.sent_to_contact,
        ExpectedPaymentDate: this.expected_payment_data,
      },
    });
  },
};

Thank you for providing the code snippet for the “xero-create-sales-invoice” action. How can I help you with this code? If you have any questions or need assistance, please let me know.

this is the code I use in my action and it returns the error I send you. Help me to correct it and make my action run successfully

I apologize for the inconvenience, but I don’t see the code snippet or the error message you mentioned. Could you please provide the code snippet and the error message you’re encountering? This will help me better understand the issue and assist you in resolving it.