Thursday, May 18, 2017

Simple BADI for Material Check

At the time of material creation we often activate Batch Management. It means different materials will have different batches. Hence, material & batch both will behave as a unique. At this point the Valuation Category needs to be activated. If we don’t activate Valuation Category then there will be no meaning to activate batch management. Because Valuation Category always ensures that the different batch will hold different price of the materials. If it is not activated in this case, then all materials need to satisfy average price of the batch. At this point the valuation will be wrong for those materials.

In the following BADI we have created a simple logic by which Valuation Category will be mandatory if Batch Management is activated at the time of creating a material; t-code MM01. The required BADI is BADI_MATERIAL_CHECK. Now we implement this step by step.

1. Go to SE18 & enter BADI name and display.


2. We need to add our custom code inside the CHECK DATA.


3. Go to SE19 & enter the BADI name and create Implementation.


4. Enter implementation name and then go to Interface.


5. Double click on CHECK DATA method.


6. It will open with the editor. Write the required code here. Then save -> check -> active.


7. Go back and click on the Active button. By this the BADI is activated.


Required Code:


  METHOD if_ex_badi_material_check~check_data.

    
IF    wmarc-xchar = 'X'
      
AND ( wmara-mtart = 'ROH' OR wmara-mtart = 'VERP' )
      
AND sy-tcode = 'MM01'.

      
IF wmbew-bwtty IS INITIAL.
        
MESSAGE 'Valuation Category is mandatory (Value = X)'
        
TYPE 'I' DISPLAY LIKE 'E'.
        
LEAVE LIST-PROCESSING.
        
LEAVE TO SCREEN sy-dynnr.
      
ENDIF.
    
ENDIF.

  ENDMETHOD.

Material Creation:

1. Now go to MM01.


2. Select the required views and enter.



3. Enter plant & s-loc, and then enter.



4. Check the Batch Management.


5. Create the Inspection Setup.




6. Now keep Valuation Category blank and then try to save the material.


7. It will give the Information (display like error) message.


8. Now enter the Valuation Category and the save the material.




9. The material has been created and we can check the Valuation Category details in the MARC table.


Tuesday, April 11, 2017

Table Control with Uploading from Clip Board

Sometimes we just need to copy & paste a lot of records into table control. Now table control lines are specific (say 13 lines) as per design level. If we want to paste more lines (say 100) into this table control then it will not happen. In this case the system will paste only first 13 lines into the table control which is visible though the copy is there (RAM). At this point we need to paste total 100 lines into that table control.

We cannot achieve this by just paste (ctrl + v) option. Here we create a button by which we shall achieve this purpose. This button functions exactly like ‘Upload from Clip Board’ which is standard. Here we need to call method: cl_gui_frontend_services=>clipboard_import

This method generates an internal table which contains the total copy data. If we select only one field then the table will have only one field. If we select more than one field then internal table will have more than one field. Hence the table structure depends on the selected copy.


In the following example we just create a table control.

INCLUDE ztabc_top                               .  " global Data
INCLUDE ztabc_o01                               .  " PBO-Modules
INCLUDE ztabc_i01                               .  " PAI-Modules
INCLUDE ztabc_f01                               .  " FORM-Routines 

Top Include:

TABLESzsd_pallet_ser.
TYPESBEGIN OF ty_ser,
         sernr TYPE zsd_pallet_ser-sernr,
       END OF ty_ser.
DATAwa_ser TYPE ty_ser,
      it_ser TYPE TABLE OF ty_ser.

DATAok_code_9000 TYPE sy-ucomm.

CONTROLStabc_9000 TYPE TABLEVIEW USING SCREEN 9000.

PBO Include:

MODULE status_9000 OUTPUT.
  SET PF-STATUS 'PF_9000'.
  SET TITLEBAR 'T_9000'.
ENDMODULE.

MODULE tabc_9000_pbo OUTPUT.

  DESCRIBE TABLE it_ser LINES sy-dbcnt.
  tabc_9000-current_line sy-loopc.
  tabc_9000-lines sy-dbcnt.

  zsd_pallet_ser-sernr wa_ser-sernr.

ENDMODULE.

PAI Include:

