Post a batch of sales invoices using api, try to fix line custom fields

I successfully posted a new sales invoice using api. but I can’t do it for more than one Invoice. When I try to post two Sales invoices I get this message:


{
  "Success": false,
  "Error": "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Manager.Model.SalesInvoice' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\nPath '', line 1, position 1."
}

Here are my two invoices:

[
{
  "IssueDate": "2022-01-16",
  "Reference": "2022000050",
  "Customer": "fe78e04e-16c8-490b-b235-1ca756dd3846",
  "BillingAddress": "جدة - حي الحمرا\nالإدارة المالية - القطاع الغربي\nالرقم الضريبي:  123456789",
  "Lines": [
    {
      "Item": "df8b08e3-1a9d-4336-90a4-b6c35c05a0c6",
      "Account": "e1575310-f8e3-4bf8-a72e-0e401bdc7750",
      "Description": "212134252",
      "CustomFields": {
        "415a2f72-bc19-4829-9512-b88c633077d3": "الشركة",
        "88fac2a9-9acd-4103-afec-c0b54a393ec9": "<U+202B>فحص واختبار دقة وحدة قياس الطاقة<U+202C>‏",
        "a59bbc90-2fb0-4f87-9d42-82e76a040676": "465",
        "0d3bb8f0-cdd0-43f1-ae72-2dead531ebe3": "2021/12/01"
      },
      "Qty": 300,
      "UnitPrice": 127,
      "TaxCode": "303938dc-9b3a-4bc3-99e6-dbece8893d7b",
      "Project": "df09a515-c1d4-4a55-aaa0-2acdef9eee9b",
      "Division": "c51dcc48-94cd-486c-9b0e-5109c3db693c"
    }
  ],
  "LineDescription": true,
  "DiscountType": "ExactAmount",
  "TotalAmountInWords": true,
  "TotalAmountInBaseCurrency": true,
  "HideDueDate": true,
  "HasSalesInvoiceCustomTheme": true,
  "SalesInvoiceCustomTheme": "c23482c6-3c8a-4bbb-9c26-dcfe2658d747",
  "CustomFields": {
    "b8a8dd87-9c60-4ad1-81a3-3f5c82a4d349": "",
    "182e7530-9f06-4da5-a683-9503ab4a3647": "",
    "f64501d7-55e6-4a6a-b79a-0a4eea96e345": "",
    "64f2ae36-fde1-482e-9bc3-6aa03bc92ad5": "",
    "a77752fc-db12-44ef-a874-7d37fa10469d": "",
    "764659a6-b70a-4e78-9f5d-29c95916bdab": "",
    "9daa867b-30d7-4a54-ae1c-1e48e3d29388": "",
    "3e0167da-3bd4-4928-8c56-ed88404ba207": "2201000714",
    "4b8f6a1c-c4ee-4d43-85b6-d27207a20d74": "4400014765"
  }
},
{
  "IssueDate": "2022-01-16",
  "Reference": "2022000051",
  "Customer": "fe78e04e-16c8-490b-b235-1ca756dd3846",
  "BillingAddress": "جدة - حي الحمرا\nالإدارة المالية - القطاع الغربي\nالرقم الضريبي:  123456789",
  "Lines": [
    {
      "Item": "df8b08e3-1a9d-4336-90a4-b6c35c05a0c6",
      "Account": "e1575310-f8e3-4bf8-a72e-0e401bdc7750",
      "Description": "212134252",
      "CustomFields": {
        "415a2f72-bc19-4829-9512-b88c633077d3": "الشركة",
        "88fac2a9-9acd-4103-afec-c0b54a393ec9": "<U+202B>فحص واختبار دقة وحدة قياس الطاقة<U+202C>‏",
        "a59bbc90-2fb0-4f87-9d42-82e76a040676": "465",
        "0d3bb8f0-cdd0-43f1-ae72-2dead531ebe3": "2021/12/01"
      },
      "Qty": 300,
      "UnitPrice": 127,
      "TaxCode": "303938dc-9b3a-4bc3-99e6-dbece8893d7b",
      "Project": "df09a515-c1d4-4a55-aaa0-2acdef9eee9b",
      "Division": "c51dcc48-94cd-486c-9b0e-5109c3db693c"
    }
  ],
  "LineDescription": true,
  "DiscountType": "ExactAmount",
  "TotalAmountInWords": true,
  "TotalAmountInBaseCurrency": true,
  "HideDueDate": true,
  "HasSalesInvoiceCustomTheme": true,
  "SalesInvoiceCustomTheme": "c23482c6-3c8a-4bbb-9c26-dcfe2658d747",
  "CustomFields": {
    "b8a8dd87-9c60-4ad1-81a3-3f5c82a4d349": "",
    "182e7530-9f06-4da5-a683-9503ab4a3647": "",
    "f64501d7-55e6-4a6a-b79a-0a4eea96e345": "",
    "64f2ae36-fde1-482e-9bc3-6aa03bc92ad5": "",
    "a77752fc-db12-44ef-a874-7d37fa10469d": "",
    "764659a6-b70a-4e78-9f5d-29c95916bdab": "",
    "9daa867b-30d7-4a54-ae1c-1e48e3d29388": "",
    "3e0167da-3bd4-4928-8c56-ed88404ba207": "2201000714",
    "4b8f6a1c-c4ee-4d43-85b6-d27207a20d74": "4400014765"
  }
}
]

