Variables, Calculations, Comparisons

Variables, calculations and comparisons are needed to write complex InputScripts. You can also use them in normal GuiXT Scripts, but the InputAssistant component is required in this case as well.

It is also possible to call dll-functions or ABAP-functions (“Call”-Statement”) to perform complex logic and computations. But you will see here that most common problems can be solved without using an exit function.

Working with variables

A variable is denoted by V[my_variable] where my_variable is the name (or identifier) of the variable. Maximum length of the name: 255 characters.  All names are case sensitive, i.e. V[my_variable] and V[my_Variable] are different variables.

Tip Use only a…z, A..Z, 0…9 and the special character “_” “.” “-” in the name of a variable

Examples of variable names:

  • NewAmount
  • PrevMonth
  • IdNumber1

It is not necessary to declare a variable explicitly before working with it.
A variable always represents a string, the value of the variable. This value is denoted either by &V[my_name] or by &[my_name]. Maximum length of the string: 255 characters.

Several variables are predefined by the system, including:

V[_user]
V[_client]
V[_transaction]
V[_host]
V[_database]

For a complete list, see the documentation: System variables.

The variables always have a global scope, i.e. they are visible (and keep their value) in all other scripts of the same R/3 session as well. If you open a new R/3 session (e.g. with the /O command in R/3) you work with a new set of variables.

Tip:  If you want to use the simple notation &[name] for values of variables and of fields, you can use variable names starting with a ‘.’ in order to avoid any conflict with field names on the screen.

An alternative would be to use the qualified notations &V[name] and &F[name]

Setting values 

To set a value, use the format

Set  V[my_variable]   “value”

The value may refer to other variables, e.g.

Set  V[my_variable]   “&V[var2]“

Set  V[my_variable]   “System &V[_database]  Client &V[_client]“

Environment variables (Windows) 

The value of a Windows environment variable “env” is denoted as &%[env].

For example, if you want to create a temporary file, you can use the TEMP variable to get the right folder:

Set V[myFilename] “&%[TEMP]\guixtva01.txt”

Set V[myString] “Just an example”

OpenFile “&V[myFilename]” -Output
AppendFile “&V[myFilename]” myString
CloseFile “&V[myFilename]“

Comparing variables

To compare a variable with a string, use the format

if V[my_variable=value]

endif

You can also compare two variables with the following expression (which looks a little bit complicated):

if V[my_variable_1=&[my_variable_2]]

endif

The comparison  does not distinguish between upper case and lower case, and it does not distinguish between “0″, “00″, and the empty string. The reason for this behavior is that SAP entry fields are often automatically converted to upper case, and a value entered as “0″ is often shown as Space. It would be quite difficult to determine if, for example, an account number changed, if GuiXT would distinguish upper case and lower case values here.

You can use the option “-strict”  after a comparison if you  really want to compare the values as they are.   

Example:

Set V[my_variable] “wood”

if V[my_variable=WOOD]
… yes
endif

if V[my_variable=WOOD] -strict
… no
endif

The if-expression may contain the operators “and”, “not”, “or”, “(” and “)”.

Example:


if (V[my_variable=WOOD1]  or  V[my_variable=WOOD2])  and not V[status=X]
… 
endif

 

Example 1

if V[_database=PR1]
Title “Welcome to our production system PR1″
else
Title “Welcome to our system &V[_database]“
endif

It is also possible to use the operators “<” and “>”. Please observe that in this case the variable is always considered to be a number. There is no string comparison with “<” or “>”.

Example 2

if V[index<100]
goto next_row
endif

Calculations

The Set command supports the basic operation +,-,*,/. All operands are considered to be numbers. The result is then stored in string format, up to 2 decimal places (rounded) for resulting non-integer values. The format is as follows:

Set V[my_variable] value1 + value2
Set V[my_variable] value1 – value2
Set V[my_variable] value1 * value2
Set V[my_variable] value1 / value2

Please observe that at least one Space is needed before and after the operator.

Example 3

The following script splits an amount field shown on the screen into 3 parts and displays the resulting amounts:

Set V[total_amount] &[Amount]

// Set total_amount =0 if no amount shown on screen
if not V[total_amount]
Set V[total_amount] 0
endif

// Split amount
Set V[amount_east] &V[total_amount] * 0.50
Set V[amount_mid] &V[total_amount] * 0.10
Set V[amount_west] &V[total_amount] * 0.40

// Display splitted amount
Text (10,80) “East: &V[amount_east]“
Text (11,80) “Middle: &V[amount_mid]“
Text (12,80) “West: &V[amount_west]“

 

Example 4

The following script can pe performed “On Enter” in VA01 in order to enter the value 1 into the “order quantity” column for each row where the user enters a material number without specifying a quantity:

Set V[i] 1

label start

Set V[mat] “&cell[All items,Material,&V[i]]”
if not V[mat]
goto end
endif

Set V[oqt] “&cell[All items,Order quantity,&V[i]]”
if not V[oqt]
Set cell[All items,Order quantity,&V[i]] “1″
endif

Set V[i] &V[i] + 1
goto start

 

Variables, Calculations, Comparisons