Script Examples: Examine and Modify Data to be Used at Print Time
At print time, the data used for a field is usually pulled from the data map (MAP). This is done by getting the name (key) of the data map entry from the field (getFullyQualifiedKey) and then doing a lookup in the data map with this key to get the value. If the key does not exist, then the default value is used. If you configure the data for the field explicitly by calling setData, then what is configured is used and the data map and default data is not checked.
Sample Data
Refer to the following sample fields and data for the examples that follow.
FORMAT
Field |
Name |
FQN |
Default data |
---|---|---|---|
Field1 |
Text0001 |
/Body/Text0001 |
Undefined |
Field2 |
Text0002 |
/Body/Text0002 |
"text2DefaultData" |
Field3 |
Text0003 |
/groupedField |
Undefined |
Field4 |
Text0004 |
/groupedField |
Undefined |
Field5 |
Text0005 |
/groupedField |
Undefined |
MAP
Name (Key) |
Value |
---|---|
/Body/Text0001 |
"value1" |
/groupedField" |
"value2" |
Examine the Field Data Value to Print
The value to be used at print time for a field is based on conditions.
In the following example, field data has not been defined. A data map entry with the fully-qualified name of the field results in the value used at print time.
var field1 = getField("/Body/Text0001");
var dataToBePrintedForField1 = field1.getPrintingValue(MAP);
dataToBePrintedForField1 will have the value "value1"
In the following example, field data has not been defined. No entry exists in the data map for the fully-qualified name, so the default value is used for the field.
var field1 = getField("/Body/Text0002");
var dataToBePrintedForField2 = field1.getPrintingValue(MAP);
dataToBePrintedForField2 will have the value "text2DefaultData"
In the following example, field data that has been explicitly defined takes precedence over a data map entry and the default data.
var field1 = getField("/Body/Text0001");
field1.setData("explicitValue");
var dataToBePrintedForField1 = field1.getPrintingValue(MAP);
dataToBePrintedForField1 will have the value "explicitValue"
Override the field data for a single field
The field's data can be explicitly defined.
var field1 = getField("/Body/Text0001");
field1.setData("mystaticdata");
var dataToBePrintedForField1 = field1.getPrintingValue(MAP);
dataToBePrintedForField1 has the value "mystaticdata"
Determine whether field data has been explicitly set
var field1 = getField("/Body/Text0001");
var isSet = field1.isDataSet();
// isSet will return false, because the script has not set the data on the field.
field1.setData("explicitValue");
var isSetNow = field1.isDataSet();
// isSetNow will return true, because the script data has been set.
field1.clearData();
var isSetAfterClear = field1.isDataSet();
// isSetAfterClear will return false, because the script data has been cleared.
field1.setData(null);
var isSetWithNull = field1.isDataSet();
// isSetWithNull will return true, because the script data has been set.
Override the field data for multiple fields with the same data reference
This example assumes that there are several fields in the label that have the same fully qualified name. Refer to Text0003, Text0004, and Text0005 in the sample data.
var field3 = getField("/Body/Text0003");
var fqn = field3.getFullyQualifiedName();
// the FQN in this case will be "/groupedField"
MAP.setValueForKey(fqn,"myglobalvalue");
var field4 = getField("/Body/Text0004");
var field5 = getField("/Body/Text0005");
var dataToBePrintedForField3 = field3.getPrintingValue(MAP);
var dataToBePrintedForField4 = field4.getPrintingValue(MAP);
var dataToBePrintedForField5 = field5.getPrintingValue(MAP);
dataToBePrintedForField3, dataToBePrintedForField4, and dataToBePrintedForField5 will all have the same value of "myglobalvalue"
Change the default value of a field
var field2 = getField("/Body/Text0002");
field2.setDefaultData("mynewDefaultData");
var dataToBePrintedForField2 = field2.getPrintingValue(MAP);
dataToBePrintedForField2 will be set to the value "mynewDefaultData"