MODULE user_command_9000 INPUT.

  CASE ok_code_9000.
    WHEN 'BACK'.
      PERFORM back_9000.
    WHEN 'UPL'.
      PERFORM upload_data.
  ENDCASE.

ENDMODULE.

MODULE tabc_9000_pai INPUT.

  READ TABLE it_ser INTO wa_ser INDEX tabc_9000-current_line.
  IF sy-subrc 0.
    MODIFY it_ser FROM wa_ser INDEX tabc_9000-current_line.
  ENDIF.

ENDMODULE.

Form Include:

FORM back_9000 .

  CLEARok_code_9000.
  LEAVE PROGRAM.

ENDFORM.

FORM upload_data .

  TYPESBEGIN OF ty_clip,
           data TYPE char18,
         END OF ty_clip.

  DATAit_clip TYPE TABLE OF ty_clip,
        wa_clip TYPE ty_clip.

  CALL METHOD cl_gui_frontend_services=>clipboard_import
    IMPORTING
      data                 it_clip
    EXCEPTIONS
      cntl_error           1
      error_no_gui         2
      not_supported_by_gui 3
      OTHERS               4.

  IF sy-subrc 0.
    LOOP AT it_clip INTO wa_clip.
      wa_ser-sernr wa_clip-data.
      APPEND wa_ser TO it_ser.
      CLEARwa_serwa_clip.
    ENDLOOP.
  ENDIF.

  CLEAR ok_code_9000.

ENDFORM.

Here we have generated the internal table by that method and then populate the table control’s internal table. To avoid duplicate entries we just clear the OK code inside there.

Now create a T-code and execute it. The table control will look like follows. We have created an upload button to copy from clipboard. In the visible section we have only 13 rows.


Now from excel we have selected 25 rows and copy (ctrl + c) it.


Now go to SAP screen and click on UPLOAD button. The 25 rows have been inserted and only 13 rows are visible because of table control design.


Now we scroll and check whether the purpose got successful or not.




Saturday, February 4, 2017

ABAP Help Information

Colour Code for WRITE Statement:

Colour No. Colour
1 Gray-blue (blue sky)
2 Light Gray (light cyan)
3 Yellow
4 Blue-green (between light cyan and blue sky)
5 Green
6 Red
7 Violet (orange)

Sample:
COLOR INTENSIFIED OFF
COLOR INTENSIFIED ON
COLOR INVERSE ON


Message Type:

Sometimes we need to show error message for specific situation. But we need to stay on that screen. To achieve this we can fix this into Information or Success message with display like Error.

REPORT zsr_test.

PARAMETERS p_inp TYPE char12.

IF p_inp 'INFORMATION'.
  MESSAGE 'Information Message' TYPE 'I'.


ELSEIF p_inp 'ERROR'.
  MESSAGE 'Error Information' TYPE 'I' DISPLAY LIKE 'E'.

ELSEIF p_inp 'SUCCESS'.
  MESSAGE 'Success' TYPE 'S'.

ELSEIF p_inp 'FALSE'.
  MESSAGE 'False' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.


Output options Field Catalog:

outputlen (column width)
       Value range: 0 (initial), n
       For fields with reference to the Data Dictionary you can leave this
       parameter set to initial.
       For fields without reference to the Data Dictionary (program fields)
       you must set the parameter to the desired field output length on the
       list (column width).
       initial = The column width is derived from the output length of the
       referenced field (domain) in the Data Dictionary.
       n = The column width is n characters.
key (key column)
         Value range: SPACE, 'X'
         'X' = Key field (colored output for key fields)
         Key fields cannot be hidden interactively by the user.
         Parameter FIELDCAT-NO_OUT must be left set to initial.
         For exceptions, see the documentation on parameter FIELDCAT-KEY_SEL.
key_sel (key column that can be hidden)
         Value range: SPACE, 'X'
         Only relevant if FIELDCAT-KEY = 'X'
         Key field that can be hidden interactively by the user.
         The user cannot interactively change the sequence of the key
         columns.
         As with non-key fields, output control is performed using parameter
         FIELDCAT-NO_OUT.
