Creating a Load and shipment from a sales line
I’ve recently been asked to develop a mod to create a load, load line and a shipment for a SalesLine record from code. I had a quick look and could not see anything on the internet so below is the code I used. Sadly it seems that the shipment table does not have any init methods so the values are assigned individually:
static void Job1(Args _args)
{
SalesLine salesLine;
SalesTable salesTable;
WHSLoadTable loadTable;
WHSLoadLine loadLine;
InventDim inventDim;
InventSite inventSite;
WHSShipmentTable whsShipmentTable;
WHSShipmentTable consolidateShipmentTable;
LogisticsPostalAddress dlvAddress;
boolean consolidate;
TMSSalesTable tmsSalesTable;
;
select firstOnly salesLine
where salesLine.InventTransId == "LOT006059";
salesTable = salesLine.salesTable();
InventDim = salesLine.inventDim();
inventSite = inventDim.inventSite();
ttsBegin;
loadTable.initValue();
loadTable.initFromItem(salesLine.ItemId);
loadTable.LoadId = "MyCustomID4";
loadTable.LoadDirection = WHSLoadDirection::Outbound;
loadTable.initFromLoadTemplateId(inventSite.DefaultLoadTemplateId);
loadTable.LoadPaysFreight = NoYes::No;
loadTable.insert();
dlvAddress = salesLine.deliveryAddress();
loadLine.initValue();
loadLine.LoadId = loadTable.LoadId;
loadLine.initFromSalesLine(salesLine);
if(loadLine.validateWrite())
loadLine.insert();
if(!dlvAddress.whsAddressFormatValidation())
throw error("error");
whsShipmentTable.initValue();
consolidate = InventLocation::find(inventDim.InventLocationId).ConsolidateShipAtRTW;
select firstonly forupdate consolidateShipmentTable
where consolidateShipmentTable.AccountNum == salesLine.CustAccount
&& consolidateShipmentTable.DeliveryName == salesLine.DeliveryName
&& consolidateShipmentTable.DeliveryPostalAddress == dlvAddress.RecId
&& consolidateShipmentTable.InventLocationId == inventDim.InventLocationId
&& consolidateShipmentTable.LoadId == loadLine.LoadId
&& consolidateShipmentTable.ShipmentStatus < WHSShipmentStatus::Shipped
&& (consolidate
|| consolidateShipmentTable.OrderNum == salesLine.SalesId);
if(consolidateShipmentTable)
{
consolidateShipmentTable.OrderNum = consolidateShipmentTable.OrderNum != salesLine.SalesId ? '' : salesLine.SalesId;
consolidateShipmentTable.CustomerRef = consolidateShipmentTable.CustomerRef != salesTable.CustomerRef ? '' : salesTable.CustomerRef;
consolidateShipmentTable.CustomerReq = consolidateShipmentTable.CustomerReq != salesTable.PurchOrderFormNum ? '' : salesTable.PurchOrderFormNum;
consolidateShipmentTable.DlvTermId = consolidateShipmentTable.DlvTermId != salesTable.DlvTerm ? '' : salesTable.DlvTerm;
consolidateShipmentTable.update();
}
else
{
whsShipmentTable.clear();
whsShipmentTable.ShipmentId = "CustomID4";
whsShipmentTable.LoadId = loadLine.LoadId;
whsShipmentTable.WorkTransType = WHSWorkTransType::Sales;
whsShipmentTable.OrderNum = salesLine.SalesId;
whsShipmentTable.AccountNum = salesLine.CustAccount;
whsShipmentTable.DeliveryName = salesLine.DeliveryName;
whsShipmentTable.DeliveryPostalAddress = dlvAddress.RecId;
whsShipmentTable.CountryRegionISOCode = LogisticsAddressCountryRegion::find(dlvAddress.CountryRegionId).isOcode;
whsShipmentTable.Address = LogisticsPostalAddress::formatAddress( dlvAddress.Street,
dlvAddress.ZipCode,
dlvAddress.City,
dlvAddress.CountryRegionId,
dlvAddress.State,
dlvAddress.County);
whsShipmentTable.DlvTermId = salesTable.DlvTerm;
whsShipmentTable.InventSiteId = inventDim.InventSiteId;
whsShipmentTable.InventLocationId = inventDim.InventLocationId;
whsShipmentTable.CarrierCode = tmsSalesTable.CarrierCode;
whsShipmentTable.CarrierServiceCode = tmsSalesTable.CarrierServiceCode;
whsShipmentTable.BrokerCode = tmsSalesTable.BrokerCode;
whsShipmentTable.RouteCode = tmsSalesTable.RouteConfigCode;
whsShipmentTable.ModeCode = tmsSalesTable.ModeCode;
whsShipmentTable.CarrierGroupCode = tmsSalesTable.CarrierGroupCode;
whsShipmentTable.LoadDirection = WHSLoadDirection::Outbound;
whsShipmentTable.CustomerRef = salesTable.CustomerRef;
whsShipmentTable.CustomerReq = salesTable.PurchOrderFormNum;
if(whsShipmentTable.validateWrite())
{
whsShipmentTable.insert();
whsShipmentTable.createShipmentNotes(salesTable);
loadLine.ShipmentId = whsShipmentTable.ShipmentId ? whsShipmentTable.ShipmentId : consolidateShipmentTable.ShipmentId;
if(loadLine.validateWrite())
loadLine.update();
}
}
ttsCommit;
}