Show TOC

HTMLBusiness Function DevelopmentLocate this document in the navigation structure

Use

General Principles

To support the design of HTML templates when implementing Internet applications, you can develop your own HTMLBusiness functions, which are syntactically similar to those used in programming languages like ABAP or C.

For example, a simple HTMLBusiness function concatenate_string that concatenates two strings could have the following form:

`function concatenate_string (string1,string2)
 result = string1 & string2;
 return (result);
end;`
            

You pass the two strings to be concatenated in the parameters string1 and string2, and then return the result.

You could call the function concatenate_string as follows:

...
 `write (concatenate_string("A","B"));`
...
            

This would produce the output AB.

The advantage of defining HTMLBusiness functions is that you can reuse them as required in as many templates as you like. This saves you from having to redefine the code every time.

SAP does not deliver a standard library of reusable HTMLBusiness functions. Rather, it is up to your development team to define functions that meet your specific application's requirements.

HTMLBusiness Function Definition

The function definition specifies:

  • The name of the function

  • The number and the names of the parameters the function can expect to receive

  • A function body containing the statements that determine what the function does

  • A return value

HTMLBusiness functions must be defined before they can be used. You can place a function definition anywhere in the code, but not within any other HTMLBusiness statement. For example, you cannot embed a function definition in an if statement.

HTMLBusiness Function Syntax

The basic syntax of an HTMLBusiness function definition is:

function <function name> (<parameter list>)
 <function body>
 return ( <return value> );
end;
            

The syntax components are summarized in the following table:

Component

Description

<function name>

Unique identifier, which consists of an unbroken sequence of alphanumeric characters and permitted special characters.

HTMLBusiness function names must begin with a letter. After this letter, you can use any combination of upper case letters from A to Z, lower case letters from a to z, and digits from 0 to 9.

Blanks are not allowed, but you can use the underscore character _ to join separate words that make up the function name.

<parameter list>

The names of the parameters you want to pass.

The default expression is used whenever the caller does not provide a parameter.

<function body>

Any valid HTMLBusiness statement.

<return value>

Any valid HTMLBusiness statement.

Calling HTMLBusiness Functions

You can call HTMLBusiness functions as part of any valid HTMLBusiness statement. The syntax is:

`
 ...
  <function name> (<parameter expression>)
 ...
`
            

The <parameter expression> consists of a list of parameter names, each separated by a comma. For each parameter name, the syntax is:

<parameter name>=<any valid HTMLBusiness expression>
            

When an HTMLBusiness function is called, each parameter expression is evaluated and the resulting value is assigned to the matching parameter identifier.

  • If parameters are identified with parameter names, a matching parameter name is looked up in the function definition. If no matching name is found, a runtime error occurs.

  • If parameters are not identified with parameter names, they are regarded as positional parameters and are copied one by one into the matching parameter identifier by copying the value of the n. parameter expression into the n. parameter identifier.

This means that you can call the function concatenate_string in two ways:

  • With parameter names, as in:

    concatenate_string(string1="prefix",string2="suffix") ;

  • With positional parameters, as in:

    concatenate_string("prefix","suffix") ;

A function can call another function, provided the other function has been declared.

In the following example, a function func_1 calls a function func_2, but this would result in a compile time error, because the function func_2 has not been declared when it is called.

`
 function func_1 (x)
  write(func_2(2*x));   <!-compile error -->
 end;

 function func_2 (y)
  write(y);
end;

 func_1(10);
`
            

Calling HTMLBusiness Functions Recursively

An HTMLBusiness function can call itself. The following example function calculates factorials:

`
 function factorial(f,x=1)
  if (x==f)
   return (x);
  else
   return (x*factorial(f,x+1));
 end;
 end;

 repeat with i from 1 to 10;
  factorial(i);
 end;
`
            

Return Values in HTMLBusiness Functions

HTMLBusiness functions can return values to the caller.

If an HTMLBusiness function returns a string value, which is then used in an expression that results in a numerical value, automatic type conversion is performed.

In the following function dup_string, which duplicates strings, all the statements specified below are possible:

`
 function dup_string (s)
  return (s&s);
 end;

 dup_string("a");                    <-- outputs "aa" -->
 write(dup_string("b"));                    <-- outputs "bb" -->
 dup_string(dup _string("c")); <-- outputs "cccc" -->
 2*dup_string("1");                                       <-- outputs "22" -->
`
            

Variable Scope in HTMLBusiness Functions

If a parameter of an HTMLBusiness function has the same name as a global variable, that parameter contains the value supplied by the caller within the function, but the value of the global variable is restored when processing returns from the function to caller, as shown in the following example:

