Looking for Non-Printable characters inside internal table ABAP - abap

I have an internal table this is written to file and then pulled into the BW as a datasource. Occasionally a non printable character makes it into the file output and breaks the import process into the BW. Below is a sample of my code. Since the itab is not type c or string I am unable to use the find/replace regex on it. Has anyone else had to solve this type of problem before?
FORM eliminate_non_print_char TABLES p_shiptab STRUCTURE shiptab.
LOOP AT p_shiptab INTO wa_shiptab.
FIND REGEX '[^[:print:]]+(?!$)'
IN wa_shiptab
IGNORING CASE.
"RESULTS result.
IF sy-subrc = 0.
REPLACE REGEX '[^[:print:]]+(?!$)'
IN wa_shiptab WITH ''
IGNORING CASE.
ENDIF.
ENDLOOP.
DATA: BEGIN OF shiptab OCCURS 2000.
INCLUDE STRUCTURE ship1.
INCLUDE STRUCTURE ship2.
DATA: landtx LIKE vbrk-landtx,
bl_konwa LIKE vbak-waerk.
INCLUDE STRUCTURE ship3.
INCLUDE STRUCTURE ship4.
DATA: frght_amnt_usd LIKE konv-kwert,
revenue_amnt_usd LIKE vbap-netwr,
unit_price_usd LIKE vbap-netpr,
pgi_posting_date LIKE mkpf-budat,
ord_line_item_qty LIKE lips-lfimg,
asm_no LIKE kna1-kunnr,
asm_username LIKE adrc-sort1,
va_augru_t LIKE tvaut-bezei,
ship_to_name LIKE adrc-name1,
bill_to_name LIKE adrc-name1,
forward_to_name LIKE adrc-name1,
fmv_amnt LIKE konv-kbetr,
va_butxt LIKE t001-butxt,
sold_to_search_term LIKE adrc-sort1,
bill_to_search_term LIKE adrc-sort1,
va_prctr LIKE vbap-prctr,
va_bezei LIKE tvrot-bezei.
INCLUDE STRUCTURE zorder_attr.
DATA: extended_bits_count(20),
va_bstkd_hdr LIKE char32.
DATA: gsm_bp_katr6 LIKE kna1-katr6,
gsm_bp_vtext6 LIKE tvk6t-vtext,
asm_ze_katr7 LIKE kna1-katr7,
asm_ze_vtext7 LIKE tvk7t-vtext,
gsm_ze_katr6 LIKE kna1-katr6,
gsm_ze_vtext6 LIKE tvk7t-vtext.
DATA: END OF shiptab
.
The error I get is: "WA_SHIPTAB" must be a character-type data object (data type C, N, D,T, or STRING). I have non character types in the itab shiptab. I know I can do this lookup on each field individually, but the itab has 235 fields and that does not seem efficient.

The program has another copy of the main itab that is char based fields, I looped through this and placed the following code:
REPLACE ALL OCCURRENCES OF REGEX '[^[:print:]]+$'
IN transtab WITH ''
IGNORING CASE.
Links for the answer:
SCN Link
Help.SAP Link

You could use Runtime Type Sevices to loop through the definition of each field in the table to determine which are type-compatable with the REGEX operation. You can then use dynamic assignment to process only those fields which are compatable, one at a time. For example:
" Initalize Range of typekinds that are compatable with REGEX. See constant
" attributes of CL_ABAP_DATADESCR for typekind definitions. You'll have to
" define these explicitly
lr_typekind_regex = <...>.
" Get components of table structure
lo_tabdescr = cl_abap_typedescr=>describe_by_data( p_shiptab ).
lo_strdescr = lo_tabdescr->get_table_ine_type( ).
li_comp = lo_structdescr->get_components( ).
" Loop through table, sanitizing regex-compatable fields in each row
LOOP AT p_shiptab ASSIGNING <la_shiptab>.
LOOP AT li_comp INTO la_comp.
CHECK la_comp-type->type_kind IN lr_typekind_regex.
" Call subroutine containing Regex to remove non-printable chars from field
ASSIGN COMPONENT la_comp-name OF STRUCTURE <la_shiptab> TO <l_charvalue>.
PERFORM sanitize_field CHANGING <l_charvalue>.
ENDLOOP.
ENDLOOP.

