Wednesday, April 23, 2014

Code useful for executing ALV Grid report in Background mode causing short dump

 
Whenever an ALV grid control program is executed in background, a short dump occurs
Error:   GUI cannot be reached 
Reason
Whenever an ALV grid control program is executed in foreground it tries to create some front-end related objects. But when the same program is executed in background, the above error occurs as it fails to create front-end objects (GUI objects). So in order to rectify this problem, we need to use the docking container instead of custom container.
The docking container doesn't need any of the custom controls on the screen, instead it attaches an area to any or all of the four edges of the screen (top, left, right or bottom).
The behavior of the areas in the container is determined by the sequence in which they are initialized. Docking Containers are attached to the screen from the inside out. This means that when you create a second container, it is attached to the edge of the screen, and the container that was already there is pushed outwards.
We can use the below code when ALV is used in background programs
DATA : or_doc TYPE REF TO cl_gui_docking_container.
IF cl_gui_alv_grid=>offline( ) IS INITIAL.
 CREATE OBJECT or_custom_container
   EXPORTING
    container_name = c_container.
 CREATE OBJECT or_grid
   EXPORTING
    i_parent = or_custom_container.
ELSE.
  CREATE OBJECT or_grid
   EXPORTING
    i_parent = or_doc.
ENDIF.
We can also use  SAP system variable SY-BATCH  is ‘X’ to check if the program is being executed in background or foreground


Wednesday, July 25, 2012

To find the IP address of application server in SAP

TYPES: BEGIN OF kernel_version,
         key(21) TYPE c,
         data(69) TYPE c,
      END OF kernel_version.
  DATA : gt_kernel_version TYPE STANDARD TABLE OF kernel_version,
         gw_kernel_version TYPE kernel_version.
  DATA ip_address(69) TYPE c.
  CALL 'SAPCORE' ID 'ID' FIELD 'VERSION'
                 ID 'TABLE' FIELD gt_kernel_version[].
  READ TABLE gt_kernel_version INTO gw_kernel_version INDEX 11.
  ip_address = gw_kernel_version-data.
WRITE ip_address.

Shift negative sign from right to left in ABAP

DATA: gv_amount type konp-kbetr.
DATA: gv_amount_text(15)  type c.
gv_amount = 100.
gv_amount = gv_amount * -1.
gv_amount_text = gv_amount.
Now value will be 100 -
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
    CHANGING
      value         = gv_amount_text.
Now final value will be -100

Details of messages being displayed on WEB UI in SAP CRM

In order to figure out the details of the messages being displayed on WEB UI in SAP CRM for analysis purpose, the following steps can be performed:
1.      Put a break-point in the method ADD_MESSAGE of the message service class CL_BSP_WD_MESSAGE_SERVICE  and replicate the scenario for which the error message is appearing on the WEB UI. The  import parameters IV_MSG_ID and IV_MSG_NUMBER of this method would have the message details.
2.      The standard database table T100 maintains the standard error messages and their message class and message number.

3.      The method call stack in the debugger (‘Standard’ tab) can then be used to trace where in the logic this message is being added.

Grey out fields on ME21N/ME22N/ME23N

How to grey out fields on ME21N/ME22N/ME23N

For certain Purchase orders, based on some condition, we need to grey out the fields NETPR & KBETR.

In order to do this, we need to find the right BADI or the right user exit to code our functionality. For this debug tool is a must to know where and how these fields are populated on the purchase order screen.

This functionality can be achieved by following the below steps.

For the PO creation/change screen below is the solution to gray out the required fields (NETPR & KBETR)

1. for NETPR:

The field catalog that fills NETPR is TC_1211 Actually in standard program SAPLMEGUI, there is a call to the FM MEGUI_BUILD_PO_ITEMOV_PLUGIN.. Inside that, in line 44, TC_1211 gets populated. This contains all field catalog values. So in BADI, code to disable field NETPR by reading values using field symbols.

In SE19:

Create Implementation — > Classic BADI –> ME_ACTV_CANCEL_PO –> Implementation name ZCL_IM_TEST

In method CHECK_ACTIVITY code the following:

Method IF_EX_ME_ACTV_CANCEL_PO~CHECK_ACTIVITY.
FIELD-SYMBOLS :<fs>        TYPE ANY TABLE,
<ls>        TYPE ANY,
<lr_screen> TYPE screen.

ASSIGN (‘(SAPLMEGUI)TC_1211-COLS’) TO <fs>.
IF sy-subrc IS INITIAL.
LOOP AT <fs> ASSIGNING <ls>.
ASSIGN COMPONENT ’SCREEN’ OF STRUCTURE <ls> TO <lr_screen>.
IF <lr_screen>-name = ’MEPO1211-NETPR’.
<lr_screen>-input = 0.
ENDIF.
ENDLOOP.
ENDIF.

endmethod.

Activate the implementation. NETPR will be disabled in the header when u try to create or change purchase order.

2. for KBETR

In Screen 6201 of program SAPLV69A -> LV69AO03à Module FELDAUSWAHL, there is a User Exit: PERFORM userexit_field_modification.

Inside this EXIT, change the contents of TCTRL_KONDITIONEN-COLS [6]-SCREEN-INPUT to 0. KBETR will get grayed out.

Capture error messages for function modules without exceptions


