Script to add line to SO_InvoiceDetail

We have steel surcharges due to market pricing that need to be added to invoices for certain parts (separate charge).  I have written a script that will add a line to the table when one of those items is entered.  It works, but I wanted another set of eyes on it for potential pitfalls or recommendations for improvement.  I'd love to post the script if someone can tell me how, as I haven't been able to figure it out on this new forum.

Thanks!

Jim

  • Ever thought of pasting in the response box?

  • in reply to BigLouie

    I thought there would be code tags that I would need to use to avoid public flogging.  Thank you for being so delicate :)  Here is the script:

    retVal = 0
    itemCode = ""
    qtyShipped = 0
    cat4 = ""
    scPrice = 0

    retVal = oBusObj.GetValue("QuantityShipped", qtyShipped)

    if CBool(qtyShipped) then

    oCIItem = oSession.GetObject("CI_ItemCode_svc") ' need this to get surcharge amount
    if oCIItem <> 0 then

    SET oCIItem = oSession.AsObject(oCIItem)

    end if

    retVal = oBusObj.GetValue("ItemCode$", itemCode) ' get current ItemCode
    retVal = oCIItem.SetKeyValue("ItemCode$", itemCode) ' filter and find oBusObj.ItemCode in CI_Item
    retVal = oCIItem.Find()
    retVal = oCIItem.GetValue("Category4$", cat4) ' surcharge stored here

    if len(cat4) > 0

    scPrice = CDbl(cat4)
    retVal = oBusObj.AddLine()
    retVal = oBusObj.SetValue("ItemCode$", "/SURCHARGE")
    retVal = oBusObj.SetValue("CommentText$", "Surcharges for "+itemCode)
    retVal = oBusObj.SetValue("QuantityOrdered", qtyShipped)
    retVal = oBusObj.SetValue("UnitPrice", scPrice)
    retVal = oBusObj.Write()

    end if

    end if

  • in reply to jcmeyer5

    Since you asked...

    1) The way you have your error checking on oCIItem is kind of pointless since you are not doing anything to handle the case of an error. Your script will just get a different error later when you try to access a member of oCIItem. I usually write it as:

    oCIItem = oSession.GetObject("CI_ItemCode_svc") ' need this to get surcharge amount

    if oCIItem = 0 then

       MsgBox oSession.LastErrorMsg

       Exit Sub

    end if

    set oCIItem = oSession.AsObject(oCIItem)

    ...

    Then if something happens (like user doesn't have correct permissions), the script exits cleanly instead of getting a runtime error.

    2) I think that you could get the CIItem object much more easily using oBusObj.GetChildHandle. So now you have:

    oCIItem = oBusObj.GetChildHandle("ItemCode") ' need this to get surcharge amount

    if oCIItem = 0 then

       MsgBox oSession.LastErrorMsg

       Exit Sub

    end if

    set oCIItem = oSession.AsObject(oCIItem)

    ...

    So there is now no need for:

    retVal = oBusObj.GetValue("ItemCode$", itemCode) ' get current ItemCode
    retVal = oCIItem.SetKeyValue("ItemCode$", itemCode) ' filter and find oBusObj.ItemCode in CI_Item
    retVal = oCIItem.Find()

    Just:
    retVal = oCIItem.GetValue("Category4$", cat4) ' surcharge stored here

    3) Hopefully you are doing some validation on Category4$ when it is entered. You could add a check to this script, but it is really better to have it correct when it is entered. Alternately, you could use a numeric UDF instead of one of the categories, then you don't have to worry about it.

  • in reply to dlech

    If anyone from Sage is listening, a code tag would be really nice, please. As you can see, it is a mess to read without one.

  • in reply to dlech

    Amen to the code tags!

    I see your point on the error checking.  Oversight on my part (as you had previously showed me that)

    The GetChildHandle() bit is interesting... had not seen that before.  Worked like a charm.

    I thought about a separate UDF for that surcharge.  I don't like using a text field either.

    Thanks!

    Jim

  • in reply to dlech

    Hey! Just wanted to let you guys know that we are listening and adding the option to highlight/tag code is in my list of things to do/add once we're settled :)  Thanks for the input!