ABAP log

May 19, 2007

Creating and changing a production order from ABAP.

Filed under: ABAP, SAP — abaplog @ 9:21 pm

Creating production orders is one of common tasks when you have some external system that does the planning. Even if you work “directly” in R/3, you will probably need to create or change production orders. The SAP’s standard answer – BDC – is not really performant if you are processing a lot of data. To disappointment of any ABAPer, SAP provides no BAPIs to deal with that task (they do for order confirmations though). But fortunately, there are some other function modules that can help us. Before going further, I have to say that those functions can help you to create a production order, change its header parameters like scheduled start, or change its operations. Changing components, documents or production resources still has to be done with BDC (CO01 or CO02).

For production orders, SAP has implemented an interface for external planning systems (and even provided some documentation!). Their own “external” system, APO, does not use those functions (actually, one function supporting different tasks), but they are kind of official and you can find some documents on them in SAP help. Of course, the use is not limited to external systems and the function can be used directly from ABAP programs.

So, let’s start with creating an order. The function that we are going to use is CLOI_CHANGES_UPL_31. Don’t get confused by the “31” in its name. It works perfectly in 4.6 and (most likely) in later version of R/3. There is one inportant point that you have to keep in mind when working with the function: you are given just one call per session. That means, you can create one order or many orders, but with one call. After that, some internal state data is messed up and to save us from initializing it (which has to be done with some really internal functions), we better start a new sesion if we need to call the function again. So, in my example code below I am going to check whether the current session is not the last one allowed for the current user and call the function in a new task. Note that we have to do the check before EACH call unless we are really sure (I do it only once in the sample code below). Since it’s going to be an anynchronous call, we need to suspend our program until the function running in parallel finishes and get the return data into our program. This is achieved with standard SAP constructs like “performing … on end of task” in the CALL FUNCTION statement and “receive results” in the form that is performed. I am also using a global variable that tells me that the call is over. You can find more useful information in SAP OSS note 545860 that described those specifics about calling that function.

* 1. Create a production order.

data: t_cloi_ord_exp          type standard table of cloiord,
      lw_cloi_ord_exp         type cloiord,
      lt_cloi_msg_obj_log_exp type standard table of cloimoblog,
      lt_cloi_methods_exp     type standard table of cloimetlog,
      t_cloi_messages_exp     type standard table of cloimsglog,
      lw_cloi_messages_exp    type cloimsglog,
      ls_cloi_if_par_v2       like cloiifpar.

data:     lt_cloi_ordu        type standard table of cloiordu,
          lw_cloi_ordu        type cloiordu,
          lt_cloi_ordi        type standard table of cloiordi,
          lw_cloi_ordi        type cloiordi,
          lt_cloi_ord_opru    type standard table of CLOIOPERU,
          lw_cloi_ord_opru    type CLOIOPERU.

data:
  lf_date   like sy-datum,
  lf_date2  like sy-datum,
  f_flag(1) type c,
  f_aufnr   like aufk-aufnr.

data: f_act_sess like sm04dic-counter,
      f_max_sess like sm04dic-counter.


lf_date = sy-datum + 7.   "let's try to schedule it next week
lf_date2 = sy-datum + 14.

clear: ls_cloi_if_par_v2.

* those fields below are described in SAP help, but nevermind...
ls_cloi_if_par_v2-commitflg   = 'C'.
ls_cloi_if_par_v2-r3_version  = '40'.
ls_cloi_if_par_v2-metlog_req  = 'X'.
ls_cloi_if_par_v2-msglog_req  = 'X'.
ls_cloi_if_par_v2-msgobj_req  = 'X'.
ls_cloi_if_par_v2-ord_req     = 'X'.
ls_cloi_if_par_v2-ordseq_req  = 'X'.
ls_cloi_if_par_v2-ordopr_req  = 'X'.

* in case if we have external number range,
* we have to provide the future order number
lw_cloi_ordi-extaufnr = '1234567890'.

* material
lw_cloi_ordi-field =  'MATNR'.
lw_cloi_ordi-value =  'MATNUM1'.
append lw_cloi_ordi to lt_cloi_ordi.

* plant
lw_cloi_ordi-field =  'WERKS'.
lw_cloi_ordi-value = 'W001'.
append lw_cloi_ordi to lt_cloi_ordi.

