Help:Processing:Scripting Language

From Opadeez Wiki
Jump to navigationJump to search

Scripting Language

Opadeez Scripting Language has been designed to be as easy as possible to access the solution data model, implement loops and advance logic.
Scripts can be used in different areas of a solution:

  • Attribute formulas are used to define calculated fields (Refer to Data Dictionary for details). For Attribute formulas you should provide either:
    • A single expression. For example: FirstName + " " + LastName
    • A program with a return statement. For example:

     IF LastName <> "" THEN
       RETURN FirstName + " " + LastName
     ELSE
       RETURN FistName
     END IF
  • Visibility, mandatory and read-only rules (Refer to Attribute rules for details). Similarly to Attribute formulas, these rules should be either a single expression or a program with a return statement.
  • Scripts (Refer to Scripts for details). In this case, the script should be a Program.


Keywords, variable names and function names are case sensitive.

General syntax Where Expressions are required, script shall contain one single expression statement e.g. a comparison, a function call etc...

For complete scripts, multiple expressions and instructions can be used. If the script is expected to return a value, the return keyword shall be used.

Instructions Instructions are separated by end of line character. Example below are 2 instructions:

 A = 123
 B = 456

It is possible to write an instruction on multiple lines using the \ character:

 A = 123 \
     + 456
Comments The scripting language supports 2 kinds of comments:
  • End of line comments: End of line comments are identified with // characters
  • Multi-line or in-line comments are identified with /* comments ... */

In the example below, bold characters are comments

 // These are some comments
 A = 123 // These are comments
 /* Multi-line comments
    More comments*/
 A = 456
Literal values The scripting language handles multiple data types (data types are not explicitly defined in the script). Literal values can be entered as:
  • Integer values: 123456
  • Decimal values: 123.456
  • String values: "Some text"
  • Boolean values: true, false
  • Null values: null, NULL
Operators The scripting language handles the following operators:
  • Comparison operators: ==, >, >=, <, <=, <>
  • Boolean operators: NOT, OR, AND
  • Arithmetical operators:
    • +: If both operands are numeric, returns the sum of the numbers. Otherwise, this operator will concatenate the operands.
    • -: Negation operator (only for numeric operands).
    • ^: Power operator (only for numeric operands)
    • *: Multiplication operator (only for numeric operands)
    • /: Division operator (only for numeric operands)
    •  %: Modulus
  • Affectation operator: =
Control structures The scripting language supports the following control structures:

 IF condition THEN program ELSEIF program ELSE program END IF 

Example:

 IF A > 0 THEN
    A = 123
 ELSEIF B > 0
    A = 789
 ELSE
    A = 456
 END IF


 FOR variable IN array program NEXT 

Example:

 FOR account IN Customer.Account
    A = A + account.Balance
 NEXT
Variables declaration Variables are created using declare keyword:

 declare A
 A = 123

Variable names start with a letter, followed by letters, digits or the . character. For example: myClient123.name if a valid variable name.
Variables are not strongly typed, however the values they contain is typed.

Function call Functions are called using syntax: functionName(argument1, argument2, ...)

