Friday, August 28, 2009
Code snippet for ALSM_EXCEL_TO_SAP
row TYPE i VALUE 1,
p_ifile type rlgrap-filename value 'c://documents and setting/input.xls',
t_tab type standard table of alsmex_tabline
col TYPE i VALUE 1,
rowsize TYPE i VALUE 4000,
columno TYPE i VALUE 40.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_ifile
i_begin_col = col
i_begin_row = row
i_end_col = columno
i_end_row = rowsize
TABLES
intern = t_tab
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc <> 0.
Write : / ,'Load failed'.
ENDIF.
Thursday, August 27, 2009
The Transactions and tables I have used
SM35------> To check the session overview. You can check the status of your request here.
SM36-----> Create a schedule for a program.
SM37-----> Check the scheduling of various jobs and edit it.
File-----> Here you can check the logical file path of any file. Or you can create one.
SHDB----> This is a transaction by which you can record the other transactions. I am still in the process of learning it. will post more shortly ....
tptco---> job scheduling
tbtcp --> Table that contains information about scheduled job.
Code Snippet for BP_JOB_CREATE.
There is very less information on this function module and how it works on internet.
I have used this function module to create batch jobs.
TYPES: BEGIN OF x_fm_input,
jobname TYPE char32, "Job name.
abapname TYPE char128, "Program name.
variant TYPE char255, "Variable name.
username TYPE tbtcstep-authcknam, "User Id
printer TYPE tbtcstep-pdest, "Output printer device
language TYPE tbtcstep-language, "Language
END OF x_fm_input.
DATA:
lt_excel_tab TYPE STANDARD TABLE OF x_fm_input,
t_inputtofunct TYPE STANDARD TABLE OF tbtcstep,
w_fm_input TYPE x_fm_input.
LOOP AT t_tab_excel
INTO w_tab_excel.
IF w_tab_excel-jobname IS NOT INITIAL AND
w_tab_excel-comments_controlm IS INITIAL.
w_fm_input-jobname = w_tab_excel-jobname.
w_fm_input-abapname = w_tab_excel-abapname.
w_fm_input-variant = w_tab_excel-variant.
w_fm_input-username = w_tab_excel-username.
APPEND w_fm_input TO lt_excel_tab.
CLEAR w_fm_input.
ENDIF.
ENDLOOP.
SORT lt_excel_tab BY jobname.
LOOP AT lt_excel_tab INTO w_fm_input.
w_fm_import-jobname = w_fm_input-jobname.
w_fm_import-status = c_stat. "Default Y
w_inputtofunct-program = w_fm_input-abapname.
w_inputtofunct-parameter = w_fm_input-variant.
w_inputtofunct-authcknam = w_fm_input-username.
w_inputtofunct-typ = c_inty. "Default A
w_inputtofunct-language = g_language.
w_inputtofunct-language = w_fm_input-language.
APPEND w_inputtofunct TO t_inputtofunct.
CLEAR w_inputtofunct.
AT END OF jobname.
***** TO CREATE JOBS *****
CALL FUNCTION 'BP_JOB_CREATE'
EXPORTING
job_cr_dialog = c_dlg "Default N
job_cr_head_inp = w_fm_import
TABLES
job_cr_steplist = t_inputtofunct
EXCEPTIONS
cant_create_job = 1
invalid_dialog_type = 2
invalid_job_data = 3
job_create_canceled = 4
OTHERS = 5.
REFRESH t_inputtofunct.
ENDAT.
CLEAR: w_inputtofunct, w_fm_import.
ENDLOOP.
If any doubt please post in comments I will surely answer. :)
cheers!!!
Monday, August 10, 2009
The journey of a process automation Business Value Add tool
Recently I read a book (forgot the name though) on ruby on rails. And that inspired me writing my next development in ABAP as a story. (ALL comments are welcome and if anyone wants my help just write his/her email id I will surely contact, I update the blog weekly).
Hmmm.. then
I have been shifted to control-M from SAP-ABAP for my project. While working for the team basically I have to schedule (To make it rosier let's say "automate") the jobs/programs that are being developed my fellow ABAPers. To be true the job kinda suck for a guy like me. The job description..
1) Take the requirement of how the jobs have to be setup from a central team that manages the process flow.
2) Check if all the abap program names and variants are valid or not (belive me this is the worst part).
3) Put some logic (uff finally) while creating some relevant jobname. (it involves basically concatenating two fields with some standard "confidential" suffix)
4) Update the massive excel sheet with that job name.
5) If there are some glitches say abap name is not there then mark my comment and send it back to the central team for clarification.
I always use to dream of some software that would do these tasks for me. Then came a hyped kind of thing- "Business Value Add on" . Hmm finally, my chance to fulfil my dream of creating a standalone utility as well as making my work eaiser.
Ok.. So finally the action began from this point.
well to automate the process the first task was jotting downt the requirement of our tool.
1. Create a dialogue to input file.
I named this function module as FILE_BROWSER.
for browsing files in SAP you can use a standard function module called 'F4_FILENAME'
It has the following parameters.
Exporting
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ' '
importing
file_name = fname.
fname is my variable that will store the name of file in it.
2. To read Excel file.
To read file you have two choices
a). GUI_UPDLOAD.
b) ALSM_EXCEL_TO_INTERNAL_TABLE
I used b) because that's exclusively for excel file and is pretty easy to use as compared to a).
The pattern of ALSM_EXCEL_TO_INTERNAL_TABLE is as followes. (I named this form as GET_EXCEL.
form GET_EXCEL.
data : l_file type rlgrap-filename,
l_firstrow type i value 1,
l_firstcol type i value 1,
l_rowsize type i value 4000,
l_columnnumber type i value 40,
l_file = p_ufile.
call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
Exporting
filename = l_file
i_begin_col = l_firstcol
i_begin_row = l_firstrow
i_end_col = l_columnnumber
i_end_row = l_rowsize
Tables
intern = t_tab
Exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.
if sy-subrc <> 0.
write: 'load not sucessful'.
ENDFORM.
**Good programming practice say you pass all the parameters via variables. Hence you will see some data declaration in the begining. l_ Stands for local data declaration. Well here it takes
a) The first row from which you want to start (1 in my case)
b) The first column (again 1)
c) Number of rows (I took 4000 to be safer side)
d) Number of columns (My excel sheet has 40)
e) An internal table t_tab whose type is like alsmex_tabline (you can check it in attributes column in function module)
That's pretty much it.
Wohla.... there you go all the data has been successfully transfered. (Phew.. finally).
But hey wait... The data that we have got is not like what we expected!!!!!
This graphical image will give you the better idea.
So next task.
3) Convert the data back to the form that we want.
First we created one internal table called t_tab_excel and one work are with name w_tab_excel
the data fields of this internal table is same as that of our excel sheet.
I named this function module as EXCEL_REINCARNATION
Form EXCEL_REINCARNATION
loop at t_tab into w_tab.
condense w_tab-value.
case w_tab-col.
when 1.
w_tab_excel-name = w_tab-value.
when 2.
w_tab_excel-class = w_tab-value.
when 3.
w_tab_excel-age = w_tab-value.
when 4.
w_tab_excel-marks = w_tab-value.
At end of row.
append w_tab_excel to t_tab_excel.
clear w_tab_excel.
endat.
endloop.
Endform.
Here we used an AT END OF ROW event to tell the SAP system when to update one row. The t_tab table is sorted with row column.
4) Concatenate the two fields and tell the program to do the "logic" for you .
Well I assume this was the easiest part. Although it uses one concept of "field symbols".
To put it in easier words. We use
So here we make a field symbol to just like our work are
FIELD-SYMBOLS:
now loop at (our internal table that we got) and use ASSIGNING keyword for field symbol
something like
LOOP AT t_tab ASSIGNING
and then perform the concatenation thing.
CONCATENATE
MOVE g_finalfield TO
** Note g prefix. That is the standard we follow when defining some global field.
Edit: In the testing phase I have got one errror. The variant name has spaces in between. Say
'test for country' for making a job in control-M the job should not have spaces in between. For that I have used condense statement in combination with NO-GAP for all the fields on the first loop where I am populating the values.
CONDENSE string NO-GAPS.
(I can share the whole code but I am using this a lot of time hence I have defined this field in global data)
So using this we need not worry about appending and that other tedious stuff.
And we get a ful fledged table with everything we wanted.
5) To check for the abapname and variants given in the sheet to be existing.
Well it was also pretty easy if you keep in mind some points
SAP basically store all (more or less all) the information in tables. So (right !!!) we have a table that contains the information about all the abap name and variants associtated with them. The table name is VARI AND TRDIR.
So bascially we have to compare our table entries for ABAP name and variant with the entries in VARI table and TRDIR table. Step are as follows:
1) Create a table t_vari similar to vari table.
2) Select all entries from our table containing entries as well as in standard table
SELECT * INTO TABLE t_vari
FROM vari
FOR ALL ENTRIES IN t_tab_excel
WHERE report = t_tab_excel-abapname.
SELECT * FROM trdir INTO TABLE t_trdir
FOR ALL ENTRIES IN t_tab_excel
WHERE name = t_tab_excel-abapname.
ELSE.
some custom message
*** Good coding practice.. WE used for all entries and not the whole table so that we only get relevant information hence making process less perfomance exhaustive.
**Also we have checked for our table to be initial or not so that we should not pass empty table.
Always make a point to check this when firing some query on standard tables.