no_out (field in the available fields list)
         Value range: SPACE, 'X'
         'X' = Field is not displayed on the current list.
         The field is available to the user in the field list and can be
         interactively selected as a display field.
         At row level, the user can use the detail function to display the
         content of these fields.
         See also the documentation on the 'Detail screen' section of
         parameter IS_LAYOUT.
no_zero (suppress zeros)
         Value range: SPACE, 'X'
         Only relevant to value fields
         'X' = Supress zeros
no_sign (no +/- sign)
         Value range: SPACE, 'X'
         Only relevant to value fields
         'X' = Value output without +/- signs.
tech (technical field)
         Value range: SPACE, 'X'
         'X' = Technical field
         The field cannot be output on the list and cannot be shown
         interactively by the user.
         The field may only be used in the field catalog (not in IT_SORT,...).
emphasize (highlight column in color)
        Value range: SPACE, 'X' or 'Cxyz' (x:'1'-'9'; y,z: '0'=off '1'=on)
        'X' = The column is highlighted in the default color for color
        highlighting.
        'Cxyz' = The column is highlighted in the coded color:
-   C: Color (coding must start with C)
 -   x: Color number
-   y: Intensified
 -   z: Inverse
hotspot (column as hotspot)
        Value range: SPACE, 'X'
        'X' = The cells of the column are output as a hotspot.
do_sum (calculate totals for column)
        Value range: SPACE, 'X'
        'X' = Totals are calculated for this field of the internal output
        table.
        This function can also be used interactively by the user.
no_sum (totals calculation not allowed)
        Value range: SPACE, 'X'
        'X' = No totals may be calculated for this field although the data
        type of the field allows totalling.
Formatting column contents
icon (icon)
         Value range: SPACE, 'X'
         'X' = The column contents are displayed as an icon.
         The column contents of the internal output table must consist of
         valid icon strings (@xx@).
         The caller should consider the problem of printing icons.
symbol (symbol)
         Value range: SPACE, 'X'
         'X' = The column contents are output as a symbol.
         The column contents of the internal output table must consist of
         valid symbol characters.
         The caller should consider the problem of printing symbols.
         Although symbols can generally be printed, they are not always shown
         correctly depending on the printer configuration.
just (justification)
         Value range: SPACE, 'R', 'L', 'C'
         Only relevant to fields of data type CHAR or NUMC
         ' ' = Default justification according to data type
         'R' = Right-justified output
'L' = Left-justified output
         'C' = Centered output
         The justification of the column header depends on the justification
         of the column contents. You cannot justify the column header
         independently of the column contents.
lzero (leading zeros)
         Value range: SPACE, 'X'
         Only relevant to fields of data type NUMC
         By default, NUMC fields are output in the ALV right-justified
         without leading zeros.
         'X' = Output with leading zeros
edit_mask (field formatting)
         Value range: SPACE, mask
         mask = See documentation on the WRITE formatting option
         USING EDIT MASK mask
         Using mask = '== conv' you can force an output conversion conv.


ALV Color Code:

* Colour code : *
* Colour is a 4-char field where : *
* - 1st char = C (color property) *
* - 2nd char = color code (from 0 to 7) *
* 0 = background color *
* 1 = blue *
* 2 = gray *
* 3 = yellow *
* 4 = blue/gray *
* 5 = green *
* 6 = red *
* 7 = orange *
* - 3rd char = intensified (0=off, 1=on) *
* - 4th char = inverse display (0=off, 1=on) *
* *
* Colour overwriting priority : *
* 0 = background color *
* 1. Line *
* 2. Cell *
* 3. Column *


Calling Standard Buttons in Custom PF Status:

- Go to program SAPLSLVC_FULLSCREEN 
- Go to GUI status STANDARD_FULLSCREEN 
- Add required standard code to your custom PF status


Fetching System Terminal Name:

DATA: opcode_usr_attr(1) TYPE VALUE 5,
      terminal           TYPE usr41-terminal.

CALL 'ThUsrInfo' ID 'OPCODE' FIELD opcode_usr_attr
  ID 'TERMINAL' FIELD terminal.

WRITE:/ 'Terminal:', terminal.


Finding IP Address:

  CALL METHOD cl_gui_frontend_services=>get_ip_address
    RECEIVING
      ip_address           = ip (string)
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS                4.



