Application Development 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 a file in a server and create mail

amine_lamkaissi
Active Contributor
0 Kudos

Hi experts,

I have a csv file in an AL11 BW server as following:

I would like to add some abap instructions into a BW abap to program to read the second line of this file and then create a dynamic address mail with the number or name present in the second line in this format name recovered@gmail.com .

So in my case that will be 1200@gmail.com.

Is that possible?

Thanks

Amine

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

The comands for reading a file in Application server are OPEN DATASET/ READ DATASET,  you can see the help for more details.

Anyway if you want to read the second line:

* Open file

OPEN DATA <FILENAME>   FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF SY-SUBRC = 0.

  DO 2 TIMES.

     READ DATASET <FILENAME> INTO <STRING>.

     IF SY-SUBRC <> 0. EXIT. ENDIF.

     CHECK SY-INDEX = 2.

* This is the second line where you can get the ID

     SPLIT <STRING> AT ';' INTO TABLE <ITAB>.

* Read info from first field of csv

     READ TABLE <ITAB> INDEX 1.

* Now in workarea of <ITAB> there's ID

  ENDDO.

of course it's only a sample, perhas there's an option to point directly to the second line

Max

7 REPLIES 7

Former Member
0 Kudos

Hi

The comands for reading a file in Application server are OPEN DATASET/ READ DATASET,  you can see the help for more details.

Anyway if you want to read the second line:

* Open file

OPEN DATA <FILENAME>   FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF SY-SUBRC = 0.

  DO 2 TIMES.

     READ DATASET <FILENAME> INTO <STRING>.

     IF SY-SUBRC <> 0. EXIT. ENDIF.

     CHECK SY-INDEX = 2.

* This is the second line where you can get the ID

     SPLIT <STRING> AT ';' INTO TABLE <ITAB>.

* Read info from first field of csv

     READ TABLE <ITAB> INDEX 1.

* Now in workarea of <ITAB> there's ID

  ENDDO.

of course it's only a sample, perhas there's an option to point directly to the second line

Max

0 Kudos

Hi Max,

Thanks a lot for your helpful answer. I have a question please?

How the program can find the adress of the directory? in my case F:/Interfaces

Thanks.

Amine

0 Kudos

The path has to be defined in an additional table or harcoded in the abap code (if it'll be always the same)

but it's the same for the file

In your case <FILENAME> = 'F:/Interfaces/ZFILE_IN_SALES_PLAN.csv'

How to get path and file depend on your business, in R3 (probably in BW too) there's transaction FILE where it's possible to define and manage a logical file (path and file) and use fm like FILE_GET_NAME in order to get the phisical file name.

Max

0 Kudos

Hi Max,

Thansk for your answers.

I have two questions regarding your proposed code please.

In this part:


max bianchi wrote:

* Read info from first field of csv

     READ TABLE <ITAB> INDEX 1.

* Now in workarea of <ITAB> there's ID

  ENDDO.

if i want to recover the id from the itab in a workarea in order to build a mail. Should i do this?

READ TABLE <ITAB> INDEX 1 into workarea1.

Concatenate workarea1 ; @gmail.com into workarea2


My other question, if i want to send a simple text to the mail like:


Hello;

Your file is corrupted, can you review it please?

Is the FM SO_NEW_DOCUMENT_SEND_API1 relevant to my case?


Thanks.


Amine

0 Kudos

Hi Amine,

1) <ITAB> should have structure as of your output record in AL11. So after the statement SPLIT <STRING> AT ';' INTO TABLE <ITAB> all the data in the record will be breaked down using the ':' seperator. So after the READ TABLE <ITAB> INDEX 1 into workarea1 statement the details will be filled automatically to the fields of the workarea. so you can use the email related ( first field ) and then concatenate it with @gmail.com and use it forward.

2) FM should suffice.

Thanks

0 Kudos

if i want to recover the id from the itab in a workarea in order to build a mail. Should i do this?

READ TABLE <ITAB> INDEX 1 into workarea1.

Concatenate workarea1 ; @gmail.com into workarea2

Yes it'right, the table ITAB should have all values from a record (in your case the second one) of the file:

SPLIT <STRING> AT ';' INTO TABLE <ITAB>

this comand move the data of a single record of CSV (so with ; as separator) in a internal table, every record of this table will be a information of the CSV record.

In your case the ID mail should be the first information of the second line, so your code is ok


Hello;

Your file is corrupted, can you review it please?

Is the FM SO_NEW_DOCUMENT_SEND_API1 relevant to my case?

Yes you're right, something like this should work:


DATA: doc_data LIKE sodocchgi1.

DATA: reclist      LIKE somlreci1  OCCURS 0 WITH HEADER LINE.

DATA: packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE.

DATA: tb_text      TYPE soli       OCCURS 0 WITH HEADER LINE..

doc_data-obj_name  = 'MYMSG'.

doc_data-obj_descr = 'CSV file'.

doc_data-sensitivty = 'P'.

doc_data-priority   = '1'.

* Receiver

reclist-receiver = workarea2.    "ID mail

reclist-rec_type = 'U'.                "Receiver is a mail address

reclist-express  = 'X'.

APPEND reclist.

* Object list

*packing_list-head_start = 1.

*packing_list-doc_type   = 'RAW'.

*APPEND packing_list.

tb_text-line = 'Hello'.

APPEND tb_text.

tb_text-line = 'Your file is corrupted, can you review it please?'.

APPEND tb_text.

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

   EXPORTING

     document_data  = doc_data

     put_in_outbox  = 'X'

     commit_work    = 'X'

   TABLES

*   OBJECT_HEADER  = packing_list

     object_content = tb_text

     receivers      = reclist.

But the fm places the message in the queue of mails to be sent

The mailing is managed by program RSCONN01, this program can be called in your program or scheduled in a job

Max

amine_lamkaissi
Active Contributor
0 Kudos

Thanks a lot guys.

Amine