* order type
lw_cloi_ordi-field =  'AUART'.
lw_cloi_ordi-value = 'PP01'.
append lw_cloi_ordi to lt_cloi_ordi.

* quantity
lw_cloi_ordi-field =  'BDMNG'.
lw_cloi_ordi-value = '100'.
append lw_cloi_ordi to lt_cloi_ordi.

lw_cloi_ordi-field =  'GLTRP'.    "finish date
lw_cloi_ordi-value = lf_date2.
append lw_cloi_ordi to lt_cloi_ordi.

lw_cloi_ordi-field =  'GSTRP'.    "start date
lw_cloi_ordi-value = lf_date.
append lw_cloi_ordi to lt_cloi_ordi.

* now check if we have one free session
call function 'TH_USER_INFO'
     importing
          act_sessions = f_act_sess
          max_sessions = f_max_sess
     exceptions
          others       = 1.

if sy-subrc  0 or f_act_sess >= f_max_sess.
  message e208(00) with 'No more free sessions'.
endif.

* and finally, let it happen!
clear: f_flag.
call function 'CLOI_CHANGES_UPL_31'
     starting new task 'CLOI_TEST'
     performing receive_res on end of task
     exporting
          cloi_if_par          = ls_cloi_if_par_v2
     tables
          cloi_ordi_imp        = lt_cloi_ordi
          cloi_method_log_exp  = lt_cloi_methods_exp
          cloi_message_log_exp = t_cloi_messages_exp
          cloi_msg_obj_log_exp = lt_cloi_msg_obj_log_exp
          cloi_ord_exp         = t_cloi_ord_exp.

* wait till the global variable will be set by our form
wait until f_flag = 'X'.

This is the form that is used to receive results and signalize
the end of processing:

form receive_res using taskname.

  data: lt_cloi_msg_obj_log_exp type standard table of cloimoblog,
        lt_cloi_methods_exp     type standard table of cloimetlog.

  receive results from function 'CLOI_CHANGES_UPL_31'
     tables
       cloi_method_log_exp        = lt_cloi_methods_exp
       cloi_message_log_exp       = t_cloi_messages_exp
       cloi_msg_obj_log_exp       = lt_cloi_msg_obj_log_exp
       cloi_ord_exp               = t_cloi_ord_exp.

  f_flag = 'X'.

endform.

The list of created production orders is returned back in the global table t_cloi_ord_exp. Now, let’s release them. (Setting, for example, technically complete status is similar). We are going to use the parameter cloi_ordu_imp instead of cloi_ordi_imp (I for insert, U for update). We populate the field FIELD with the value “METHOD” to say that we are not setting any values but we want to call a “method” of releasing the order (method in the same sense as in object programming).

* release created prod order

clear: lt_cloi_ordu.
loop at t_cloi_ord_exp into lw_cloi_ord_exp.
  check not lw_cloi_ord_exp-aufnr is initial.
  clear: lw_cloi_ordu.
  lw_cloi_ordu-aufnr  =  lw_cloi_ord_exp-aufnr.
  f_aufnr  =  lw_cloi_ord_exp-aufnr.
*   Methods:
*     SetTechnicalComplete
*     RevokeTechnicalCompletion
*     Schedule
*     Release
*     SetUserStatus
*     RevokeUserStatus
  lw_cloi_ordu-field  =  'METHOD'.
  lw_cloi_ordu-value  =  'Release'.
  append lw_cloi_ordu to lt_cloi_ordu.
endloop.

clear: lt_cloi_methods_exp, t_cloi_messages_exp,
       lt_cloi_msg_obj_log_exp, t_cloi_ord_exp.

clear: f_flag.
call function 'CLOI_CHANGES_UPL_31'
     starting new task 'CLOI_TEST'
     performing receive_res on end of task
     exporting
          cloi_if_par          = ls_cloi_if_par_v2
     tables
          cloi_ordu_imp        = lt_cloi_ordu
          cloi_method_log_exp  = lt_cloi_methods_exp
          cloi_message_log_exp = t_cloi_messages_exp
          cloi_msg_obj_log_exp = lt_cloi_msg_obj_log_exp
          cloi_ord_exp         = t_cloi_ord_exp.

