Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Perform validation on CSV upload before inserting data to database table

jackyhjj
Explorer
3,423

Hi all,

I am quite new in ABAP world as this new system is being recently implemented in our company. The first task on hand is to do a program which


1. receive an CSV file as the input


2. Validate the value in the CSV file accordingly (length, mandatory based on type, accepted value range, etc compared to the destination database table created)

Example :
a. value column A in csv file has length 10 but the field in destination database has length of 5 only. Expect to throw a message to user saying it is exceeding the length of database field
b. column B only accepts '1', '2' or '3' as value, will prompt error if the value from CSV is '4'
c. if column C is of type 'type1', then column D will need to have value too (mandatory).


Note: user might be able to upload the CSV file with randomize position of the column, but will include column name in the CSV file.


3. Pushing the uploaded data to the respected field.

6 REPLIES 6
Read only

joltdx
Active Contributor
2,694

Hi!

Yes, this is something ABAPers might do, what is it you specifically need help with? How are you approaching this and where are you stuck?

Read only

jackyhjj
Explorer
0 Likes
2,694

Since I am new to ABAP, I more keen about the list of ready-to-use FM that can validate my input value easily, instead of manually compare the length or type.


Currently I am getting the fields from the component descriptor of the destination DB table and compare the name of the fields uploaded via CSV to ensure that it is available in the DB destination fields. I am also using this to compare the length of the value from the CSV to the field destination. Btw, I am splitting the CSV data by using comma (,) and compare it. (loop inside a loop)!!!!


Have tried googling and found out DDUT_INPUT_CHECK. It doesn't help since it will still get the maximum character of the defined length and throw me a sy-subrc = 0 ( I am expecting this to throw me some error but it doesn't).

I know that there is a CSV_TO_STRUCTURE method but the limitation is we need to specify the field in the CSV in the exact order as the structure defined (which is not applicable in my case)

It is working okay but I don't think it will survive the performance test and deep inside, I still expect something better than this way, so any consult or guidance is muchly appreciated.


Oh and I am also stuck to validate the mandatory field and checking against 'value/check' table for each value from CSV. Looking forward to your kind advice! Thanks jorgen_lindqvist41

Read only

matt
Active Contributor
2,694

I don't think you'll find anything useful among the function modules - or methods for that matter. I wrote a generic CSV uploader years ago (no, I do not have the code to hand!), and just did all the validation through my own ABAP code. And yes, you can't avoid a loop in a loop.

In the book, Clean ABAP (to be recommended), there's an excellent piece of code for b), if the value is held in a domain validation.

d) check the type is correct. I.e. if you're expecting a numeric 'AB' is an error...

Read only

jackyhjj
Explorer
0 Likes
2,694

thanks for your response matthew.billingham !

Well, your answer is reassuring that somehow I might be on the right track. Will try to look for that book and find out more to fulfill my requirement.

Really appreciate your response! Thanks and cheers!

Read only

FredericGirod
Active Contributor
2,694

Hi

I am not an expert about LSMW, but I think there is a validation like this.

I have wrote a dynamic CSV reader in clean code. You need to create a structure of the file in the SE11 dictionary, and the code use the domain to validate the data. But not the list of possible values related to a table or a domain. But I think it is possible

And I have also wrote a part of code that identify the date format if it is DD.MM.YYYY DD-MM-YYYY ... (and also excel)

Read only

jackyhjj
Explorer
0 Likes
2,694

Thanks frdric.girod for your response.

I guess I am going to write my own code then.

Cheers!