Getting the Shipment numbers from the posted sales invoice

Sometimes, you might want to print sales shipment information on a posted sales invoices. Because this is a one-to-many relationship – one sales invoice might be linked to many sales shipments and vice versa – you must get multiple records from the database.

Note: You can also use this article to get the invoices from the shipments. Just reverse the table numbers.

The first step is to add a JavaScript record for the sales shipment:

The next step is to read the records and store the information for use.

First add an array variable in the Header.OnPreDataItem trigger:

var shipments = []

 

Then add the script to read and store the shipments in the Header.OnAfterGetRecord trigger:

shipments = []
if (SalesShipmentHeader.FieldExtensions.No.HasValue) {
    shipments.push({
        no: SalesShipmentHeader.No,
        shipmentDate: SalesShipmentHeader.ShipmentDate
    })
}
while (SalesShipmentHeader.Next()) {
    shipments.push({
        no: SalesShipmentHeader.No,
        shipmentDate: SalesShipmentHeader.ShipmentDate
    })
}

 

Finally, we can use the collected information in a section. This line reads the shipment numbers and joins them with a new line character:

shipments.map(=> e.no).join(“\n”);