At times we see function modules without any exceptions but still there is an error message being thrown inside the function module if any piece of code fails. If this situation happens then the program terminates at that point. In this case there is no SY-SUBRC other than 0.

To solve this, in the program where you have written your code for the FM, include the following lines of code.

EXCEPTIONS
    error_message = 99.

By doing this you will ensure that whenever there is any error thrown by the function module, SY-SUBRC is set as 99 and you can then capture the message in system fields i.e. SY-MSGID, SY-MSGTY, SY-MSGNO, SY-MSGV1, SY-MSGV2, SY-MSGV3, SY-MSGV4, and SY-MSGV5.
SAMPLE CODE:
CALL FUNCTION 'RV_CUSTOMER_MATERIAL_UPDATE'
  
TABLES
    xknmt_tab     = lt_xknmt
    yknmt_tab     = lt_ykmt
    tcatalog_tab  = lt_cat
  
EXCEPTIONS
    error_message = 
99.
  IF sy-subrc <> 0.
     
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  
ENDIF.

Wednesday, May 30, 2012

Embedding Image in mail body instead of sending it as an attachment

Get the path of image residing in MIME repository, fetch the binary content of the image, convert into BASE64 data and attach the base64 data into HTML mail body to embed image.
·         Create a z report program.
·         Get the image path from MIME repository (SE80): '/SAP/PUBLIC/image.jpg' (Path of image in mime repository which need to be embedded)
·         Get the binary content of the image.
  DATA: o_mr_api  type ref to if_mr_api,
                 is_folder  type boole_d,
                 l_current type xstring,
                 l_loio type skwf_io.

 IF o_mr_api is initial.
    o_mr_api = cl_mime_repository_api=>if_mr_api~get_api ( ).
  ENDIF.
      CALL METHOD o_mr_api->get
    EXPORTING
      i_url              = '/SAP/PUBLIC/image.jpg'
    IMPORTING
      e_is_folder        = is_folder
      e_content          = l_current
      e_loio             = l_loio
    EXCEPTIONS
      parameter_missing = 1
      error_occured      = 2
      not_found          = 3
      permission_failure = 4
      OTHERS             = 5.

*l_current will hold the image in a XSTRING 
·         Convert the binary image data into Base64.
  CALL FUNCTION 'SSFC_BASE64_ENCODE'
    EXPORTING
      bindata                        = l_current
   IMPORTING
     b64data                        = b64data.
  IF sy-subrc <> 0.  
  ENDIF.
·         Create mail body using HTML.
     In email body, the image will be displayed using its binary content.
    DATA lv_length TYPE I,
                 lv_len2 TYPE i.

  clear wa_mail_body.
  move '<html>' to wa_mail_body.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  move '<head>' to wa_mail_body.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  move '<title>hello</title>' to wa_mail_body.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  move '<meta http-equiv="content-type" content="text/html;charset=iso-8859-   1">' to wa_mail_body.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  move '</head>' to wa_mail_body.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  move '<body>' to wa_mail_body.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  wa_mail_body  = '<em><font'  .
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  wa_mail_body  = 'color="#0000ff" size="+7" face="arial,'.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  wa_mail_body  = 'helvetica, sans-serif">test image</font></em>'.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
*add image base64 content
  wa_mail_body = '<img src="data:image/gif;base64,'.

  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.

  lv_length = strlen (b64data).
  lv_len2 = lv_length / 255.

  wa_mail_body = b64data.
  append wa_mail_body to gt_mail_body.
  CLEAR wa_mail_body.


  DATA :lv_len3 TYPE I,
              temp1 TYPE i,
              temp2 TYPE i.
  do lv_len2 times.
    lv_len3 = 255 * sy-index.

    if lv_len3 <= lv_length.
      wa_mail_body = b64data+lv_len3.
      if wa_mail_body is not initial.
        append wa_mail_body to gt_mail_body.
        clear wa_mail_body.
      else.
        exit.
      endif.
    elseif lv_len3 > lv_length.

        exit.
    endif.
  enddo.

  wa_mail_body = '"alt="happy birthday" align="middle" width="304" height="228" />'.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  move '</body>' to wa_mail_body.
  append wa_mail_body to gt_mail_body.

  clear wa_mail_body.
  move '</html>' to wa_mail_body.
  append wa_mail_body to gt_mail_body.
·         Send Mail.
           DATA : lt_mail_body TYPE soli_tab,
         lwa_mail_body LIKE LINE OF lt_mail_body.

  l_subject = 'Test : Image in mail'.

  lr_email_body = cl_document_bcs=>create_document(
                                   i_type = 'HTM'
                                   i_text = gt_mail_body
                                   i_subject = l_subject ).

  lr_email = cl_bcs=>create_persistent( ).
  lr_email->set_document( lr_email_body ).

  l_mail_address = 'test@test.com'.

  lr_receiver = cl_cam_address_bcs=>create_internet_address( l_mail_address ).
  lr_email->add_recipient( i_recipient = lr_receiver ).

  "Set Sender and send mail
  l_sender = cl_sapuser_bcs=>create( sy-uname ).
  lr_email->set_sender( l_sender ).
  lr_email->set_send_immediately( 'X' ).  "Send email directly
  l_send_result = lr_email->send( i_with_error_screen = 'X' ).

  COMMIT WORK.
Output:
Go to SAP inbox check mail for the image embedded in the mail.