Related

Open Refine: Exporting nested XML with templating

I have a question regarding the templating option for XML in Open Refine. Is it possible to export data from two columns in a nested XML-structure, if both columns contain multiple values, that need to be split first?
Here's an example to illustrate better what I mean. My columns look like this:
Column1
Column2
https://d-nb.info/gnd/119119110;https://d-nb.info/gnd/118529889
Grützner, Eduard von;Elisabeth II., Großbritannien, Königin
https://d-nb.info/gnd/1037554086;https://d-nb.info/gnd/1245873660
Müller, Jakob;Meier, Anina
Each value separated by semicolon in Column1 has a corresponding value in Column2 in the right order and my desired output would look like this:
<rootElement>
<recordRootElement>
...
<edm:Agent rdf:about="https://d-nb.info/gnd/119119110">
<skos:prefLabel xml:lang="zxx">Grützner, Eduard von</skos:prefLabel>
</edm:Agent>
<edm:Agent rdf:about="https://d-nb.info/gnd/118529889">
<skos:prefLabel xml:lang="zxx">Elisabeth II., Großbritannien, Königin</skos:prefLabel>
</edm:Agent>
...
</recordRootElement>
<recordRootElement>
...
<edm:Agent rdf:about="https://d-nb.info/gnd/1037554086">
<skos:prefLabel xml:lang="zxx">Müller, Jakob</skos:prefLabel>
</edm:Agent>
<edm:Agent rdf:about="https://d-nb.info/gnd/1245873660">
<skos:prefLabel xml:lang="zxx">Meier, Anina</skos:prefLabel>
</edm:Agent>
...
</recordRootElement>
<rootElement>
(note: in my initial posting, the position of the root element was not indicated and it looked like this:
<edm:Agent rdf:about="https://d-nb.info/gnd/119119110">
<skos:prefLabel xml:lang="zxx">Grützner, Eduard von</skos:prefLabel>
</edm:Agent>
<edm:Agent rdf:about="https://d-nb.info/gnd/118529889">
<skos:prefLabel xml:lang="zxx">Elisabeth II., Großbritannien, Königin</skos:prefLabel>
</edm:Agent>
)
I managed to split the values separated by ";" for both columns like this
{{forEach(cells["Column1"].value.split(";"),v,"<edm:Agent rdf:about=\""+v+"\">"+"\n"+"</edm:Agent>")}}
{{forEach(cells["Column2"].value.split(";"),v,"<skos:prefLabel xml:lang=\"zxx\">"+v+"</skos:prefLabel>")}}
but I can't find out how to nest the splitted skos:prefLabel into the edm:Agent element. Is that even possible? If not, I would work with seperate columns or another workaround, but I wanted to make sure, if there's a more direct way before.
Thank you!
Kristina
I am going to expand the answer from RolfBly using the Templating Exporter from OpenRefine.
I do have the following assumptions:
There is some other column left of Column1 acting as record identifying column (see first screenshot).
The columns actually have some proper names
The columns URI and Name are the only columns with multiple values. Otherwise we might produce empty XML elements with the following recipe.
We will use the information about records available via GREL to determine whether to write a <recordRootElement> or not.
Recipe:
Split first Name and then URI on the separator ";" via "Edit cells" => "Split multi-valued cells".
Go to "Export" => "Templating..."
In the prefix field use the value
<?xml version="1.0" encoding="utf-8"?>
<rootElement>
Please note that I skipped the namespace imports for edm, skos, rdf and xml.
In the row template field use the value:
{{if(row.index - row.record.fromRowIndex == 0, '<recordRootElement>', '')}}
<edm:Agent rdf:about="{{escape(cells['URI'].value, 'xml')}}">
<skos:prefLabel xml:lang="zxx">{{escape(cells['Name'].value, 'xml')}}</skos:prefLabel>
</edm:Agent>
{{if(row.index - row.record.fromRowIndex == row.record.rowCount - 1, '</recordRootElement>', '')}}
The row separator field should just contain a linebreak.
In the suffix field use the value:
</rootElement>
Disclaimer: If you're keen on using only OpenRefine, this won't be the answer you were hoping for. There may be ways in OR that I don't know of. That said, here's how I would do it.
Edit The trick is to keep URL and literal side by side on one line. b2m's answer below does just that: go from right to left splitting, not from left to right. You can then skip steps 2 and 3, to get the result in the image.
split each column into 2 columns by separator ;. You'll get 4 columns, 1 and 3 belong together, and 2 and 4 belong together. I'm assuming this will be the case consistently in your data.
export 1 and 3 to a file, and export 2 and 4 to another file, of any convenient format, using the custom tabular exporter.
concatenate those two files into one single file using an editor (I use Notepad++), or any other method you may prefer. Several ways to Rome here. Result in OR would be something like this.
You then have all sorts of options to put text strings in front, between and after your two columns.
In OR, you could use transform on column URL to build your XML using the below code
(note the \n for newline, that's probably just a line feed, you may want to use \r\n for carriage return + line feed if you're using Windows).
'<edm:Agent rdf:about="' + value + '">\n<skos:prefLabel xml:lang="zxx">' + cells.Name.value + '</skos:prefLabel>\n</edm:Agent>'
to get your XML in one column, like so
which you can then export using the custom tabular exporter again. Or instead you could use Add column based on this column in a similar manner, if you want to retain your URL column.
You could even do this in the editor without re-importing the file back into OR, but that's beyond the scope of this answer.