How to unset MS word as editor:
Go to report RSCPSETEDITOR & execute. Unselect the Smartform as follows.





Different Viewing of Output as Modal Dialog:
cl_demo_output=>displayit_mara ). 

Wednesday, February 1, 2017

Multiple ALVs with Different Header

In the following example we have prepared two ALV Grid report in one single screen. There are two custom screens – one is for finished goods materials and another is for Raw materials. We also have mentioned two different header texts by using the Layout. Step by step approach is as follows:

1. At first create a report and create a custom screen (9000) without modifying the standard one.


2. Go to Layout and then create the custom containers (here 2 is in our example).


3. Now create the PBO & PAI modules at the custom screen.


4. In PBO we are creating custom container, ALV grid & calling the ALV grid method along with PF status & Title Bar.





5. In PAI the BACK, EXIT & CANCEL buttons are activated.


The detailed coding is as follows.

REPORT zsr_test NO STANDARD PAGE HEADING.

TABLESmara.

"-General Material Data
TYPESBEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         ersda TYPE mara-ersda,
         mtart TYPE mara-mtart,
       END OF ty_mara.
DATAwa_mara TYPE ty_mara,
      it_mara TYPE TABLE OF ty_mara.

"-Material Descriptions
TYPESBEGIN OF ty_makt,
         matnr TYPE makt-matnr,
         maktx TYPE makt-maktx,
       END OF ty_makt.
DATAwa_makt TYPE ty_makt,
      it_makt TYPE TABLE OF ty_makt.

"-Output Table
TYPESBEGIN OF ty_out,
         matnr TYPE mara-matnr,
         maktx TYPE makt-maktx,
         ersda TYPE mara-ersda,
         mtart TYPE mara-mtart,
       END OF ty_out.
DATAwa_out TYPE ty_out,
      it_fg  TYPE TABLE OF ty_out"-Finished Goods Table
      it_rm  TYPE TABLE OF ty_out"-Raw Materials Table

"-Field Catalouge
DATAwa_fcat TYPE lvc_s_fcat,
      it_fcat TYPE TABLE OF lvc_s_fcat.

"-Custom Container Object
DATAob_cont_fg TYPE REF TO cl_gui_custom_container,
      ob_cont_rm TYPE REF TO cl_gui_custom_container.

"-ALV Grid Object
DATAob_grid_fg TYPE REF TO cl_gui_alv_grid,
      ob_grid_rm TYPE REF TO cl_gui_alv_grid.

"-Layout
DATAwa_lay_fg TYPE lvc_s_layo,
      wa_lay_rm TYPE lvc_s_layo,
      ok_code   TYPE sy-ucomm.

INITIALIZATION.
  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
  SELECT-OPTIONS   s_ersda FOR mara-ersda OBLIGATORY.
  SELECTION-SCREEN END OF BLOCK b1.

CLASS cl_material DEFINITION.
  PUBLIC SECTION.
    METHODS:
      get_material,
      prepare_output,
      fieldcat.
ENDCLASS.