What I’m doing wrong?
How can I post more than one sales invoice, batch creat?

You have the same reference on both invoices - could this be the problem?

I’ve changed the 2nd Ref to 2022000051 but still the same message!

The API doesn’t accept an array of invoices, i.e:

[
    { 
        Invoice 1
    },
    { 
        Invoice 2
    },
]

The solution could be to name the array to make it a collection, i.e:

{
    'SalesInvoices' : [
        {
            ...
        },
        {
            ...
        }
    ]
}

Idk whether Manager is expecting a name or if any name could work.

:rose:
But, :roll_eyes: It’s little hard for me to understand your answer. Could you explain in a simpler way?

What I can do is to post one invoice, one by one it’s okay. What I can’t do is to post more than one invoice.
How can I join Invoices?!

Put these before your JSON data:

{ 'test_your_names_here' :

And then this at the end:

}

And see if it works.

Either try to figure out what the API accepts for a batch or make a loop that sends API post requests one at a time.

Personally, I prefer the loop method because it’s less likely to fail and also because I can keep track of individual invoices

I do not have any links or loops for api, I’m trying to fix the line custom field problem while doing batch creat, so I thought about using api to post these sales invoices. It works as I said but for one invoice, but I need to post more than one invoice.

This will be the right solution I know, but it needs an experience person and time. I’m searching for a temporary solution now then I can do it with the right way with some professional help.

I tried putting the first text “exact text” before the text I used then putting } at the end, but it gives me an empty invoice with error message inside manager.

The error message says it’s expecting either an object, collection or a named list but it says nothing about the naming, it could be:

  • Name the entire array to make a collection or

  • Name each invoice

Or maybe

  • An object of objects, which means just replacing the [ and ] at the beginning and end with { and }, respectively. (Which in hindsight, should’ve been the first thing we tried :man_facepalming:)

Anyway,

You can batch delete those

Also regarding the loop:

  1. I’m assuming you’re writing a code to send API requests for you, because otherwise manually sending API requests is a big waste of time.

  2. Assuming you’re writing a code, the loop isn’t part of the API, it’s part of the code you’re writing to send API requests.

Also also, have you considered using batch create/update? They’re huge time savers with far less overhead to use than API.

:slightly_frowning_face: actually, That’s what I’m doing.

:grin: That’s my problem, I can’t creat the invoice with line custom field, in my case the li e custom fields are very important.

I see.

But anyhow, manually editing each and every invoice is much faster than manually writing API requests.

I was thinking about a form easy to be filled in excel then join the fields text to have the right post request then join these requests to post them at once from api.
Doing the join in excel is not a problem but joining the requests for all the invoices. That’s what I can not do until now.

Try this one out:

This doesn’t involve guessing any names and I think it may actually work.

This also doesn’t work, are there spaces before or after or {} affecting the result?
I mean my request starts line:


{
{
  "IssueDate": "2022-01-16",
  "Reference": "2022000056",
  "Customer": "fe78e04e-16c8-490b-b235-1ca756dd3846",

and ends like that:


    "4b8f6a1c-c4ee-4d43-85b6-d27207a20d74": "4400014765"
  }
}
}

I’ve took the time to try some methods of posting more than 1 invoice using a single API request and it looks to me like the API is expecting a single invoice at a time.

That’s the furthest I can go.

1 Like

Thanks @Ealfardan for your precious time, it’s so kind of you to try.
Now, what I realized is that:

  1. I can use API to post invoices individually
  2. can’t post more than one invoice at once
  3. if I need to use it correctly I should create a loop to post invoices automatically.

So, this way won’t work to fix the main problem with the line custom field of the sales invoice.
Again and again, many thanks to you Mr @Ealfardan. :rose: