Jerry Welliver
Jerry Welliver

Reputation: 377

Create sales order via code 2017 R2

I am trying to create sales orders with data retrieved from my edi system. I have the code working to retrieve the orders, but I am having trouble creating the SO. I can create them with an external program using the SOAP interface, but I am trying to use the graph and insert them directly. I get an exception stating "{"Error: An error occurred during processing of the field CustomerLocationID : Object reference not set to an instance of an object.."}" but I successfully looked up the customer id and location id. Here is the code for my routine. I use similar code to create records for a new master/detail set of tables that I created.

Please advise if anyone has insight on creating sales orders in this manner. The error is thrown at the line: soOrder.CurrentDocument.Insert(order);

I also am getting an error trying to access the extended fields for SOOrder and SOLine. I currently commented those fields out to see if I could create an order, but I will need to load data in them as well.

        foreach (LingoOrderSearch ediOrder in doc850)
        {
            res850 = lingo.Retrieve850(ediOrder.documentId, "850");
            SOOrderEntry soOrder = PXGraph.CreateInstance<SOOrderEntry>();
            soOrder.Clear();
            var order = new SOOrder();
            //SOOrderExt orderExt = order.GetExtension<SOOrderExt>();

            order.OrderType = "SO";
            order.Status = "Open";
            if (res850.Data850.partner == "BEDBATH" || res850.Data850.partner == "BEDBATH_CAN")
                customerLookup = "BBB";
            else
                customerLookup = res850.Data850.partner;
            CustomerMaint customerGraph = PXGraph.CreateInstance<CustomerMaint>();
            Customer arCustomer = PXSelect<Customer, Where<Customer.status, Equal<Required<Customer.status>>,
                And<Customer.acctCD, Equal<Required<Customer.acctCD>>>>>.Select(this, "Active", customerLookup);
            if (arCustomer == null)
                throw new PXException("Unable to find customer " + customerLookup + " (partner:" + res850.Data850.partner + ")");
            order.CustomerID = arCustomer.BAccountID;
            CustomerLocationMaint customerLocationGraph = PXGraph.CreateInstance<CustomerLocationMaint>();
            Location arCustomerLocation = PXSelect<Location, Where<Location.isActive, Equal<Required<Location.isActive>>,
                And<Location.bAccountID, Equal<Required<Location.bAccountID>>,
                And<Location.locationCD, Equal<Required<Location.locationCD>>>>>>
                .Select(this, true, arCustomer.BAccountID, res850.Data850.location);
            if (arCustomerLocation == null)
                throw new PXException("Unable to find customer location" + customerLookup + " / " +
                    res850.Data850.location  + " (partner:" + res850.Data850.partner + ")");
            order.CustomerLocationID = arCustomerLocation.LocationID;
            order.CustomerOrderNbr = res850.Data850.poNumber;
            order.ExtRefNbr = res850.Data850.documentId.ToString();
            //orderExt.UsrEDICustomerVendorId = res850.Data850.vendor;
            //orderExt.UsrEDICustomerId = res850.Data850.partner;
            if (!DateTime.TryParse(res850.Data850.poDate, out tempDate))
            {
                tempDate = DateTime.Today;
            }
            order.DocDate = tempDate;                
            DateTime.TryParse(res850.Data850.requestedDeliveryDate, out tempDate);
            if (!DateTime.TryParse(res850.Data850.requestedDeliveryDate, out tempDate))
            {
                tempDate = DateTime.Today;
            }
            order.RequestDate = tempDate;
            soOrder.CurrentDocument.Insert(order);
            soOrder.Persist();
            newOrderId = soOrder.CurrentDocument.Current.OrderNbr;

            itemList = res850.Data850.items;
            foreach (EdiDoc850Lingoitems item in itemList)
            {
                InventoryItemMaint invItemGraph = PXGraph.CreateInstance<InventoryItemMaint>();
                InventoryItem invItem = PXSelect<InventoryItem, 
                    Where<InventoryItem.itemStatus, Equal<Required<InventoryItem.itemStatus>>,
                    And<InventoryItem.inventoryCD, Equal<Required<InventoryItem.inventoryCD>>>>>
                    .Select(this, "Active", item.vendorItem);
                if (invItem == null)
                    throw new PXException("Unable to locate item " + item.vendorItem);
                var line = new SOLine();
                //SOLineExt lineExt = line.GetExtension<SOLineExt>();
                line.OrderNbr = newOrderId;
                line.InventoryID = invItem.InventoryID;
                line.Qty = item.qtyOrder;
                line.ShipComplete = "Ship Complete";
                Int32 tempLine = 0;
                if (Int32.TryParse(item.lineNo, out tempLine)) { };
                //lineExt.UsrEDILineNbr = tempLine;
                soOrder.Transactions.Insert(line);
            }
        }
    }

Upvotes: 0

Views: 445

Answers (1)

RuslanDev
RuslanDev

Reputation: 6778

Hopefully, the sample below will help move forward with your task:

SOOrderEntry soOrder = PXGraph.CreateInstance<SOOrderEntry>();
var order = new SOOrder();
order.OrderType = SOOrderTypeConstants.SalesOrder;
order = soOrder.CurrentDocument.Insert(order);

Customer arCustomer = PXSelect<Customer, Where<Customer.status, Equal<Required<Customer.status>>,
    And<Customer.acctCD, Equal<Required<Customer.acctCD>>>>>.Select(this, "Active", "ABARTENDE");
if (arCustomer == null)
    throw new PXException("Unable to find customer");
order.CustomerID = arCustomer.BAccountID;
order = soOrder.CurrentDocument.Update(order);

Location arCustomerLocation = PXSelect<Location, Where<Location.isActive, Equal<Required<Location.isActive>>,
    And<Location.bAccountID, Equal<Required<Location.bAccountID>>,
    And<Location.locationCD, Equal<Required<Location.locationCD>>>>>>
    .Select(this, true, arCustomer.BAccountID, "CHICAGO");
if (arCustomerLocation == null)
    throw new PXException("Unable to find customer location");
order.CustomerLocationID = arCustomerLocation.LocationID;
order.CustomerOrderNbr = "res850.Data850.poNumber";
order.ExtRefNbr = "res850.Data850.documentId";
order.RequestDate = DateTime.Today;
soOrder.CurrentDocument.Update(order);

for(int i = 0; i < 3; i++)
{
    InventoryItem invItem = PXSelect<InventoryItem,
        Where<InventoryItem.itemStatus, Equal<Required<InventoryItem.itemStatus>>,
        And<InventoryItem.inventoryCD, Equal<Required<InventoryItem.inventoryCD>>>>>
        .Select(this, "Active", "AALEGO500");
    if (invItem == null)
        throw new PXException("Unable to locate item");
    var line = soOrder.Transactions.Insert();
    line.InventoryID = invItem.InventoryID;
    line.Qty = 1;
    line.ShipComplete = SOShipComplete.ShipComplete;
    soOrder.Transactions.Update(line);
}

soOrder.Actions.PressSave();

Upvotes: 3

Related Questions