Notation
A script variable is denoted by V[varname] where varname is the name (or identifier) of the variable.
  • Use only letters, digits, '_'  or  "-". The name should start with a letter or digit.
  • All names are case sensitive, i.e. V[X8] and V[x8] are different variables

Tip: Observe a consistent name convention. For example, only lower case letters and the underscore as delimiter, such as V[previous_account]. Or with upper case and lower case letters, without underscore: V[PreviousAccount]. It often makes sense to let all variable names start with a common identifier denoting the transaction or transaction group, e.g. V[VA01_PreviousAccount] or [VA_PreviousAccount].

Value
A variable represents a string, the value of the variable. Each expression &V[varname] in a script line is replaced by the value of the given variable V[varname] before the script line is executed. 

System variables
Some variables are already predefined by the system. Their names start with an underscore, followed by lower case letters. Examples:

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

For a detailed list of all system variables please see the section system variables.

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

If you need to pass values from one session to another you can use the "toSharedText" and "fromSharedText" options of the CopyText command. If you start an InputScript that runs in a new session, using a "/O..." function code, the parameters are automatically passed to the new mode. To return parameters from the called session to the calling session, use the "ReturnValues" statement.

Setting values
To set a value, use the format

  Set  V[varname]   "value"

Example:

  Set V[MM_init] "X"

Use the "&V[...]" notation if the new string value is to be taken from another variable. Example:

Set V[MM_nr2]   "&V[MM_nr1]"

Please avoid a notation without quotation marks, e.g.

Set V[MM_nr2]  &V[MM_nr1]    // not recommended !!

since this will not work as desired if the variable value contains blank characters: The statement is executed after replacing the variables and this results in using the first word of the given string without the rest, which GuiXT interprets as additional options of the Set command. The only case where quotation marks are not needed is when you work with numbers (represented as string values). Example:

Set V[i] 1
...
Set V[i] &V[i] + 1

Querying a variable value
To compare a variable with a string, use the format

if V[varname=value]
...
endif

Please observe: The comparison  does not distinguish between upper case and lower case, and it does not distinguish between "0", "00", and an empty string. The reason for this behaviour is that SAP entry fields are often automatically converted to upper case, and a value entered as "0" is often shown as Space. If GuiXT distinguished between upper and lower case values here, it would be difficult to determine if an account number changed.

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

Example:

Set V[x] "wood"

if V[x=WOOD]
... yes
endif

if V[x=WOOD] -strict
... no
endif

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

Example:


if (V[x=WOOD1]  or  V[x=WOOD2])  and not V[status=X]
...
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:

if V[k<100]
  goto next_row
endif

Short notation to ensure that a variable is not empty
Use the notation

if V[varname]
  
....
endif

 

Comparing two variables
Instead of a fixed string you can compare the variable with another variable using the notation        &V[varname]. Example:

if V[account1=&V[account2]]
  ...
endif

Please observe: two ending brackets and no inverted commas

Generic reset of numerous variables
If you specify a variable name with an ending *, all variables the names of which start with the given string will be set to this value. This is handy to reset numerous variables that belong to the same transaction or transaction group. You can use the system variable V[_transactionid] in order to decide whether a new transaction has started. Example:

if not V[_transactionid=&V[MM_transactionid]]

  Set V[MM_*] ""
  Set V[MM_transactionid] "&V[_transactionid]"

endif 

Concatenating variables
In the "Set" command, like in all other commands, you can use several variables one after the other which are then concatenated. Example:

Set V[MM01_text] "&V[M01_text1]&V[MM01_text2]"

Constant strings can be interspersed. Example:

Set V[id] "&V[a]-&V[b]-&V[c]"

Partial strings
To extract a partial string, denote its first and last position after the variable, enclosed with round brackets and separated by a hyphen or a comma. Example:

Set V[x] "00113100"
Set V[y] "&V[x](5-6)"

No V[y] has the value "31". Similarly with comma:

Set V[x] "00113100"
Set V[y] "&V[x](5,6)"

There is a difference in meaning between the two notations that is only significant in Unicode systems. Internally GuiXT uses the UTF-8 character code and some characters (e.g. Umlauts such as ä,ö)  are stored in two bytes, not in one byte as in ANSI. With the hyphen notation e.g. (4-6) you will obtain bytes 4 to 6, whereas you obtain the characters 4 to 6 with the comma notation  (4,6). Example:

Set V[x] "München"
Set V[y] "&V[x](4-6)"

Now  V[y] has the value "nch". Whereas  

Set V[x] "München"
Set V[y] "&V[x](4,6)"

sets y to the value "che".

Since variables can contain structures with binary or decimal packed components, GuiXT cannot generally use the character semantics in Unicode systems.

Length of a string in bytes or characters
The option -stringLength of the Set command returns the length of a string in bytes, whereas               -charCount returns the number of characters. In a Unicode system this can be different. Example:

Set V[leng] "München" -stringLength

Set V[count] "München" -charCount

V[leng] is 7 in a non-Unicode system and 8 in a Unicode system, since 'ü' needs two bytes in UTF-8. The value of V[count] on the other hand is 7 for all systems.

Tip: Use -stringLength in conjunction with the hyphen substring notation (i-k) and -charCount with the comma notation (i,k);  see also the example in the next section.

