0
votes

I had wrote some code for creating po via code, but it faild with error message: "CS Error: Cannot generate the next number for the sequence."

How can I fix this? Anything I missed? Thx for helping!

protected void createPO() {

  POOrder order = new POOrder();
  POOrderEntry graph = PXGraph.CreateInstance < POOrderEntry > ();
  order.OrderType = "Normal";
  order.OrderDesc = "some text";
  order.EmployeeID = 215;
  order.Hold = false;

  var branch = (Branch)PXSelect<Branch, Where<Branch.branchCD, Equal<Required<Branch.branchCD>>>>.Select(Base, "WEST");

  graph.FieldDefaulting.AddHandler<POOrder.branchID>((s, e) =>
  {
      e.NewValue = branch.BranchID;  
      e.Cancel = true;
  });

  order.VendorID = 79;
  order = graph.CurrentDocument.Insert(order);
  graph.CurrentDocument.Update(order);
  graph.Actions.PressSave();

  throw new PXRedirectRequiredException(graph, null);
}
1

1 Answers

1
votes

Try to simply it first to see if something like this works...

protected void createPO()
{
    var graph = PXGraph.CreateInstance<POOrderEntry>();
    var order = graph.Document.Insert(new POOrder());

    order.OrderType = POOrderType.RegularOrder; // This is the default so not necessary
    order.OrderDesc = "some text";
    order.EmployeeID = 215;
    order.Hold = false;
    order.VendorID = 79;
    graph.Document.Update(order);
    graph.Actions.PressSave();

    throw new PXRedirectRequiredException(graph, null);
}

Using the Document view in place of the CurrentDocument view which is based on the current record of Document. Document is the primary view and the primary view should be used.

Also for the purchase order type the attribute related to the list values should be used for the stored value of the database (vs what you had was the displayed list value). For example order.OrderType = POOrderType.RegularOrder. Also this is the default value for a PO so it is not necessary to set this value unless you want a different constant found in the POOrderType class.