`
...
function write_numbers ( x1,x2,x3 )
 global_variable = x1;
 write("global_variable is ",global_variable,"\n");
 write("x1 is ",x1,"\n");
 write("x2 is ",x2,"\n");
 write("x3 is ",x3,"\n");
end;

global_variable = 1000;
x1 = 100;
x2 = 200;
x3 = 300;
 write("Before the call\n");
 write("global_variable is ",global_variable,"\n");
 write("x1 is ",x1,"\n");
 write("x2 is ",x2,"\n");
 write("x3 is ",x3,"\n");
 write("Calling the function . . .\n");
 write_numbers(10,20,30);
 write("After the call\n");
 write("global_variable is ",global_variable,"\n");
 write("x1 is ",x1,"\n");
 write("x2 is ",x2,"\n");
 write("x3 is ",x3,"\n");
...
`
            

This produces the following output:

Before the call

global_variable is 1000

x1 is 100

x2 is 200

x3 is 300

Calling the function . . .

global_variable is 10

x1 is 10

x2 is 20

x3 is 30

After the call

global_variable is 10

x1 is 100

x2 is 200

x3 is 300

Standard HTML in HTMLBusiness Functions

HTMLBusiness functions can contain standard HTML code.

In the following example, an HTMLBusiness function called write_html_text outputs the HTML text <H1> This is standard HTML </H1> ten times in the Web browser.

`
 function write_html_text() 
  ` <!--Now entering HTML mode -->
    <H1> This is standard HTML </H1>
  `end;`

`repeat with I from 1 to 10;
  write_html_text();
 end;
`
            

Field References

You cannot pass parameters to fields by reference. Consider the following example:

`
 ...
  function dump_table(t)   
   `<TABLE>`
   repeat with i from 1 to t.dim;
    `<TR><TD>`t[i]`</TD></TR>`
   end;
   `</TABLE>`
end;

mytable[1] = "Line 1";
mytable[2] = "Line 2";
mytable[3] = "Line 3";
dump_table(mytable);
...
`
            

In this case, HTMLBusiness outputs only the first line of the table because mytable, passed as a parameter to the function dump_table, is evaluated as the value of the first line of the multi-value field mytable which is Line 1.

To resolve the name at runtime, you must pass the name of the variable as a string instead and use the HTMLBusiness ^ operator, as shown below:

`
... function dump_table(t)
 `<TABLE>`
 repeat with i from 1 to ^t.dim;
  `<TR><TD>`^t[I]`</TD></TR>`
 end;
 `</TABLE>`
end;

mytable[1] = "Line 1";
mytable[2] = "Line 2";
mytable[3] = "Line 3";
dump_table(mytable); ...
`
            

Application Example

One useful application is to write HTMLBusiness functions that generate HTML code. For example, you could define a function screenfield as follows:

`
function ( ~screenfield )
 if (^~screenfield.disabled)
  <!--- Screenfield is not ready for input -->
  write(^screenfield.label);
 else
  write("<INPUT TYPE=\"");
   if (^screenfield.type=="RadioButton")
     write("RADIO\"");
     write(" NAME=\"",^screenfield.group,"\"");
     write(" VALUE=\"",^screenfield.name,"\"");
   else
    if (^screenfield.type=="CheckButton")
     html_type = "CHECKBOX";
    else
     html_type = "TEXT";
    end;
    write(html_type,"\"");
    write(" NAME=\"",^screenfield.name,"\"");
    write(" VALUE=\"",^screenfield.value,"\"");
   end;
  end;
 end;
`
            

You could call the function screenfield in two ways:

  • With parameter names, as in:

    screenfield (~screenfield="VBAK-VBELN");

  • With positional parameters, as in:

    screenfield ("VBAK-VBELN");

Function calls can evaluate expressions, so you can also say:

this_field = "VBAK-VBELN";
dnprofield (this_field);
            

Including HTMLBusiness Functions

You can define HTMLBusiness functions in one template, and then include them in any other template where you want to use them.

In the following fragment example, it is assumed that the function screenfield is defined in the template util.html. In order to use screenfield, another HTMLBusiness template must therefore include util.html:

`
include(~service="util",~theme="",~name="util.html");

screenfield(~screenfield="VBAK-VBELN");
screenfield(~screenfield="VBAK-BELNR");
screenfield(~screenfield="VBAK-DATUM");
`
            

Although the include statement assigns default values to the parameters and does not generate an error message if parameters are missing, you must list the parameters ~service, ~theme and ~name explicitly, because the HTMLBusiness interpreter differentiates between compile time includes and runtime includes.

Compile time includes are already included at compile time of the including template. This is necessary so that the functions defined in the included template are known.