Salesforce

How Do I Construct and Evaluate an Expression at Runtime? (Magic xpa 4.x)

« Go Back

Information

 
Created BySalesforce Service User
Approval Process StatusPublished
Objective
Description

How Do I Construct and Evaluate an Expression at Runtime? (Magic xpa 4.x)

There may be occasions where you want to construct an Magic xpa expression at runtime, then execute it. This is useful when you want to store “macro” instructions in a DB Table, for instance. The instructions can be created by one program, and executed by another.

EvalStr() allows you to do just that. It is a very powerful function that can execute any other Magic xpa function. In this test case, you can type in any text you like, and Magic xpa will execute it.

Using EvalStr

EvalStr(expression string, default value) takes two parameters:

  • expression string is the expression to be evaluated. It can be a literal string, in single quotes, or an alpha variable.

  • default value is the value that will be sent back if there is a syntax error in the expression. In our second example, the alpha string is an illegal expression (as the right parenthesis is missing), so, the string ‘Does not compute’ is returned.

Note that there are some issues here. First, you have to ensure that the data type of the result matches what you are expecting. The Magic xpa syntax checker has no way of knowing what the expression will evaluate to at runtime.

Also, there is no syntax checking of the string until runtime. If we typed in an invalid expression, the default value would be returned, but we wouldn’t know what the error was.

You can handle these issues using the EvalStrInfo() function, described below.

Using EvalStrInfo

EvalStrInfo(expression string, option) takes two parameters:

  • string is the expression to be evaluated. It can be a literal string, in single quotes, or an alpha variable.

  • option is a number, 1, 2, or 3:

    • 1 = returns the Attribute of the expression

    • 2 = returns the parser error, if any

    • 3 = returns the result expression

Option 1 returns a letter representing the attribute result of the expression: A for Alpha, N for Numeric, etc. It also returns E for Error, or * for unknown type.

Option 2 returns the parser error. In this example, we have entered an expression that isn’t correct, and the error message is: ‘)’ expected.

Option 3 returns the parsed expression, if there is no error.

See also

The Online and Rich Client Samples projects (program EX02 and REX02)

Reference
Attachment 
Attachment