Syntax error "Field "L_TABDEF–TLTYPE" is unknown" in Smart Form

I have defined local variables in a program line in SMARTFORMS but it seems that even it is locally declared within the program line node, it does not see the variables.
Error shown below.
Field "L_TABDEF–TLTYPE" is unknown. It is neither in one of the
specified tables nor defined by a "DATA" statement. "DATA" state
FIELD-SYMBOLS: <f_tab> TYPE tsftabdef.
DATA: l_tabdef TYPE ssftabdef , " Work Area for the Table
t_ltypes TYPE tsfltype , " Table – Line types
l_ltypes TYPE ssfltype , " Work Area for the table
t_colinfo TYPE tsfcolinfo , " Table – Columns
l_colinfo TYPE ssfcolinfo , " Work area for the table
t_border TYPE tsfctaba , " Tables – Borders
l_border TYPE ssfctaba . " Work Area for the border
ASSIGN ('(SAPLSTXBC)TABDEFS') TO <f_tab>.
* Table definition table
LOOP AT <f_tab> INTO l_tabdef.
LOOP AT l_tabdef–tltype INTO l_ltypes WHERE linetype = 'LINE'.
LOOP AT l_ltypes–tcolinfo INTO l_colinfo.
LOOP AT l_colinfo-borders INTO l_border.
CLEAR l_border-intensity.
l_border-fillcolor-red = '255'.
l_border-fillcolor-green = '000'.
l_border-fillcolor-blue = '000'.
l_border-fillcolor-used = 'X'.
l_border-cfillcolor-color = 'X'.
l_border-cfillcolor-xred = 'FF'.
l_border-cfillcolor-xgreen = '00'.
l_border-cfillcolor-xblue = '00'.
MODIFY l_colinfo-borders FROM l_border.
ENDLOOP.
MODIFY l_ltypes-tcolinfo FROM l_colinfo.
ENDLOOP.
MODIFY l_tabdef-tltype FROM l_ltypes.
ENDLOOP.
MODIFY <f_tab> FROM l_tabdef.
ENDLOOP.
What am I missing here?
In l_tabdef–tltype, you are confused by the dash character which is in fact the invalid Unicode character EN DASH U+2013, so it's not recognized as the "structure component selector" (i.e. the dash character U+002D) and the compiler considers the whole name as referring to a classic data object, not a structure component.
Probably the error originates from a copy/paste from your favorite Text Processing software.
Solution: please retype "-" in l_tabdef–tltype.
PS: thank you for having provided this Minimal, Complete, and Verifiable example otherwise it would have been impossible to troubleshoot the issue!

