Aart Troost
Read all my blogsIn today’s blog I would like to share a code example about how to dynamically replace text variables in a Standard (SO10) text. Every time I have to use text while I’m programming I want to make this text maintainable so I do not have to hardcode this text. When this text is not hardcoded it’s also easier to make changes to the text by someone else then the developer who created the logic.
For this blog I created the following text containing the variables LV_COMPANY, LV_DATE, LV_CITY and LV_MEDIUM. By the end of the blog we would have an elegant way to replace variables in a SO10 text.
The part I focused on while programming is on how to dynamically replace the variables I used in the standard text. Instead of setting every variable individually with FM ‘SET_TEXTSYMBOL’ I would just like to find all the variables used in the standard text and try to replace them with variables available in the used program.
To read the standard text into local table lt_lines I use FM ‘READ_TEXT’, as you can see the text contains the variables.
We can use this result to find the two variables used in line 2. The check ‘check sy-tabix mod 2 = 1.’ is used to only start at the beginning ‘&’ of each variable.
To find the variable LV_DATE in line 2, you can use FM ‘GET_TEXTSYMBOL’ and feed offset 35 to the start_offset variable. By now the value of the variable ‘lv_textsymbol’ will be ‘LV_DATE’. With this value we can try to find the corresponding value of the variable used in the program.
So the above code will result in the next values for LV_DATE.
After setting all the variables we will call FM ‘TEXT_SYMBOL_REPLACE’ to transfer the text symbols to the standard text we started with.
So now you’ll have a way to dynamically replace the variables in a standard text in your code. For those who want to use (and or improve) this logic here’s the complete code:
data lt_lines type standard table of tline.
data ls_line type tline.
data lv_textsymbol type string.
data lt_result type match_result_tab.
data ls_result type match_result. field-symbols <fs_textsymbol> type any. * SO10 variables
data lv_company type string value ‘Acorel’.
data lv_date type string value ‘05.10.2016’.
data lv_city type string value ‘Amersfoort’.
data lv_medium type string value ‘blog’. call function ‘READ_TEXT’
exporting
id = ‘ST’
language = ‘N’
name = ‘Z_BLOG_ACOREL’
object = ‘TEXT’
importing
header = ls_header
tables
lines = lt_lines
exceptions
id = 1
language = 2
name = 3
not_found = 4
object = 5
reference_check = 6
wrong_access_to_archive = 7
others = 8.
if sy–subrc <> 0.
* Implement suitable error handling here
endif. call function ‘INIT_TEXTSYMBOL’. loop at lt_lines into ls_line.
clear lv_textsymbol.
clear lt_result.
find all occurrences of ‘&’ in ls_line–tdline results lt_result.
loop at lt_result into ls_result.
check sy–tabix mod 2 = 1.
call function ‘GET_TEXTSYMBOL’
exporting
line = ls_line–tdline
start_offset = ls_result–offset
importing
name = lv_textsymbol.
if lv_textsymbol is not initial.
unassign <fs_textsymbol>.
assign (lv_textsymbol) to <fs_textsymbol>.
lv_textsymbol := |&{ lv_textsymbol }&|.
if <fs_textsymbol> is assigned.
call function ‘SET_TEXTSYMBOL’
exporting
header = ls_header
name = lv_textsymbol
value = <fs_textsymbol>
value_length = strlen( <fs_textsymbol> )
replace = ”.
endif.
endif.
endloop.
endloop. call function ‘TEXT_SYMBOL_REPLACE’
exporting
header = ls_header
tables
lines = lt_lines.
Eén reactie op “Dynamic variable replacement in a standard text”
There seems to be an even shorter way to replace the variables in the text. I haven't tested it yet but if a variable is available in the global scope of the program using the FM 'TEXT_SYMBOL_REPLACE' should be enough. After using FM 'READ_TEXT' to read the attributes of the text. Thanks Wilco!