Link PO Detail to SO Detail

SOLVED

Trying to update the SO Detail line after a receipt of goods is updated for a PO.  I have this script set in PO Detail on a table-postwrite but the first time I ran it the SO udf did not update.  Does the PO Detail table get updated after a receipt of goods update (I assumed it would)?  Or is something off in this script (I used an outline from an old post trying to update the other way around SO to PO).

Dim sItemCodeCur
Dim sPO_QTYREC
Dim sSO_QTYREC
Dim sOrderStatus

sSalesOrderNo = ""
sItemCode = ""
sItemCodeCur = ""
sPO_QTYREC = ""
sSO_QTYREC = ""
sOrderStatus = ""

retVal = oBusObj.GetValue("SalesOrderNo$",sSalesOrderNo)

if sSalesOrderNo <> "" then

            retVal = oBusObj.GetValue("ItemCode$",sItemCode)

            retVal = oBusObj.GetValue("UDF_QTY_RECEIVED$",sPO_QTYREC)

            if sPO_QTYREC <> "" then

                        sSO_QTYREC = sPO_QTYREC

                        oSalesOrder = oSession.GetObject("SO_SalesOrder_bus")

                        if oSalesOrder <> 0 then

                                    Set oSalesOrder = oSession.AsObject(oSalesOrder)

                        end if

                        retVal = oSalesOrder.SetKeyValue("SalesOrderNo$", sSalesOrderNo)

                        retVal = oSalesOrder.SetKey()

                        if retVal = 1 then

                                    retVal = oSalesOrder.GetValue("OrderStatus$",sOrderStatus)

                                    Set oSOLine = oSession.AsObject(oSalesOrder.Lines)

                                    retVal = oSOLine.MoveFirst()

                                    do until oSOLine.Eof

                                                retVal = oSOLine.GetValue("ItemCode$",sItemCodeCur)

                                                if sItemCode = sItemCodeCur AND sOrderStatus = "N" then

                                                            retVal = oSOLine.SetValue("UDF_QTYREC",sSO_QTYREC)

                                                            retVal = oSOLine.Write()

                                                            retVal = oSalesOrder.Write()

                                                end if

                                     retVal = oSOLine.MoveNext()

                                    loop

                        end if

                        retVal = oSession.DropObject("SO_PurchaseOrder_bus")

            end if

