Example: Implementation of Code Generation 
You generate the code for the Create Business Partner Contact expression using Code Composer with BRFplus-specific APIs, which facilitates working with Code Composer.
This custom BRFplus expression is defined in application ISU_AMI_EM_EXPR. The class behind this expression is CL_ISU_FDT_CREATE_CONTACT. Method GENERATE_CONTACT_PROCESS shows how to implement code generation.
At the bottom of this method, there is a template for code generation which starts with directive
Syntax
* @end @nocomposer
Typically in a custom expression, an API is called when creating a business object. Therefore, in the code-generated part, the input parameters have to be extracted and sent to the API, and the result is returned and mapped to the return structure. To do so, a set of variables is needed. Note that the variable names used in the code generation template must be unique for each execution of a custom expression. This is because in BRFplus, the entire function execution is generated into one method of PROCESS_PURE of the generated class. This means that if several expressions of the same type are repeated in one function, the variables used inside the expression template have to be unique. Otherwise, the code generation fails with a syntax check warning. To achieve uniqueness of variable names, you can use the method GET_NEXT_VARIABLE_NAME of class CL_FDT_CC_SERVICES as shown below.
The method which substitutes placeholder variables denoted by the ‘$’ symbol is ADD_VAR of class CL_CMP_COMPOSER.
Syntax
lv_var_name = cl_fdt_cc_services=>get_next_variable_name( 'ls_contact' ).
lo_cc->add_var( i_name = 'ls_contact' i_value = lv_var_name ).
To map input context parameters (function context parameters or input values directly set in the expression from the BRFplus UI), the following techniques can be used:
The input parameters can be loaded from the expression instance buffer. They are preset in expression from the BRFplus UI.
Syntax
ls_buffer = load_buffer( iv_timestamp = iv_timestamp ).
lo_cc->add_var(: i_name = 'buffer_class' i_value = ls_buffer-class ),
i_name = 'buffer_activity' i_value = ls_buffer-activity ),
i_name = 'buffer_text_line' i_value = ls_buffer-text_line ),
i_name = 'buffer_partner' i_value = ls_buffer-partner ),
i_name = 'buffer_priority' i_value = ls_buffer-priority ).
The input parameters are mapped from the function context. This has higher precedence than the input parameter loading from the buffer.
This code performs as shown below:
Syntax
* Contexts variables are prioritized over buffer values
lt_context_ids = lo_function->get_context_data_objects( iv_timestamp ).
LOOP AT lt_context_ids ASSIGNING <fs_uid>.
CLEAR lv_var_name.
io_generation_mngr->get_context_info(
EXPORTING
iv_id = <fs_uid> " Universal Unique Identifier
IMPORTING
ev_context_name = lv_var_name " Beschreibung
).
lo_cc->add_var( i_name = lv_var_name i_value = lv_var_name ).
ENDLOOP.
The result data object has to be specified in the code generation template as well. This is done as follows:
Syntax
lv_result_do_id = if_fdt_expression~get_result_data_object( iv_timestamp ).
cl_fdt_factory=>get_instance_generic(
EXPORTING iv_id = lv_result_do_id
IMPORTING eo_instance = lo_instance ).
lo_result_do ?= lo_instance.
IF iv_create_variable EQ abap_true.
lo_result_do->generate_create_data_ref(
EXPORTING iv_variable_name = iv_variable_name
iv_timestamp = iv_timestamp
io_generation_mngr = io_generation_mngr
IMPORTING et_source_code = lt_result_decl ).
ENDIF.
lo_cc->add_var( i_name = 'result_var_decl' i_value = lt_result_decl ).
lo_cc->add_var( i_name = 'result_var_name' i_value = iv_variable_name ).
With the following line in the code template, the definition of the result object is inserted in the generated code:
Syntax
* @INSERT $result_var_decl$.
The call to method BUILD_CODE of class CL_CMP_COMPOSER with flag i_flg_decomment set to True uncomments code in GENERATE_CONTACT_PROCESS, finds directive * @end @nocomposer, and from that line uses the template to generate code.
Syntax
lt_source_code = lo_cc->build_code( i_flg_decomment = abap_true
i_template_include = lv_include_name ).