How to retrieve a specific field from a list output?

I don't have any developer rights in my SAP-System but I found a way to write some ABAP-Code in a tiny "User-Exit" box (I don't know if that's what you call it) inside a report.
I'm trying to submit a HR-Report and plug it's outcoming PERNR into that same report again.
There's a syntax-error that is telling me that t_list doesn't have a component with the Name PERNR.
What do I have to do in order to get this to work?
DATA: t_list TYPE TABLE OF abaplist WITH HEADER LINE,
seltab TYPE TABLE OF rsparams,
selline LIKE LINE OF seltab.
*I found out that the name of the selection field in the Report-GUI is "PNPPERNR" and tested it
selline-selname = 'PNPPERNR'.
selline-sign = 'I'.
selline-option = 'EQ'.
SUBMIT Y5000112
USING SELECTION-SET 'V1_TEST'
EXPORTING LIST TO MEMORY
AND RETURN.
CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
listobject = t_list
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE 'Unable to get list from memory'.
ELSE.
LOOP AT t_list.
*The Problem is here: how do I get the pnppernr out of t_list, it's the first column of the report output
selline-low = t_list-pernr.
append selline to seltab.
ENDLOOP.
SUBMIT Y5000112
WITH SELECTION-TABLE seltab
USING SELECTION-SET 'V2_TEST'
AND RETURN.
ENDIF.
Use the function module LIST_TO_ASCI to decode the contents of t_list into something readable. This answer contains some sample code including the data types required. At this point, the data you're looking for will probably occur at the same column range in the output. Use the standard substring access methods - e. g. line+42(21) to obtain the part of the line you need.
The vwegert's answer is more than useful! In my previous answer I forgot to mention LIST_TO_ASCI FM :)
The only thing I can add is that parsing of result lines has no universal solution and greatly depends on its structure. Usually it is done like:
LOOP AT t_list.
SPLIT t_list AT '|' INTO <required_structure>.
selline-low = <required_structure>-pernr.
APPEND selline TO seltab.
ENDLOOP.
where <`required_structure> is your Y5000112 output structure. But this may be not so simple and may require additional manipulations.

Inserting data into BAPIRET2_TAB structure

My method is using an export parameter of type BAPIRET2_TAB. I need to fill the values of this structure, but I cant access the structure directly. For example, parameter-message = 'text', etc.
How can I do this?
These are the parameters I need to pass:
lv_msg_line. type i
lv_syntax_text. //Error message
And this is syntax checker.
syntax-check for l_tab_code
program lv_progname
message l_error_message
line l_error_line
word l_error_word
id 'ERR' table l_tab_errors.
Like said above in the comments, BAPIRET2_TAB is not a structure, and therefore cannot have its components accessed directly via STRUCTURE-FIELD paradigm.
What you need is to declare an structure like this
DATA error_line TYPE LINE OF BAPIRET2_TAB.
Then, you can use it to fill the data in...
error_line-program = sy-repid.
error_line-id = sy-msgid.
... and so forth. Then, lastly, you append the error_line item to your BAPIRET2_TAB.
APPEND error_line TO bapi2tab.
CLEAR error_line.
Hope it helps.

How to find a standard text within a SapScript or SmartForm?

I need to track down where within a large number of custom sapscripts and smartforms a specific standard text (SO10) is being used.
Apart from the equivalent of "check the code for each print script", I've not found a workable solution online. Any suggestions?
After posting, I found a partial solution. The code below will search for a standard text within sapscripts, but not smartforms.
PARAMETERS: p_sttxt LIKE stxh-tdname.
DATA: BEGIN OF t_stxh OCCURS 0,
tdname LIKE stxh-tdname,
tdspras LIKE stxh-tdspras,
END OF t_stxh.
DATA t_lines LIKE tline OCCURS 0 WITH HEADER LINE.
SELECT tdname tdspras FROM stxh INTO TABLE t_stxh
WHERE tdobject = 'FORM'
AND tdid = 'TXT'
AND tdspras = 'E'.
LOOP AT t_stxh.
REFRESH t_lines.
CALL FUNCTION 'READ_TEXT'
EXPORTING
* CLIENT = SY-MANDT
id = 'TXT'
language = t_stxh-tdspras
name = t_stxh-tdname
object = 'FORM'
TABLES
lines = t_lines
EXCEPTIONS
id = 0
language = 0
name = 0
not_found = 0
object = 0
reference_check = 0
wrong_access_to_archive = 0
OTHERS = 0 .
SEARCH t_lines FOR p_sttxt.
IF sy-subrc EQ 0.
WRITE:/ t_stxh-tdname, t_stxh-tdspras.
ENDIF.
ENDLOOP.
This is a (fixed) version of the code found here: http://scn.sap.com/thread/179142
What concerns SmartForms, you cannot. You cannot just find it like you want it.
Unfortunately, in such ̶g̶o̶o̶d̶ ̶o̶l̶'̶ legacy technology as SmartForms everything is working legacy way, and standard texts are simply hard-coded. Yes, it looks awkward but they are really hard-coded, and these names are written out to SmartForm FM code every time it is re-generated.
So the only workaround here is to analyze the code.
Find all FMs for existing Smart Forms in system
There is a D010INC table containing all forms with their includes. The main point here is that all SmartForm FMs start with /1BCDWB/ prefix.
The main logic is in the includes, so we need to find correspondent INCLUDE for the target form.
Fetch SF include source code
It can be done in a several ways: via CL_RECA_RS_SERVICES class, via table REPOSRC, but the simplest way is ABAP statement READ REPORT.
Search SO10 text element name in the source code
Get Smart Form names for the FMs from hit list. It can be done via STXFADMI table, like in below snippet, but the more correct way is SSF_FUNCTION_MODULE_NAME FM
Bingo!
Sample solution could look like this:
DATA: lt_source TYPE TABLE OF string,
lt_smartforms TYPE TABLE OF d010inc,
so_text TYPE char50,
fs_form TYPE string,
used_in TYPE TABLE OF string,
len TYPE i.
* populating the list of SmartForm FMs
SELECT * FROM d010inc AS d
INTO TABLE lt_smartforms
WHERE master LIKE '/1BCDWB/%'
AND include LIKE '/1BCDWB/%'.
so_text = '85XX_FOOTER'. " <- our SO10 text element name
LOOP AT lt_smartforms ASSIGNING FIELD-SYMBOL(<fs_fm_name>).
* reading FM source code
READ REPORT <fs_fm_name>-include INTO lt_source.
* checking if SO11 exists in source code
FIND FIRST OCCURRENCE OF so_text IN TABLE lt_source.
IF sy-subrc = 0.
len = strlen( <fs_fm_name>-include ) - 7.
* searching for SmartForm related to the target FM
SELECT SINGLE formname
FROM stxfadmi
INTO fs_form
WHERE fmnumb = <fs_fm_name>-include+len(4).
IF sy-subrc = 0.
APPEND fs_form TO used_in.
ENDIF.
ENDIF.
ENDLOOP.
Yes, it is junky, not elegant and awkward, but who said it should be so?