Shipping calculations

Our company has always charged a % of the order total for a shipping fee. I don't believe this is possible using shipping rate maintenance since I wouldn't want to have an enormous table with every possible price to the penny...

This has left me looking at creating a script to do this. I saw another post that mentioned this is easily done using a script, which sounds great.. I'm just having problems actually implementing it.

The post I read said to pass the TaxableAmt and NonTaxableAmt fields to the script, sum them, then multiply by our shipping rate. Math works out, that sounds right. Problem is the TaxableAmt and NonTaxableAmt fields show as "Display Only", and I cannot select them to "Return" to the script. That being said, I'm not what I would consider good at scripting... If I could get some help with this I'd greatly appreciate it.. Trying to apply 7% shipping with a $5 minimum charge.

Here's my script:

-----------------------------------------------------------------------------------------------------------------------------------------

If oSession.CompanyCode = "TST" then

retval = 0
nTaxableAmt = ""
nNonTaxableAmt = ""
nTotal = 0

retval = obusobj.getvalue("TaxableAmt", nTaxableAmt)
retval = obusobj.getvalue("NonTaxableAmt", nNonTaxableAmt)

if nTotal < 5 then
nTotal = 5.0
else
nTotal = (nTaxableAmt + nNonTaxableAmt * .07)
End If

retval = obusobj.setvalue("FreightAmt", nTotal)

End If

-----------------------------------------------------------------------------------------------------------------------------------------

When I press the button this is assigned to, nothing writes to the FreightAmt field. I know the script is firing because if I put this in it:

retval = oSession.AsObject(oSession.UI).MessageBox("Test")

It shows the dialog box... The script passes the syntax check just fine, just doesn't do anything else. Something else to note is, in User defined script maintenance, if I try to compile the script I get an error message that says "There are no user-defined script events defined for columns or tables." I'm not exactly sure what this is referring too. Is there something else I need in my script to make it a "script event"? I have two "similar" scripts doing different things that work just fine. Not sure what's up.

  • 0

    Please ignore the "" on nTaxableAmt = "" and nNonTaxableAmt = "". I've corrected these to = 0 to avoid a "type mismatch" error I was getting... Again, I'm not very good at this!

  • 0

    So, maybe I had some logic problems there..? I've updated the script to the following which seems to "work":

    If oSession.CompanyCode = "TST" then

    retval = 0
    nTaxableAmt = 0
    nNonTaxableAmt = 0
    nTotal = 0

    retval = obusobj.getvalue("TaxableAmt", nTaxableAmt)
    retval = obusobj.getvalue("NonTaxableAmt", nNonTaxableAmt)

    nTotal = ((nTaxableAmt + nNonTaxableAmt)*.07)

    if nTotal > 5 then
    nTotal = ((nTaxableAmt + nNonTaxableAmt)*.07)
    else
    nTotal = 5.0
    End If

    retval = obusobj.setvalue("FreightAmt", nTotal)

    retval = oSession.AsObject(oSession.UI).MessageBox(nTotal)

    End If

    However, this displays an error message saying "Wrong number of arguments or invalid property assignment: 'oSession.AsObject(...).MessageBox'". When I try to get rid of the dialog box entirely, the calculations no longer happen... What? Is its a pre-validate / post-validate thing...? Even if I change the message to say something like "Freight has been calculated.", it still breaks it. I'm confused...

  • 0 in reply to llevron20

    MessageBox displays text only.  Try retval = oSession.AsObject(oSession.UI).MessageBox(Cstr(nTotal))

  • 0 in reply to BadgerJerry

    Thanks, that seems to resolve the error message itself. It looks like it now displays the correct value in the message box, but doesn't push anything at all to the FreightAmt field.

    Also, I didn't even want the text box, that was simply in there as a means to see that it's doing something at all.

    Any idea why this is happening? The math seems to work out fine, but I can't get it to reliably write to the field unless I have a message box that errors out...

    And on a side note... when this does work with the error message box, it only calculates after I click the OK button on the error message. This also leaves the order total field incorrect. Do you know a line of code similar to what's in the "Recalc Price" button at the bottom of the sales order entry screen? I'd just like to execute this automatically at the end of my script.

  • 0 in reply to llevron20

    The FreightAmt may actually be set in your script, but then overwritten by an auto-freight calculation (based  on the ship code), with the error stopping the automatic calculation.  I've banged my head against that particular wall before.

    Invoke the button.

  • 0 in reply to Kevin M

    Kevin, that's a good thing to keep in mind, but I have "Use Shipping Code to Calculate Freight" set to No in Sales Order Options. This should keep it from automatically trying to calculate anything else. Any other thoughts? I feel about the same as you right now (banging my head against a wall).

  • 0

    Well, after some changes to the code and script setup I finally got this to work. It now calculates freight at the specified % while adhering to the minimum shipping requirement with no errors! Yay!