CLASS cl_material IMPLEMENTATION.
  METHOD get_material.
    IF s_ersda[] IS NOT INITIAL.
      SELECT matnr ersda mtart
        FROM mara INTO TABLE it_mara
        WHERE ersda IN s_ersda
          AND mtart IN 'FERT''ROH' ).

      "-FERT - Finished Goods
      "-ROH  - Raw Materials

      IF sy-subrc 0.
        SORT it_mara BY matnr.
        SELECT matnr maktx
          FROM makt INTO TABLE it_makt
          FOR ALL ENTRIES IN it_mara
          WHERE matnr it_mara-matnr.

        IF sy-subrc 0.
          SORT it_makt BY matnr.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDMETHOD.

  METHOD prepare_output.
    IF it_mara IS NOT INITIAL.
      LOOP AT it_mara INTO wa_mara.
        READ TABLE it_makt INTO wa_makt
        WITH KEY matnr wa_mara-matnr
        BINARY SEARCH.
        IF sy-subrc 0.
          wa_out-maktx wa_makt-maktx.
        ENDIF.
        wa_out-matnr wa_mara-matnr.
        wa_out-ersda wa_mara-ersda.
        wa_out-mtart wa_mara-mtart.

        IF wa_out-mtart 'FERT'.
          APPEND wa_out TO it_fg.         "-Finished Goods Table
          CLEARwa_outwa_marawa_makt.

        ELSE.
          APPEND wa_out TO it_rm.         "-Raw Materials Table
          CLEARwa_outwa_marawa_makt.
        ENDIF.
      ENDLOOP.
    ENDIF.
    FREEit_marait_makt.
  ENDMETHOD.

  METHOD fieldcat.

    "-Creating Field Catalouge
    REFRESH it_fcat.
    DATAlv_col TYPE VALUE 0.
    lv_col            lv_col + 1.
    wa_fcat-col_pos   lv_col.
    wa_fcat-fieldname 'MATNR'.
    wa_fcat-reptext   'Material Code'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            lv_col + 1.
    wa_fcat-col_pos   lv_col.
    wa_fcat-fieldname 'MAKTX'.
    wa_fcat-reptext   'Description'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            lv_col + 1.
    wa_fcat-col_pos   lv_col.
    wa_fcat-fieldname 'ERSDA'.
    wa_fcat-reptext   'Created On'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    lv_col            lv_col + 1.
    wa_fcat-col_pos   lv_col.
    wa_fcat-fieldname 'MTART'.
    wa_fcat-reptext   'Material Type'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    "-Creating Layout
    wa_lay_fg-zebra      'X'.
    wa_lay_fg-cwidth_opt 'X'.
    wa_lay_fg-grid_title 'Finished Goods Materials'.
    wa_lay_rm-zebra      'X'.
    wa_lay_rm-cwidth_opt 'X'.
    wa_lay_rm-grid_title 'Raw Materials'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATAob_material TYPE REF TO cl_material.
  CREATE OBJECT ob_material.
  CALL METHODob_material->get_material,
               ob_material->prepare_output,
               ob_material->fieldcat.
  CALL SCREEN 9000.                  "-Calling Custom Screen

*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*       PBO of 9000
*----------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
  SET PF-STATUS 'PF_9000'.
  SET TITLEBAR 'MAT'.

  PERFORM finished_goods_alv.
  PERFORM raw_materials_alv.

ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       PAI of 9000
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.

  IF   ok_code 'BACK'
    OR ok_code 'EXIT'
    OR ok_code 'CANCEL'.
    FREEob_cont_fgob_grid_fgob_cont_rmob_grid_rm,
          it_fcatit_fgit_rm.
    LEAVE TO SCREEN 0.
  ENDIF.

ENDMODULE.
*&---------------------------------------------------------------------*
*&      Form  FINISHED_GOODS_ALV
*&---------------------------------------------------------------------*
*       ALV of FG
*----------------------------------------------------------------------*
FORM finished_goods_alv .
  CREATE OBJECT ob_cont_fg
    EXPORTING
      container_name 'CONT_FG'"-Creating Container
  "-In layout CONT_FG name is given

  CREATE OBJECT ob_grid_fg
    EXPORTING
      i_parent ob_cont_fg"-Creating ALV Grid

  "-ALV Grid Display Method
  CALL METHOD ob_grid_fg->set_table_for_first_display
    EXPORTING
      is_layout       wa_lay_fg
    CHANGING
      it_fieldcatalog it_fcat
      it_outtab       it_fg.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  RAW_MATERIALS_ALV
*&---------------------------------------------------------------------*
*       ALV of RM
*----------------------------------------------------------------------*
FORM raw_materials_alv .
  CREATE OBJECT ob_cont_rm
    EXPORTING
      container_name 'CONT_RM'"-Creating Container
  "-In layout CONT_RM name is given

  CREATE OBJECT ob_grid_rm
    EXPORTING
      i_parent ob_cont_rm"-Creating ALV Grid

  "-ALV Grid Display Method
  CALL METHOD ob_grid_rm->set_table_for_first_display
    EXPORTING
      is_layout       wa_lay_rm
    CHANGING
      it_fieldcatalog it_fcat
      it_outtab       it_rm.
ENDFORM.

Output.