Sage 100

 View Only
Expand all | Collapse all

Script Sales Order to Invoice - OrderLineKey won't cooperate

  • 1.  Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-06-2024 18:04

    I really hope I'm missing something simple because this is the last bit of a big project that just won't behave. I have a script that creates Sales Order Invoices from Sales Orders. It works great and creates the invoice but the one thing it is refusing to do is write the OrderLineKey from Sales Order Detail to Invoice Detail. Debugging tells me it successfully gets the OrderLineKey and debugging even says it successfully writes the OrderLineKey to invoice detail but that field is always blank in So_InvoiceDetail when reviewed through DFDM. This results in duplicated sales order lines in SO history because it is not invoicing the existing SO line, instead it adds a new one. I'm not able to use .CopyLinesFromSalesOrder because a new order isn't committed to memory enough to invoice, I get the messaging "invoice has no lines and zero amounts". So I'm looping the lines of the SO and writing to the invoice which works perfect except for the OrderLineKey. I'm hoping someone will see something I'm missing! 

    'dim variables
    SONum = "" :  quantityShipped = 0 : ItemCode = "" : ItemDesc ="" : QtyOrd =0 : UnitPrice = 0 : retVal = 0 : Whse = "" : OrderLineKey = "" : HistorySeq = "" : PromiseDate = "" : InvLineKey = "" : InvoiceNo = "" : nextInv = "" : orderInv = ""
    'Get Sales Order Number
    retVal = oBusObj.GetValue("SalesOrderNo$", SONum)
    'Set objects
    Set oInvoice = oSession.AsObject(oSession.GetObject("SO_Invoice_bus"))
    Set oOrder = oSession.AsObject(oSession.GetObject("SO_SalesOrder_svc"))
    Set oInvLines = oSession.AsObject(oInvoice.Lines)
    'Establish batch
    retVal = oInvoice.SelectNewBatch("12345", "N", "For Drop Ship Invoices")
    retVal = oInvoice.GetNextInvoiceNo(nextInv)
    
    'Create new invoice
    	retVal = oInvoice.SetKey(nextInv)
    	
    'Set sales order
    	retVal = oInvoice.SetValue("SalesOrderNo$", SONum)
    	
    'Get lines from order
    		set oLines = oSession.AsObject(oBusObj.Lines)
    		retVal = oLines.MoveFirst()
    		do until oLines.EOF
    		    retVal = oLines.GetValue("ItemCode$", ItemCode)
    		    retVal = oLines.GetValue("ItemCodeDesc$", ItemDesc)
    		    retVal = oLines.GetValue("QuantityOrdered", QtyOrd)
    		    retVal = oLines.GetValue("UnitPrice", UnitPrice)
    		    retVal = oLines.GetValue("WarehouseCode$", Whse)
    		    retVal = oLines.GetValue("LineKey$", OrderLineKey)
    		    retVal = oLines.GetValue("PromiseDate$", PromiseDate)
    		    HistorySeq = right(("00000000000000" + OrderLineKey),14)
    
                         'Write values to invoice
    		    retVal = oInvLines.AddLine()
    		    retval = oInvLines.SetValue("ItemCode$", ItemCode)
    		    retval = oInvLines.SetValue("ItemCodeDesc$", ItemDesc)
    		    retVal = oInvLines.GetValue("LineKey$",InvLineKey)
    		    retVal = oInvLines.GetValue("InvoiceNo$",InvoiceNo)
    		    retval = oInvLines.SetValue("OrderWarehouse$", Whse)	
    		    retval = oInvLines.SetValue("SOHistoryDetlSeqNo$", HistorySeq)
    		    retval = oInvLines.SetValue("PromiseDate$", PromiseDate)
    		    retval = oInvLines.SetValue("AppliedSalesOrderNo$", SONum)
    		    retval = oInvLines.SetValue("OrderLineKey$", OrderLineKey)
    		    retval = oInvLines.SetValue("QuantityShipped", QtyOrd)
    		    retval = oInvLines.SetValue("QuantityOrdered", QtyOrd)
    		    retval = oInvLines.SetValue("UnitPrice", UnitPrice)
    		    retVal = oInvLines.Write()
    
    		retVal = oLines.MoveNext()
    		Loop 'oLines
    		
          retVal = oInvoice.Write()


    ------------------------------
    Kate Krueger
    Consultant
    MicroAccounting
    214.800.5555x553
    ------------------------------


  • 2.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-06-2024 18:22

    Don't SetValue on ItemCode to start the new invoice line... set the OrderLineKey... which should pull in the ItemCode. 

    Think about how VI imports of invoice lines work, when invoicing an SO.  You don't import the fields that should flow from the SO lines.



    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    Toronto ON
    ------------------------------



  • 3.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-06-2024 18:39

    Thanks for your reply Kevin! I'm thinking this may be the same issue that I was having with CopyLinesFromSalesOrder. When I take out all the SetValue on Item Code, Qty Ordered, etc. I get the error message "The Item Code is Required, The Item Type is required" etc. If I re-open the sales order and run the script again it works perfectly to create the invoice. So I'm thinking the OrderLineKey won't write because at that moment in time, the sales order doesn't have an OrderLinekey written to the table. The script is running on table post-write but I don't think the sales order is all the way written. Is there a method to complete the write and re-open the sales order? 



    ------------------------------
    Kate Krueger
    Consultant
    MicroAccounting
    214.800.5555x553
    ------------------------------



  • 4.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-06-2024 18:49

    I'm thinking it might be that the SO is not in an editable state (allowing you to create an invoice from it) during the post-write event, blocking things at a higher level.  Did you check the retVal when setting the SO#?  Perhaps add debug comments script-line by script-line to see where in the line reading / writing that things are breaking down.



    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    Toronto ON
    ------------------------------



  • 5.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-06-2024 18:58

    This is what breaks my brain - when I add in the debugging, everything reports that it is happy - retVal = 1 across the board. and technically that is true, it is writing everything except it skips the OrderLineKey, debugging on that setvalue specifically is still 1.  I've wandered down some rabbit holes trying to just get that field to write!



    ------------------------------
    Kate Krueger
    Consultant
    MicroAccounting
    214.800.5555x553
    ------------------------------



  • 6.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-06-2024 19:11

    100%, you have to set OrderLineKey first.  Do not set the ItemCode.  Doing this will absolutely create a new line, unlinked to the SO... and you can't retroactively link a new line to an SO line.  The new line must start from the OrderLineKey. 

    Your first oInvLines.SetValue needs to be OrderLineKey.



    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    Toronto ON
    ------------------------------



  • 7.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-06-2024 19:39

    I appreciate your help! sadly that is what I have. I moved OrderLineKey to the top and it does create the invoice but unlinked to the sales order. I tested having OrderLineKey be the only setvalue line but that gave me all the "item code is required" errors.  

    		    retVal = oInvLines.AddLine()
    		    retval = oInvLines.SetValue("OrderLineKey$", OrderLineKey)
    		    retval = oInvLines.SetValue("ItemCode$", ItemCode)
    		    retval = oInvLines.SetValue("ItemCodeDesc$", ItemDesc)
    		    retVal = oInvLines.GetValue("LineKey$",InvLineKey)
    		    retVal = oInvLines.GetValue("InvoiceNo$",InvoiceNo)
    		    retval = oInvLines.SetValue("OrderWarehouse$", Whse)	
    		    retval = oInvLines.SetValue("SOHistoryDetlSeqNo$", HistorySeq)
    		    retval = oInvLines.SetValue("PromiseDate$", PromiseDate)
    		    retval = oInvLines.SetValue("AppliedSalesOrderNo$", SONum)
    		    retval = oInvLines.SetValue("QuantityShipped", QtyOrd)
    		    retval = oInvLines.SetValue("QuantityOrdered", QtyOrd)
    		    retval = oInvLines.SetValue("UnitPrice", UnitPrice)
    		    retVal = oInvLines.Write()


    ------------------------------
    Kate Krueger
    Consultant
    MicroAccounting
    214.800.5555x553
    ------------------------------



  • 8.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-07-2024 15:16

    @Kate Krueger - You mentioned:

    >> The script is running on table post-write but I don't think the sales order is all the way written.

    Correct. Unfortunately the lines have not been physically written to SO_SalesOrderDetail when Header PostWrite fires off. The lines data in still in a memory file. That is why when you revisit the order, the script does work. However, on an existing order if you add / modify / delete a line, those changes in the line will not get reflected when your PostWrite runs as it will work on the last copy of the lines you had.

    Here are your options in order of definitely works to hmm maybe works. You may have tried some of this already:

    1. Create a small mod as it can position itself to run after the Sage WRITE has completed. I've done that for this very reason.
    2. Place your working script (a version that uses CopyLinesFromSalesOrder in another place) in some place other than S/O Entry screen. I know that defeats the purpose of the convenience of S/O Entry.
    3. Create a Customizer button in S/O Entry but define it as a Perform Logic button. That pvx code can click the Accept button for you and keep on chugging to then create the Invoice.
    4. Create a Customizer script button in S/O Entry that will conditionally:
      rV = oScript.InvokeButton("BT_ACCEPT") 'Let us hope the script continues to run after this.
      'If script continues to execute,
      'Now create the invoice if CurrentInvoiceNo$ is blank and prior to that you would do all other GetValue's you need.
      'Be sure to use CopyLinesFromSalesOrder when creating the invoice. Don't do all the manual lines work.

    Hope that helps.



    ------------------------------
    Alnoor Cassim
    Accounting Systems, Inc. (ASI)
    ------------------------------



  • 9.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-09-2024 13:28

    Okay - closing the loop on this thread...sort of. I took @Alnoor Cassim's idea of having the script running on a different object/event and have a Frankenstein's monster routine that is 99% working. I added a UDT that is holding sales order numbers that need these invoices created (drop ship orders). I added a BOI script that independently goes through Sales Order bus and finds drop ship orders and writes the order number to the UDT. A post-write script on the UDT takes the order number and creates the invoice. This works except for one flaw - the last order added to the UDT does not successfully create the invoice. It stays in the UDT and is caught the next time the BOI is run but it is bothering me and I'll still probably chase that squirrel. But thank you for the help and I'm much closer!  



    ------------------------------
    Kate Krueger
    Consultant
    MicroAccounting
    214.800.5555x553
    ------------------------------



  • 10.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-09-2024 13:36

    Suggestion:

    Post-write on the UDT, to create the invoices.

    Scheduled VI job (running however often you wish) to import new SO (that meet your criteria) into the UDT.



    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    Toronto ON
    ------------------------------



  • 11.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-09-2024 13:48

    Crazy thought here and I know you're too far gone into this already, but instead of triggering a script off the UDT, maybe you can:

    • Have your BOI directly create the invoice once it IDs a drop ship order instead of relying on the UDT postwrite? If that works, then use Windows Task Scheduler to auto-run the BOI.
    • Optional: At the end of your BOI write the SONum to the UDT to indicate the invoice for it was created.
    • Optional: At the beginning of your BOI check the UDT and skip invoice creation if your SO was found. But maybe you don't want that for situations where a SO is backordered and it's time to re-ship the backordered qtys.


    ------------------------------
    Alnoor Cassim
    Accounting Systems, Inc. (ASI)
    ------------------------------



  • 12.  RE: Script Sales Order to Invoice - OrderLineKey won't cooperate

    Posted 09-09-2024 14:13

    Nothing is too crazy! I did try to have the BOI create the invoice, it was struggling with moving through the UDT. It would successfully get a line from the UDT and then just die. The BOI would just close, no indication as to why. It did the same thing if I started at the end of the table and moved up or at the beginning and moved down.  I ended up getting frustrated and went the path you see here. 



    ------------------------------
    Kate Krueger
    Consultant
    MicroAccounting
    214.800.5555x553
    ------------------------------