wait until f_flag = 'X'.

Now, as the order is created and released, let’s do some simple change. We are going to change the workcenter of the order’s operation.

* change the work center of the operation 0010

clear: lt_cloi_ord_opru.
clear: lw_cloi_ord_opru.
lw_cloi_ord_opru-aufnr  =  f_aufnr.
lw_cloi_ord_opru-aplfl  =  0.
lw_cloi_ord_opru-vornr  =  '0010'.
lw_cloi_ord_opru-field  =  'ARBID'.
* note that this is the internal ID of workcenter!
lw_cloi_ord_opru-value  =  '10000001'.
append lw_cloi_ord_opru to lt_cloi_ord_opru.

clear: lt_cloi_methods_exp, t_cloi_messages_exp,
       lt_cloi_msg_obj_log_exp, t_cloi_ord_exp.

clear: f_flag.
call function 'CLOI_CHANGES_UPL_31'
     starting new task 'CLOI_TEST'
     performing receive_res on end of task
     exporting
          cloi_if_par          = ls_cloi_if_par_v2
     tables
          cloi_ord_opru_imp    = lt_cloi_ord_opru
          cloi_method_log_exp  = lt_cloi_methods_exp
          cloi_message_log_exp = t_cloi_messages_exp
          cloi_msg_obj_log_exp = lt_cloi_msg_obj_log_exp
          cloi_ord_exp         = t_cloi_ord_exp.

wait until f_flag = 'X'.

That’s it! The function that we use doesn’t let us do anything we may need, but it can be very useful anyways. It’s stable and “blessed” by SAP, and I have seen it used in third-party products that need to be integrated with SAP. If you have any doubts, or your supervisor does, don’t forget to point to the SAP help documentation. It’s a small reassurance that it’s not one of those “internal only” functions that are better to avoid. And, the main point will of course be the performance if compared with BDC.

May 2, 2007

SAP Locks Mystery.

Filed under: ABAP, SAP — abaplog @ 8:19 pm

I was kicked once by some problem that lies on how SAP behaves when you create locks with Enqueue functions. The locks were at the first glance mysteriously released (or seemed to allow concurrent locking, though it was requested as exclusive).

Using locks in SAP is easy, and the functionality is well described in SAP Help and in the ABAP Knowledge Corner article from Richard Harper.

So, because of the apparent simplicity, all that seemed very suspicious. After playing with debugger and SM12, I have found that the problem is with the parameter _SCOPE of the locking functions. By default, as generated by SAP template generator in SE38, it’s set to 2, which means it’s released in the calling program and transferred to the update task, if the program does such calls. As soon as the update is over, the lock is deleted. If you have COMMIT AND WAIT after the call function, it can be very amusing to have the lock gone. The solution was to set it to 1, so that the lock ownership is not transferred (the functions I call do not depend on the objects I lock).

To extend the standard documentation and the article from Rich, I would add the following regarding the parameters for ENQUEUE functions:

The parameter _SCOPE defines where the lock affects the data in case the locking program does update task calls (which may be implicitly done by calling normal function modules, BAPIs or transactions).

With ‘1’, the lock is released only when the ***caller*** program finishes or releases the lock explicitly. If the lock is done in exclusive mode, the update task cannot lock the same object (and normally cannot update it).

With ‘2’, the lock is “moved” to the update task. The update task may release it explicitly, otherwise the lock will be released when update task is finished. After calling the update task, the caller has nothing to do with the lock. Example pseudocode:

Example 1:

call ENQUEUE with _SCOPE=2
call function FUNC. “synchronous call – no update task
call DEQUEUE “clean up – lock is released

Example 2:

call ENQUEUE with _SCOPE=2
call function FUNC in update task. “async call
commit work and wait. “we want to wait for update till it’s done
call DEQUEUE “oops! we have nothing to release here! the lock is gone as soon as FUNC exits!
“Other processes can lock the same object, even as we may expect them to be denied

With ‘3’, the lock created by caller is “copied”. After that, both caller and update task own their locks. The locks will be released either explicitly with dequeue, or when respective process is done. If the original lock was exclusive, as long as one of those two locks exist, no other processes can obtain a new exclusive lock.

Blog at WordPress.com.