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.
To date and print an invoice using Get functions
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).
Figure 8.15. Script names should indicate what the script will do.
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.
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).
Figure 8.18. The Invoice Date field should be specified in both the IsEmpty and the Set Field steps.
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).
Figure 8.19. The View drop-down menu lets you choose calculation functions by category.
11 .In the list of Get functions, double-click the Get(CurrentDate) function (Figure 8.20). Click OK.
Figure 8.20. The Get(CurrentDate) function returns the computer's current date.
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).
Figure 8.21. Since this step is almost the same as the previous, duplicating it is easier than re-creating it.
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).
Figure 8.22. Click the Specify button to edit a field you've previously specified.
14 .When the Specify Field dialog box appears, choose the field (in our example, Overdue Date). Click OK (Figure 8.23).
Figure 8.23. The duplicated step needs to have a new target field chosen.
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.
Figure 8.24. The Overdue Date field is set to 30 days after the current date.
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).
Figure 8.25. The duplicated and edited Set Field step.
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).
Figure 8.26. Select End If to add Go to Layout to the end of the script.
19 .In the Script Step Options section, choose Invoice from the Specify drop-down menu (Figure 8.27).
Figure 8.27. The script switches to the Invoice layout before printing.
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).
Figure 8.28. With the Options unchecked, the user will be prompted to choose the Print Setup options when printing.
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).
Figure 8.29. The last step in this script will return you to the layout where the script started.
This last part of the script returns you to the original layout.23 .Click OK twice to finish. TipThere'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.
Figure 8.30. Insert Current Date is an alternative to Set Field with a Get function.