The following functions are supported:

  • dateAdd: Add a number of intervals to a date.
    • Syntax: dateAdd(date, number, interval)
    • date: Date or String object representing the date to be used.
    • interval: y for year, h for hours, m for minutes, s for seconds, ms for milliseconds
  • dateDiff: Compute the difference between 2 dates.
    • Syntax: dateDiff(date1, date2, interval)
    • date1, date2: Date or String object representing the date to be used.
    • interval: y for year, h for hours, m for minutes, s for seconds, ms for milliseconds
  • fillLeft: Fill a String to the left until reaching a given size.
    • Syntax: fillLeft(inputText, minimumLength, paddingCharacter)
    • inputText: text to be padded
    • minimumLength: target size after padding. Note: if the inputText size is higher than minimumLength, inputText is returned
    • paddingCharacter: character used to pad the String.
  • fillRight: Fill a String to the right until reaching a given size.
    • Syntax: fillRight(inputText, minimumLength, paddingCharacter)
    • inputText: text to be padded
    • minimumLength: target size after padding. Note: if the inputText size is higher than minimumLength, inputText is returned
    • paddingCharacter: character used to pad the String.
  • formatDate: Format a date into String.
    • Syntax: formatDate(inputDate, format)
    • inputDate: the date to format
    • format: A format for the date. Recognized characters are: YY / YYYY (Year), MM (Month), DD (Day), hh (hours), mm (minutes), ss (secondes)
    • Examples: formatDate(now(), "YYYY-MM-DD") returns the current date as: 2016-03-03
  • getIndex: Returns the index (position in the selector) of the current object.
  • getParameter: Returns the value of a context parameter.
    • Syntax: getParameter(parameterName)
  • iif: Returns one of the values depending on the condition
    • Syntax: iif(condition, valueIfTrue, valueIfFalse)
  • left: Returns the left part of a String
    • Syntax: left(inputText, length)
    • inputText: text where the left part is extracted
    • length: number of characters to extract. If number is negative, the function removes <-number> characters from the right of the string
    • Examples: left("ABCDEF", 2) returns "AB", while left("ABCDEF", -2) returns "ABCD"
  • lower: Converts a String to lower case
    • Syntax: lower(inputText)
  • message: Display a message to the user.
    • Syntax: message(message, type)
    • message: The message to show to the user.
    • type (optional): "I" (default), "W", "E" for Info, Warning, Error.
  • new: new(["AliasName"]): Create an empty record for the given (optional) Alias. If no Alias provided, the top-level alias of the current data set is used.
  • noNull: Tests if an object is null, and if yes returns a default value
    • Syntax: noNull(object, default)
    • object: The object to test
    • default: value returned is object is null
    • Example: noNull(someVariable, 0)
  • now: Returns current date and time
    • Syntax: now()
  • randomString: Returns a random String, based on the given length.
    • Syntax: randomString(length)
  • reloadValueLists: Reload the value list object which take their data from a database table
    • Syntax: reloadValueLists()
  • right: Returns the right part of a String
    • Syntax: right(inputText, length)
    • inputText: text where the right part is extracted
    • length: number of characters to extract. If number is negative, the function removes <-number> characters from the left of the string
    • Examples: right("ABCDEF", 2) returns "EF", while right("ABCDEF", -2) returns "CDEF"
  • roundDown: Rounds a number down to a given precision
    • Syntax: roundDown(number, precision)
    • number: Number to be rounded down
    • precision: Number of decimals to keep
    • Example: roundDown(12.345, 2) returns 12.34
  • roundUp: Rounds a number up to a given precision
    • Syntax: roundUp(number, precision)
    • number: Number to be rounded up
    • precision: Number of decimals to keep
    • Example: roundUp(12.345, 2) returns 12.35
  • serverHook: Run a customize Server hook. Expected parameters: hook class name (must implement ServerHookCallable interface)
  • setLoggedUser: set the information of the current logged-on user into the given Alias. Entity type of the given alias must be the one defined in the System "Users table" property.
    • Syntax: setLoggedUser("AliasName")
  • upper: Converts a String to upper case
    • Syntax: upper(inputText)
Data types The scripting languages support the following data types:
  • Number: integer or decimal number e.g. 123.456
  • Boolean: true or false
  • String: e.g. "ABCD"
  • Date: Date and time
  • Alias: type that refers to an Alias defined in the system configuration
  • Array: array of values

Variables are not strongly typed, and can store objects of different types at different moments. Every expression has a specific type which can influence the way they are interpreted. For example the + operator means addition if both operands are numeric, and concatenation otherwise.

NULL values NULL values are handled in the system. Performing operations on operands having NULL values will not raise an error, however the overall expression will return null. For example 1+2+NULL will return NULL.
Access to data dictionary Access to data dictionary depends on the context where the script is being written:
  • For script written in attribute formulas, visibility, mandatory and read-only rules, attributes belonging to the same entity can be accessed directly using their name. For example, a valid visibility rule for an "Age" attribute could be: dateOfBirth <> "". This assumes that dateOfBirth and Age are both attributes from the same entity. It is also possible to access attributes from different aliases (see below).
  • For script written in Script actions, attributes must be referenced with a specific alias name

Attributes can be accessed through specific alias using syntax Alias.attribute.
For aliases having multiple occurrences Alias will return the item currently selected element. To access a specific element the syntax Alias[index] can be used.
It is also possible to access specific elements anywhere within the aliases tree. For example, considering a data model with Customer as a top level entity, a Product entity having multiple occurrences, and Payment being linked to Product with multiple occurrences as well, it is possible to access a specific Payment, from a specific Product using syntax: Product[3].Payment[2].amount
For aliases having multiple occurences, it is possible to loop the elements as in the example below:

 declare product, payment, sumAmount
 sumAmount = 0
 // Browse products
 FOR product IN Product
    // Browse payments for each product
    FOR payment IN product.Payment
       sumAmount = sumAmount + payment.amount
    NEXT
 NEXT
Access to data link attributes The attributes of linked entities can be accesses using Alias.attribute syntax (please refer to section above). It is possible to access a link (e.g. get the ID of the linked entity) or modify a link (e.g. link to a different record) by using the special attribute automatically created for each link. The name of this special attribute is: link<Entity from name><Link name>.

See also