Trying to update Sales Order Header UDF from Invoice Entry getting error

SOLVED

I'm trying to update a user defined field in the Sales Order Header table using a script from Sales Order Invoice entry from the Table - PreWrite event. The script saves the USERCODE from the session object into a UDF in the Invoice and then tries writing the USERCODE back into a duplicate UDF in the Sales Header. When the oSalesSvc.WRITE() is called I get an error "*memory* does not have correct permissions". I've tried everything I can think of. Anybody see what i could do differently?

Here is the script:

tmpValue = ""

tmpSalesOrderNo = ""

' only update UDF's if the UDF is empty

retVal = oBusObj.GetValue("UDF_USERCODE$",tmpValue)
retVal = oBusobj.GetValue("SalesOrderNo$",tmpSalesOrderNo)

IF tmpValue = "" THEN

retVal = oBusObj.SetValue("UDF_USERCODE$",oSession.UserCode)

Set oSalesSvc = oSession.AsObject(oSession.GetObject("SO_SalesOrder_bus"))

retVal = oSalesSvc.ReadRec(tmpSalesOrderNo)

retVal = oSalesSvc.SetValue("UDF_USERCODE$",oSession.UserCode)

retVal = oSalesSvc.Write()

End If

  • 0

    Hi @RonT

    You may want to the latter part of your code.   You also may want to do this as part of a Post-Write, because the Invoice object updates the sales order currently, so you may not be able to modify this until after the invoice record is written.

    IF tmpValue = "" THEN

        retVal = oBusObj.SetValue("UDF_USERCODE$", oSession.UserCode)

        Set oSalesSvc = oSession.AsObject(oSession.GetObject("SO_SalesOrder_bus"))

        retVal = oSalesSvc.SetKey(tmpSalesOrderNo)

        IF retVal = 1 Then

             retVal = oSalesSvc.SetValue("UDF_USERCODE$",oSession.UserCode)

             retVal = oSalesSvc.Write()

        End If

    End If

  • 0 in reply to jepritch

    That definitely helped but now the SetKey is failing and the lastErrorMsg from the oSalesSvc object is 'The order is currently being invoiced.' Argh!.

    So close!! Any other thoughts??

  • 0 in reply to RonT
    verified answer

    The problem is that obviously we (Sage) don't let sales orders be modified while they are being invoiced.  So, we could do a couple of different things.

    1) We could move your script to the PRE-Validate of the SalesOrderNo itself.  This would then be able to modify the order because the Invoice # is not on the order until after the validate is done.  Only problem with this is cleaning up your UDF if the invoice is deleted or cancelled or the Order # entered is invalid.  But you could probably clean that up in a post-validate script if need be.

    2) Instead of a UDF you could create a UDT that has Order #, Invoice #, and User or something like that, and update a record in that UDT table during this script instead.  Depending on where you are using this UDF.  This is probably clunkier, but would definitely have full access to whatever you want or need to do with this.

    Hope that helps.

    Elliott

  • 0 in reply to jepritch

    Thanks jepritch,

    I'll try those options. I figured there must be some way to do this. I might be able to do it on the SetDefaults event also. That is where I moved the code to populate the UDF in the Sales Invoice header table. The purpose of this is to make the User who created the Shipment Entry available on the selection form for Picking Sheet and Packing List printing. It works great since the UDF's are added to the grid automatically, if we can only get the UserCode into the UDF. :-)

  • 0 in reply to RonT

    The PreValidate event of the SalesOrderNo field option worked. I realized after my last post that the SetDefaults option was not an option. Duh!

    Thanks so much!