Create invoice from 2 salesorders with custom function

Create invoice from 2 salesorders with custom function

Hello, 

We want to make a custom function that, when a customer has 2 or more sales orders, create only one invoice instead of 2 (we have a workflow to create invoices automatically from sales orders that have been sent). 

I have been investigating the API documentation and I am not sure how to approach this. The main problem is that I dont see how to link more than one sales order to a invoice when I am creating it. 

When I retrieve an invoice created from 2 sales orders the JSON  has only two fields: salesorder_id and salesorder_number, but they are from only one invoice despite it was created in Books from 2 sales orders. After those fields there is a Map called salesorders with the info of the 2 that belong to that invoice. What are the rules about this structure? Which fields are mandatory and which not? Can I create a JSON with this structure and create an invoice with it? 

  1. "salesorder_id": "81921000005055258",
  2.     "salesorder_number": "SO-04871",
  3.     "salesorders": [
  4.       {
  5.         "salesorder_id": "81921000005055258",
  6.         "salesorder_number": "SO-04871",
  7.         "reference_number": "",
  8.         "salesorder_order_status": "open",
  9.         "order_sub_status": "",
  10.         "order_sub_status_id": "",
  11.         "total": 561.68,
  12.         "sub_total": 578,
  13.         "date": "2021-03-02",
  14.         "shipment_date": "2021-03-12"
  15.       },
  16.       {
  17.         "salesorder_id": "81921000005055280",
  18.         "salesorder_number": "SO-04872",
  19.         "reference_number": "",
  20.         "salesorder_order_status": "closed",
  21.         "order_sub_status": "",
  22.         "order_sub_status_id": "",
  23.         "total": 82.21,
  24.         "sub_total": 578,
  25.         "date": "2021-03-10",
  26.         "shipment_date": ""
  27.       }