If you recall, we started coding a handy pop-up notification like the one below:
Now we’re going to take this one step further and have our code check that freight has been entered for customers with a Customer Type of F (found on the Additional tab in Customer Maintenance).
Step 1: Open Custom Office User Defined Script Maintenance.
Step 2: Use the lookup glass to locate your “message_box” script that we created during the first installment.
Step 3: Write the code that will display a message box if freight is 0. In later steps we’ll also grab the customer type and determine whether that customer needs to be charged freight, but let’s tackle this in phases.
We’ll start with the code below. I’ll explain what it all does as we go along. For now, type up the code verbatim into the Script box. It should look like this:
Now let’s break it down.
We discussed initializing or preparing variables for use in the first installment. We’ll do that initializing with both the retVal and the Freight variable. (See first installment for more info on the use of retVal, also.) We’ll then execute a line of code that grabs the value of the “Freight Amount” field in SO Entry on the Totals tab. That line of code is retVal = oBusObj.GetValue(“FreightAmt”, Freight). The GetValue “method”, as it’s called, grabs the value in a data field of your choice which you enclose in double quotes (FreightAmt in this case). It then places that value into a variable that you can use in your code (the variable Freight).
Next, we must check the value of our Freight variable. Is it 0? Or even less than zero, due to a typo, perhaps? We do this with an If…Then…Else statement. We’re just going to use the If…Then… part, though. As with most VBScript stuff, you can find more info online about If…Then…Else statements with a web search.
Our If…Then… statement is going to ask, is the freight amount less than or equal to zero? That looks like this: if Freight <= 0 then. If so, we tell Sage to throw an error message on screen and prevent the user from saving the order before the problem is rectified. We do that with this line of code: retVal = oScript.SetError(“Freight is required for this customer.”)
This error message can say anything you want it to say. Just change the text enclosed in double quotes. E.g. retVal = oScript.SetError(“Say something else.”) Another tidbit, the part after the apostrophe, “throw an error,” is a comment. You should get into the habit of adding comments to your code. You do that by using an apostrophe pretty much anywhere and typing out little love notes to yourself and those that come after you.
Finally, because we could add as much code as we need after the if Freight <= 0 then, the End If statement just tells the program we’re done with our If statement.
We actually have a fully functioning script at this point and could test it out as is, but we’re going to take this a step further.
Step 4: Add code to check the customer type (on the Additional tab in Customer Maintenance) and throw an error only for customer type “F.”
The highlighted lines below are the new additions you’ll need to make to your code. Let’s review them together.
First, we’re prepping a couple of variables for use again. The oCust variable is a numeric value which we’ll use to store an “object.” (That’s a program in Sage, like Customer Inquiry.) The sCustType variable was initialized as a string (text) value and that’s why we have the double quotes ( = “” ), which is an empty string, rather than = 0. Next, we assign an object (customer info, basically) to the oCust variable with the line: Set oCust = oSession.AsObject(oBusObj.GetChildHandle(“CustomerNo”)). Because we will be executing this code in Sales Order Entry (see previous installment), we will already have a customer’s data loaded. This line of code says: Grab the data for this customer (for which we are entering this order).
You saw the GetValue method earlier when we retrieved the Freight Amount. Now we’ll use it to get the customer’s type. We do that with: retVal = oCust.GetValue(“CustomerType$”, sCustType). Note that we’re now calling oCust rather than oBusObj. If we had called oBusObj and asked it to retrieve the customer type, we would have received no data. That’s because the customer type is not available on the Sales Order Header, which is what oBusObj is presently pointing to being that we’re running this script in Sales Order Entry.
Finally, we’ll add another If…Then…Else… statement. We’re checking that the customer type is “F” with the line: if sCustType = “F”then. Note that we want place this line before the code that grabs the freight amount and checks if it’s <= 0. Finally, we must wrap it up with another end if.
Step 5: Compile your code.
When you’re done typing out the code, click “Accept” to save the script and close the UDS Maintenance window. (You must click “Accept”, or your code will not be saved.) Then click “Compile, “ then “Compile” again in the next screen that opens, and then close any windows remaining open.
Step 6: Test your message box script!
In Customer Maintenance, select a customer to test with. On the Additional tab, enter “F” in the Customer Type box.
Enter a sales order for your customer and try to save it with $0 in the Freight Amount field. You should get a message box like the one below and should not be able to save your order.
Congrats! You are now a scripting initiate! Stay tuned to future installments so you can use to add to your repertoire!