Like other fields, composites can have pre, change and post scripts for the entire field. In addition, the pre, change and post field scripts will be run for each component if the Call Component Scripts option is marked in the Data Type Definition window for the composite.
Note there is only one pre, change and post script for the composite field, not a pre, change and post script for each component of the field. If component scripts are enabled, the pre, change and post scripts for the composite field will be run once for each component of the composite and once for the composite as whole. The currentcomponent() function is used to determine which component the focus is in when the pre, change or post script is run.
The following table shows which scripts run and the value returned by the currentcomponent() function when a user moves into, through, and out of a three-component composite field.
User action |
Scripts run |
Value returned |
---|---|---|
Move into the first component. |
Pre script (for the entire field) Pre script (for the first component) |
0 1 |
Change the first component and tab into the second. |
Change script (for the first component) Post script (for the first component) Pre script (for the second component) |
1 1 2 |
Change the second component and tab into the third. |
Change script (for the second component) Post script (for the second component) Pre script (for the third component) |
2 2 3 |
Change the third component and move out of the composite. |
Change script (for the third component) Post script (for the third component) Change script (for the entire field) Post script (for the entire field) |
3 3 0 0 |
Component scripts for composites are often used to validate components of the composite as they are entered by the user. The following example is a change script for the Part Number composite field for which component scripts are called.
The first component of the Part Number field is the Vendor Number. The change script validates the Vendor Number component by verifying that the vendor exists in the Vendors table. If the vendor doesn’t exist, a warning will be displayed and the field will be restarted. If the vendor does exist, the remainder of the part number will be entered and the change script will attempt to retrieve the entry.
if (currentcomponent() = 0) then {Change script is run for the entire Part Number field.} {Retrieve the information about the part.} 'Part Number' of table Current_Inventory = 'Part Number' of window Inventory_Maintenance; change table Current_Inventory; if err() = OKAY then copy from table Current_Inventory; end if. else if (currentcomponent() = 1) then {Validate the Vendor component.} 'Vendor Number' of table Vendors = 'Part Number:Vendor Number'; get table Vendors; if err() <> OKAY then warning "This vendor hasn't been entered. Please select another."; restart field; end if; end if; end if;