Partial strings with variable positions
The positions in the substring notation can be variables, for example (&V[i],&V[k]) instead of (5,8), where V[i] and V[k] contain the desired positions as digits. In the following example we delete all dot characters in a given string. In the example we make use of calculations as explained in a section below.

The string V[in] is searched for a dot, one character after the other, and simultaneously we build up the output string V[out].

// for testing purposes
Set V[in] "12.580.720.4"

Set V[out] ""
Set V[charcount] "&V[in]" -charCount
Set V[k] 1

label next_char

if V[k>&V[charcount]]
  goto done
endif

Set V[char] "&V[in](&V[k],&V[k])"

if not V[char=.]
  Set V[out] "&V[out]&V[char]"
endif

Set V[k] &V[k] + 1

goto next_char
 

Extracting components of SAP structures
Often SAP function modules return an SAP data dictionary structure where you need to extract individual components. You can use fixed positions for this purpose, but instead it is better to use a symbolic notation that refers to the SAP data dictionary. You name the dictionary structure in round brackets, followed by a hyphen and the name of the component. As an example we read the address data for a given customer number and extract the city:

Call "BAPI_CUSTOMER_GETDETAIL2" in.CUSTOMERNO="0000002000" out.CUSTOMERADDRESS="addr"
Set V[city] "&V[addr](BAPICUSTOMER_04-CITY)"

Explanation: The function module  "BAPI_CUSTOMER_GETDETAIL2" defines an export parameter  "CUSTOMERADDRESS" that we receive in our variable V[addr]. As defined in the SAP function library the parameter possesses the structure "BAPICUSTOMER_04" where the city is stored in component  "CITY".

Often function modules return table parameters where each row is a SAP dictionary structure that we can handle with this symbolic notation.

If you use VersionNumber the text will be stored in the GuiXT cache (local files) and taken from there instead of relative complex reading of the text from the SAP Repository unless VersionNumber changed.

Setting components in SAP structures
Analogously we can set substrings in a variable, using fixed positions or using symbolic names according to the SAP data dictionary. As an example we read all customers with numbers between 1 and 50 using the function module  BAPI_CUSTOMER_GETLIST. As input this function expects a table where each row contains a customer number interval:

Set V[line](BAPICUSTOMER_IDRANGE-SIGN) "I"
Set V[line](BAPICUSTOMER_IDRANGE-OPTION) "BT"
Set V[line](BAPICUSTOMER_IDRANGE-LOW) "0000000001"
Set V[line](BAPICUSTOMER_IDRANGE-HIGH) "0000000050"

CopyText fromString="line" toText="idrange"

Call "BAPI_CUSTOMER_GETLIST" table.IDRANGE="idrange" table.ADDRESSDATA="adrtab"

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. A different number of decimal places and further options may be specified in the Set command; see documentation.

The format is as follows:

Set V[varname] value1 + value2
Set V[varname] value1 - value2
Set V[varname] value1 * value2
Set V[varname] value1 / value2

GuiXT uses double precision floating point numbers for all calculations.

Example:

Set V[x]  2  /  3 decimals=5

Result: 0,66667

Date calculations
When GuiXT converts strings into numbers, it recognizes the normal standard date formats such as  "01.06.2014" and calculates the days since a fixed date. In this way you can subtract dates from each other, or add and subtract a number of days from a given date. In this latter case the result is returned as date as well, using the same format as the input date.

Example 1:

Set V[x]  "01.06.2014" - 1

Result: "31.05.2014"

Example 2:

Set V[x]  "01.06.2014" -  "01.01.2014"

Result: "151"

Example 3:

We use a couple of date calculations in order to decide whether a given date "V[date]" falls on a Sunday. For this purpose, we subtract a fixed Sunday, e.g. the 31.12.2000, from the given date and then check whether the result is a multiple of 7:

Set V[x] "&V[date]" - "31.12.2000"
Set V[y] &V[x] / 7 decimals=0
Set V[y] &V[y] * 7

if V[x=&V[y]]
  Message "Sunday"
endif
 

Using variables within a variable name
It is possible to use variable values within a variable name. Example:

Set V[name] "x"
Set V[&V[name]] "10"

Now V[x] has the value "10".

This feature is often used for indexed variables, as shown in the following example. We read the "Material" column of all currently displayed rows in table "All items" and set the variables VA_material_1,  VA_material_2, ...:

Set V[i] 1

label start

Set V[VA_material_&V[i]]  "&cell[All items,Material,&V[i]]"
if not V[VA_material_&V[i]]
  goto end
endif


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

label end

 

Field texts from the SAP Data Dictionary
With the notation &ddic[xxx] you get the text of the SAP field xxx in the language of connection of the user. If you specify a length, you will get a corresponding short text from Data Dictionary. Examples:

"&ddic[BSEG-BSCHL]"   Buchungsschlüssel
"&ddic[BSEG-BSCHL] (1-15)"   Buchungsschl.
"&ddic[BSEG-BSCHL] (1-10)"   Bschl

If you use VersionNumber the text will be stored in the GuiXT cache (local files) and taken from there instead of relative complex reading of the text from the SAP Repository unless VersionNumber changed.

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"