end if

  • 0

    I fixed a format error for the sPO_QTY & sSO_QTY, they should of been numeric, doesn’t work still though.

  • 0 in reply to jland47

    The PO does get updated for sure, but sometimes Sage does this kind of thing in a way that doesn't trigger event scripts.  Make sure your script is firing before focusing on syntax.

    Also, I had major problems opening new objects during a journal update (wanting to update freight amount on an SO when an invoice was posted) and basically it corrupted the entire journal update.  Be sure to test thoroughly.

  • 0 in reply to Kevin M

    Your journal update concern made me rethink the event trigger.  Thought if I could tie the script to the "PO_ReceiptDetail" table it would be safer and I wouldn't have to wait until the receipt update to update the SO line.  I entered message boxes under each Get Value and did not get any pop ups which means it is not even firing off.  I've tried column post validate/pre-validate (on QuanitityReceived) and table-prewrite and postwrite.  Maybe should do on SalesOrderNo$ ?

    Dim sSalesOrderNo
    Dmi sItemCode
    Dim sItemCodeCur
    Dim nPO_QTYREC
    Dim nSO_QTYREC
    Dim sOrderStatus
    
    sSalesOrderNo = ""
    sItemCode = ""
    sItemCodeCur = ""
    nPO_QTYREC = ""
    nSO_QTYREC = ""
    sOrderStatus = ""
    
    retVal = oBusObj.GetValue("SalesOrderNo$",sSalesOrderNo)
    retVal = oSession.AsObject(oSession.UI).MessageBox("sSalesOrderNo = " & sSalesOrderNo)
    
    if sSalesOrderNo <> "" then
    
    			retVal = oBusObj.GetValue("ItemCode$",sItemCode)
    			retVal = oSession.AsObject(oSession.UI).MessageBox("sItemCode = " & sItemCode)
    			retVal = oBusObj.GetValue("QuantityReceived$",nPO_QTYREC)
    			retVal = oSession.AsObject(oSession.UI).MessageBox("nPO_QTYREC = " & nPO_QTYREC)
    			if nPO_QTYREC <> "" then
    
    						nSO_QTYREC = nPO_QTYREC
    
    						oSalesOrder = oSession.GetObject("SO_SalesOrder_bus")
    
    						if oSalesOrder <> 0 then
    
    									Set oSalesOrder = oSession.AsObject(oSalesOrder)
    
    						end if
    
    						retVal = oSalesOrder.SetKeyValue("SalesOrderNo$", sSalesOrderNo)
    
    						retVal = oSalesOrder.SetKey()
    
    						if retVal = 1 then
    
    									retVal = oSalesOrder.GetValue("OrderStatus$",sOrderStatus)
    									retVal = oSession.AsObject(oSession.UI).MessageBox("sOrderStatus = " & sOrderStatus)
    									Set oSOLine = oSession.AsObject(oSalesOrder.Lines)
    
    									retVal = oSOLine.MoveFirst()
    
    									do until oSOLine.Eof
    
    												retVal = oSOLine.GetValue("ItemCode$",sItemCodeCur)
    												retVal = oSession.AsObject(oSession.UI).MessageBox("sItemCodeCur = " & sItemCodeCur)
    												if sItemCode = sItemCodeCur AND sOrderStatus = "N" Or "O" then
    
    															retVal = oSOLine.SetValue("UDF_QTYREC$",nSO_QTYREC)
    
    															retVal = oSOLine.Write()
    
    															retVal = oSalesOrder.Write()
    
    												end if
    
    									retVal = oSOLine.MoveNext()
    
    									loop
    
    						end if
    
    						retVal = oSession.DropObject("SO_SalesOrder_bus")
    
    			end if
    
    end if

  • 0 in reply to jland47

    Good Morning  ,

    I think your best bet is to execute this script on the post-write() or pre-write() of PO_ReceiptDetail, but you'll likely want to do some checking on when you fire this logic.  Probably only during data entry, and probably only for new receipts or you'll have to track changes to the quantity.  

    If you are not getting it to fire, I would try the obvious first check Company Maintenance for 'external access' flag (this still bites me from time to time).  If not that I would try just re-updating the script event or deleting and adding it again, sometimes the compiled script gets messed up.

    I did notice a couple of small things in your script as well, but not sure that it would change the outcome.

    (edits in your script)

    * In your loop to find the proper SO Line to update you need to set another looping variable and an early exit for your loop.  Although, by accident this loop will likely stop because you are committing to the sales order early (ln-58) you should likely set a variable called 'lineUpdated' or something and add that to the condition of your loop.  Then use it again after the end of the loop to conditionalize the write to the sales order.

    * you are dropping the incorrect object at the end of your script

  • 0 in reply to jepritch

    Good information, yes at first I didn’t have the “allow external…” checked off, that is now checked off.  Question on the loop, do you get the value of the line update after the line session is opened or when I’m getting the value of the item code, I think it goes before line 52.  or would line 52 cover the conditionalizing of the write to the line.  I have the “GetObject” as SO_SalesOrder_bus, do you not drop the same object?

  • 0 in reply to jland47

    I was just talking about another variable that we could use to exit the loop early after we found the line to update.  See the snippet of code below.

    ...
    
        retVal = oSOLine.MoveFirst()
        
        lineUpdated = 0 ' initialize new indicator
    
    	do until (oSOLine.Eof and lineUpdated=0)
    
    		retVal = oSOLine.GetValue("ItemCode$",sItemCodeCur)
    		retVal = oSession.AsObject(oSession.UI).MessageBox("sItemCodeCur = " & sItemCodeCur)
    
    		if sItemCode = sItemCodeCur AND sOrderStatus = "N" Or "O" then
    			retVal = oSOLine.SetValue("UDF_QTYREC$",nSO_QTYREC)
    			retVal = oSOLine.Write()
    			
    			lineUpdated = 1 ' trip flag
    		end if
    
    		retVal = oSOLine.MoveNext()
    
    	loop
    	
    	if lineUpdated=1 then ' only write if we found a match
    	    retVal = oSalesOrder.Write()
    	end if
    	
    	...

    My apologies you are correct about the DropObject(), I misread the object name for some reason I thought it said "SO_PurchaseOrder_bus".  Friday eyes I guess Slight smile

    Hope this helps

    E

  • 0 in reply to jepritch

    My first post of code had the wrong object, so your eyes are still sharp!  I got the script to fire on a table-post write.  All values return correct values except the variable "nPO_QTYREC".  The field "QuantityReceived$" in the PO_Receipt Details table has been giving me trouble, I was going to link an UDF with this field but it is not available to link, so I am assuming this has something to do with me not getting a value in the script.

    Dim sPurchaseOrderNo
    Dim sSalesOrderNo
    Dim sItemCode
    Dim sItemCodeCur
    Dim nPO_QTYREC
    Dim nSO_QTYREC
    Dim sOrderStatus
    
    sPurchaseOrderNo = ""
    sSalesOrderNo = ""
    sItemCode = ""
    sItemCodeCur = ""
    nPO_QTYREC = ""
    nSO_QTYREC = ""
    sOrderStatus = ""
    
    retVal = oBusObj.GetValue("SalesOrderNo$",sSalesOrderNo)
    rVal = oSession.AsObject(oSession.UI).MessageBox("sSalesOrderNo = " & sSalesOrderNo)
    
    if sSalesOrderNo <> "" then
    
    			retVal = oBusObj.GetValue("ItemCode$",sItemCode)
    			rVal = oSession.AsObject(oSession.UI).MessageBox("sItemCode = " & sItemCode)
    			retVal = oBusObj.GetValue("QuantityReceived$",nPO_QTYREC)
    			rVal = oSession.AsObject(oSession.UI).MessageBox("nPO_QTYREC = " & nPO_QTYREC)
    			if nPO_QTYREC <> "" then
    
    						nSO_QTYREC = nPO_QTYREC
    
    						oSalesOrder = oSession.GetObject("SO_SalesOrder_bus")
    
    						if oSalesOrder <> 0 then
    
    									Set oSalesOrder = oSession.AsObject(oSalesOrder)
    
    						end if
    
    						retVal = oSalesOrder.SetKeyValue("SalesOrderNo$", sSalesOrderNo)
    
    						retVal = oSalesOrder.SetKey()
    
    						if retVal = 1 then
    
    									retVal = oSalesOrder.GetValue("OrderStatus$",sOrderStatus)
    									rVal = oSession.AsObject(oSession.UI).MessageBox("sOrderStatus = " & sOrderStatus)
    									Set oSOLine = oSession.AsObject(oSalesOrder.Lines)
    
    									retVal = oSOLine.MoveFirst()
    
    									lineUpdated = 0 ' initialize new indicator
    
    									do until (oSOLine.Eof and lineUpdated=0)
    
    												retVal = oSOLine.GetValue("ItemCode$",sItemCodeCur)
    												rVal = oSession.AsObject(oSession.UI).MessageBox("sItemCodeCur = " & sItemCodeCur)
    												
    												if sItemCode = sItemCodeCur AND sOrderStatus = "N" Or "O" then
    
    													retVal = oSOLine.SetValue("UDF_QTYREC$",nSO_QTYREC)
    
    													retVal = oSOLine.Write()
    
    													lineUpdated = 1 'trip flag
    												
    												end if
    
    												retVal = oSOLine.MoveNext()
    
    									loop
    								
    									if lineUpdated=1 then ' only write if we found a match
    	    									
    										retVal = oSalesOrder.Write()
    
    									end if
    	
    									retVal = oSOLine.MoveNext()
    
    									
    						end if
    
    						retVal = oSession.DropObject("SO_SalesOrder_bus")
    
    			end if
    
    end if

  • 0 in reply to jland47

    Don't use a "$" for numeric fields.  That is for string fields (and dates) only.

    retVal = oBusObj.GetValue("QuantityReceived",nPO_QTYREC)

    Edit:

    And initialize your numeric variables to 0 instead of "".

  • 0 in reply to Kevin M

    That worked, and now further issues down the script, all message boxes appear correctly however the last one stays on the screen, if I click "ok" it stays up. Also now that I am picking up a value on the nPO_QTYREC, I am not getting messages for the other lines on the Receipt, it seems like it is going to the SO.  The last message box that it gets stuck on is "sItemCodeCur" and it is for the last line on the Receipt, so does that mean it is reading all the lines on the Receipt and I just have a loop wrong?

  • +1 in reply to jland47
    verified answer

    I think I gave you the wrong condition for your loop..  You want to quit the loop when you hit the end of the lines or if linesUpdated=1.   I usually do while loops so I gave you the wrong condition.

    So I think the loop should be:

    ...

    do until (oSOLine.Eof or lineUpdated=1)

    ...