Combining a Get function with a conditional step, we can create a script that automatically adds the current date to an invoice, calculates the net 30 date when it will become overdue, then prints the invoice. Before you create the script, remember to set the Print/Page Setup options for the invoice.
1 .Choose Scripts > ScriptMaker (Control+Shift+S/Command+Shift+S). When the Define Scripts dialog box appears, click New.
2 .When the Edit Script dialog box appears, type a descriptive name for the script in the Script Name text box (in this case Print Invoices) (Figure 8.15).
3 .
Under the Control category in the script list on the left, double-click If.
4 .In the Script Step Options section, click the Specify button to bring up the Specify Calculation dialog box. In the function list on the right, double-click the IsEmpty function (Figure 8.16). IsEmpty checks whichever field you put in the parentheses to see if there is data in it.
5 .In the field list on the left, double-click Invoice Date. Click OK to close the Specify Calculation dialog box (Figure 8.17).
This section of the script checks the Invoice Date field to see if you've already entered anything in it.
6 .Scroll down to the Fields category in the script step list on the left and double-click Set Field.
7 .In the Script Step Options section, click the "Specify target field" check box.
8 .When the Specify Field dialog box appears, choose the same field name you used in the If step. Click OK (Figure 8.18).
9 .
In the Script Step Options section, click the Calculated result: Specify button.
10 .
When the Specify Calculation window appears, click the View drop-down menu, scroll down, and choose Get functions (Figure 8.19).
11 .In the list of Get functions, double-click the Get(CurrentDate) function (Figure 8.20). Click OK.
This portion of the script will check the computer's settings and enter the current date in the Invoice Date field.
12 .
The Set Field step in the script-assembly list should be highlighted. Click the Duplicate button. A copy of the step appears below the original (Figure 8.21).
13 .
We need to edit this step so that it will change the data of a different field. In the Script Step Options section, click the Specify target field: Specify button (Figure 8.22).
14 .When the Specify Field dialog box appears, choose the field (in our example, Overdue Date). Click OK (Figure 8.23).
The second Set Field step now targets a different field.
15 .Click the Calculated result: Specify button.
16 .When the Specify Calculation window appears, click inside the formula box just after Get(CurrentDate) and type +30 (Figure 8.24). Click OK.
This script step checks the status of the current date, adds 30 days to it, and inserts that date into the Overdue Date field (Figure 8.25).
17 .In the script-assembly list, click to select the End If step.
18 .Scroll down to the Navigation category in the script step list on the left, and double-click Go to Layout (Figure 8.26).
19 .
In the Script Step Options section, choose Invoice from the Specify drop-down menu (Figure 8.27).
This step switches to the Invoice layout.
20 .Scroll down to the Files category in the script step list on the left, and double-click Print Setup. Leave the options unchecked (Figure 8.28).
21 .In the left step window, double-click Print.
22 .
In the left step window under the Navigation category, double-click Go to Layout. Leave Specify set to "original layout" (Figure 8.29).
This last part of the script returns you to the original layout.
23 .Click OK twice to finish.
There's a step called Insert Current Date that some people use instead of Set Field and the Get(CurrentDate) function (Figure 8.30). But Insert Current Date requires that the field where the date is entered be on the current layout. Set Field is a better choice because it works even if the field is not on the layout. Good script design makes the fewest assumptions about the database's current state.