Friday, August 28, 2009

Code snippet for ALSM_EXCEL_TO_SAP

DATA : l_file TYPE rlgrap-filename,
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

AL11--> It shows the directories in SAP system.
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.

BP_JOB_CREATE is a function module for creation of new jobs instead of using sm36 transaction.
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

Hi there,
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 when we have to alter one field of our internal table.

So here we make a field symbol to just like our work are

FIELD-SYMBOLS: TYPE table structure name.

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 -fieldname ',' INTO g_finalfield.

MOVE g_finalfield TO -final.

** 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.

Friday, July 24, 2009

SAP Advance Planner and Organiser. (APO)

SAP APO enables organizations to create a very close match between supply and demand by integrating purchasing, manufacturing, distribution, and transportation into one consistent model. SAP APO also includes functionality to enable organizations to dynamically determine how and when inventory should be distributed. The system draws on the data universally available in liveCache to optimize deployment plans based on available algorithms, as well as user rules and policies. In the benchmark, we use mass processing, which allows you to run the heuristic planning for large numbers of product-location combinations, with mass processing jobs running concurrently in the background. SAP APO is the application component of SAP SCM 4.0 and
mySAP SCM that helps you solve the seeming dilemma of improving customer service while lowering costs, yet increasing profits at the same time.
APO is an entire suite of supply chain planner applications that increase overall knowledge of the supply chain and provide forecasting, planning and optimization. There are eight application levels within SAP APO: network design, supply network planning, demand planning, production planning and detailed scheduling, transportation planning and vehicle scheduling, global availability and supply chain collaboration. The SAP APO is not a standalone application and requires a backend ERP system such as SAP R/3 for the execution. SAP R/3 communicate with SAP APO using the the APO Core Interface (CIF) which supplies SAP APO with the master and the transaction data in the real time. Interaction between the SAP R/3 at backend and the SAP APO is in real time and any data related to the production, the sales or the supplies is immediately transferred from one system to another without requiring any batch processing.

Alternative to APO in Oracle is Demantra
So how does APO differ from Demantra an Oracle product? To my understanding it is not able to fully perform PTPM (Predictive Trade Planning Management) capabilities? How does APO complete pre/post analysis of trade spend? Demantra is NetWeaver certified and able to run on an ERP backbone of SAP. Meaning it can be fully integrated. There are many customers currently doing this at Oracle from what I understand. What would you say to this option?
Apart from the different in PTPM, Demantra has a inbuilt Multi Lag Forecast accuracy analysis report also called Waterfall Model of analysis. This has to be built from scratch in SAP using BI. This is a best practice report and having it readily available is an advantage with Demantra
Materials in R/3 are known as Products in APO.
Allocations in APO are a way of controlling the stock levels permitted to be confirmed to a specific transactional requirement in R/3; e.g. a sales order
E.g. If an allocation is set up (via the APO planning book) to restrict the quantity of a product for a specific customer to a quantity of 10, when the actual unrestricted stock (ATP quantity) is 100, the second step of the ATP (Available To Promise) check will only allow that customer for this specific material to confirm a maximum of 10.
Key features:
· Allocations can be set up at different levels of an organization's structure through characteristic combinations
Allocations can be controlled periodically
· Allocations can be proportionate
· Allocations can be amended manually to satisfy the business's requirements during economic climate change or due to seasonal factors
· Allocations do not have a direct link to stock in R/3

LUW(Logical Unit of Work) in SAP


LUW and significance of it.
I have been working on SAP-ABAP from past one year. This is one of the question that has been particularly asked from me many times. So I have decided to properly document the question.
By the book defination taken from IBM site
A logical unit of work identifies input operations that are either committed or rolled back as a group. A unit of work begins when your code changes a recoverable resource; and ends when the first of these events occurs:






  • Your code invokes the system function sysLib.commit or sysLib.rollback to commit or roll back the changes


  • Your program executes a transfer with a commit specified


  • EGL run-time services performs a rollback in response to a hard error that is not handled in your code; in this case, all the programs in the run unit are removed from memory


In nomal day to day work, if we want to understand a LUW, we have to take an example of a small business.



While building a business(in fact any other) software( in any language), the most important thing is consistancy of data. Let's take an example of fligh booking software, The overall process can be divided into 2 parts,



1. The selection of flight then deducting the money from the user's account.



2.The transferal of the money from user's account to the concerned account.



In between these two process the data is inconsistant (the money belongs to no one) What if some system failure occurs at this point of time? Because the whole process comes under one LUW hence the data can be recovered( Technically Rollbacked).



Every application devloper should understand the fact that while communicating between any two logical points the consistancy should be maintained. If by any chance there is some error in between, we should be able to reset the database/data to the previous consitant state. (While studying the LUW concept , I came across a very interesting phenomenon called Transaction deadlock I will be writing about it and handling this condtion in ABAP here)



There are three units related to this term in SAP are :



1) Database Logical unit of work



The database LUW is a sequence of various database operations that usually ends with the database commit statement. If there are no errors in between the operations then only the LUW will be executed. Hence it's either 100% execution or zero. The changes to the database are not fully written untill database commit is executed. The whole system can be seen graphically as shown.

The commit to database can be performed in two way either implicit or explicit.
A work process when finished, triggers an implicit commit statement. This is due to the fact that a work process can use only execute one LUW. Hence as soon as it ends it has to close the LUW.
Example of work processes are
A. When a dialogue step is completed, (control changes back to SAP GUI).
B.When a new screen is created.
C. When a system sends a RFC (Remote function Call)


2) SAP LUW:
A SAP LUW may have a longer span than a DB LUW, there may be one or more DB LUW corresponding to one SAP LUW. A SAP LUW is triggered.
  • A transaction run with a new SAP LUW ( there may be more than one database commit used during one transaction)
  • A report run with a new SAP LUW.




2) SAP Logical unit of work



3) SAP Transaction.


What is BAPI??
Can we use commit or rollback in user exit and will it work ?