Update SO field from PO button script

SOLVED

I'm trying to write a button script that will copy a single line from a SO and "pull" it into the open PO. I've done this successfully for SO (copying from Invoice or SO/Quote History), but with PO, I'd like to be able to "tie" the SO and PO together when copying the line. I can easily put the SO# into the PO line, but I'm stuck when trying to add the PO# to the SO line (since the SO isn't open and the script is running from the PO).

The following snippet results in an "Error 65: Window element does not exist or already exists"

Set oSO = oSession.AsObject(oSession.GetObject("SO_SalesOrderDetail_bus"))
retVal = oSO.SetKeyValue("SalesOrderNo$",srcOrdNum)
retVal = oSO.SetKeyValue("LineKey$",sLineNum)
retVal = oSO.Find()

retVal = oSO.SetValue("PurchaseOrderNo$",sPONum)
retVal = oSO.Write()

Is this even possible?

Top Replies

  • +1
    verified answer

    Yes,  this should be possible however, you cannot modify the Sales Order Detail business object independently like that.  You must always go through the header object.

    You should put more error checking in than I have done below and I haven't tested this, but something like this should work.   

    Set oSO = oSession.AsObject(oSession.GetObject("SO_SalesOrder_bus"))
    Set oSOLines = oSession.AsObject(oSO.Lines)

    retVal = oSO.SetKey(srcOrdNum)
    If retVal<>0 Then
        soEditKey = oSOLines.GetEditKey(sLineNum)
        retVal = oSOLines.EditLine(soEditKey)
        retVal = oSOLines.SetValue("UDF_PurchaseOrderNo$", sPONum) ' I assume this is a UDF on the SO Line?
        retVal = oSOLines.Write() ' update line with the change

        retVal = oSO.Write() ' commit changes to the order
    End If

    Hope this helps

    Elliott

  • 0 in reply to jepritch

    Elliot,

    Thank you so much! That makes sense that you can't edit the Detail file directly, and I've never done this particular thing before (edit a business object I'm not currently in). Also, the "PurchaseOrderNo$" field is a stock Sage field. It gets populated automatically when doing the "Generate PO" button in Sales order, or from the "Generate PO from SO" utility under Purchase Order. 

    Thanks again for your help!!

  • 0 in reply to chuntley87

    (Re-purposing a stock Sage field to do something else can have unexpected results... UDF's are better for scripted custom purposes).

  • 0 in reply to Kevin M

    Kevin,

    Thanks for the heads up. In this case, I want the built in Sage behavior to happen to this field. The script I'm writing is to add a SO line to a PO. I want to populate the "PO Number" field in the SO, so that if someone was to click "Generate PO" on that SO, that line would not get put on a new PO, thus preventing double ordering. There's no way to do that with a UDF, that I'm aware of. 

    *edit: That field isn't only an automatically populated field. It is user editable for the exact purpose of the above explanation.

  • 0 in reply to jepritch

    Nope on the UDF, I suspect he wants to manually enter the sales order number in the Sales Order Number field  in the purchase order line and have it automatically write out to the Sales Order.  This field is used when you automatically create purchase orders but you can manually select a sales order