Parameter Validation using Constraints 
Constraints are widely used in databases to force valid values in tables, to make sure nobody makes a typo when entering data. In SAP CPS simple constraints are used to validate parameters.
There are different types of simple constraints:
List - A list of valid values
Pair List - A list of valid values with descriptions
Expression - An expression
Table - A list of values retrieved from a table
Query Filter - A list of values retrieved with a filter, such as job filters
The Simple Constraint Data field is used to specify the list, expression, or table name. The Simple Constraint Message is displayed when the value of the parameter does not match the simple constraint. A good Simple Constraint Message could be the following:
"Please choose a valid value from the list, the value you entered is not valid."
All constraint types allow the use of variables:
${value} – value specified in the submit wizard.
${legalValues} – short list of legal values (This variable is empty when used in expression constraints)
${parameter} – name of the parameter.
The List constraint is the easiest to create and parameter-specific. The following illustrates a valid List constraint with the following in the Simple Constraint Data field:
A,B,C,D,E
Like the List constraint, the Pair list constraint is the easy to create and parameter-specific. The following illustrates a valid Pair List constraint with the following in the Simple Constraint Data field:
A="First Letter",B="Second Letter",C="Third Letter",Z="Last Letter"
Note that the letters (A, B, C, and Z) are the values passed on to the parameter. The submit wizard will create a list of values for the user to choose from.
Like the List constraint, the Expression constraint is parameter-specific and consists of a Redwood Expression Language expression. The result of the expression is used to validate the value in the submit wizard.
An example message you can use is:
The value ${value} you specified is invalid for parameter ${parameter}, Valid values are ${legalValues}.
Using Redwood Expression Language expressions:
Use the keyword value for the actual value, in the following example force the value to be a number between 5 and 10.
=value >= 5 && value <= 10
You can also use the special constraint functions:
Constraint.listConstraint(titles, list, bool)
Constraint.pairListConstraint(titles, list, bool)
In the above functions, the following parameters can be used:
titles is a comma separated list of titles.
list is either a normal list or a pair list
bool is a boolean expression involving the parameter value, true evaluates to accept and false to reject.
=Constraint.listConstraint('Country', '31,33,44', \
Table.getColumnString('Countries', value, 'Translation') !== '')
This example works with the following table named countries:
Key |
Name |
Abbreviation |
Translation |
Conversion_Rate |
31 |
The Netherlands |
NL |
Nederland |
1.0 |
33 |
France |
Fr |
France |
1.0 |
49 |
Germany |
De |
Deutschland |
1.0 |
502 |
Guatemala |
Gt |
Guatemala |
11.6 |
Note that the column we retrieve in the boolean expression, Translation in the example above, is not really relevant. The value returned is compared to '', which means empty. If the column is not mandatory and is not filled, the operator will not be able to use the value associated with the column.
Unlike the other simple constraints, the Table constraint allows you to specify a list of values once that can be used in multiple job definitions. The list of values can be maintained from one central location. The Table constraint requires you to fill a table. You can use the built-in System_Variables table or create your own. The value used will be the value from the Key column in the table. In the example above, the values available would be 31, 33, 49, and 502.
To use a table, you select Table in the Constraint Type field and fill the name of the table in the Simple Constraint Data Field.
The query filter constraint allows you to use object filters to generate list of values for parameters, this is especially handy for RedwoodScript job definitions. The submitter must be able to see the object filter, or the the parameter value will not be evaluated at submit-time. If the parameter is optional, this has no major consequences, however, if the parameter is mandatory, the user will not be allowed to submit the job. Object filters also display a list according to the submitter's privileges, so two different users might see different lists.
In the following example RedwoodScript code, the job definition has a parameter named Parameter1 which has a query filter constraint on the filter All job Definitions. When the job definition is submitted, you can choose a job definition for Parameter1, the value is a string, which can then be used to retrieve the job definition object, via its business key, for example.
{
// Print the actual parameter value
jcsOut.println("parameter=" + Parameter1);
// Get a business key for the parameter value
BusinessKey key = BusinessKey.valueOf(Parameter1);
// Use the business key to retrieve the job definition
JobDefinition jd = jcsSession.createBusinessKeyResolver().getJobDefinitionByKey(key);
// Print the name of the job definition
jcsOut.println("job definition=" + jd.getName());
}
The value of the query filter is <object_type>:<partition>.<object_name>, such as JobDefinition:GLOBAL.System_Info or EventDefinition:GLOBAL.DataLoadComplete, for example.
Field |
Description |
Default Value |
Simple Constraint Type |
The type of constraint to use |
None |
Simple Constraint Data |
The data to be used by the constraint (list, expression) |
|
Simple Constraint Message |
The message to display when the constraint is violated |