Document Name:

CRM Web UI – Smart-Form call from a Z-Component

Objective:

To call a Smart-Form from Web UI layer of a Z-Component on click of a button.

Description:

This document will guide through the steps for calling a smart-form from a Z-Component from WEB UI layer on click of a button, where the input of the smart-form is entered by user at runtime.

Detailed Steps: Creating a component that calls a Smart-Form

Step 1: Create a simple component with an input field and button.

  • Go to the TCode : BSP_WD_CMPWB and create a component (say ZCOMP_PRINT) by clicking the create button./wp-content/uploads/2014/09/1_550929.png
  • Input values for Description and Window Name. /wp-content/uploads/2014/09/2_550941.png
  • Select the package and save all the requests that follows( 3-4 Create Object entry(Selection of Package) and transport request selection will be there ) Save it all under same TR .
  • Create a View named ‘Print’ inside the component Under the Component structure browser./wp-content/uploads/2014/09/4_550946.png
  • Click Continue.

/wp-content/uploads/2014/09/5_550948.png

  • Give the view a name (lets say Print) and press continue till the Add value node tab comes, skipping ‘Add Model Node’ tab.

/wp-content/uploads/2014/09/6_550949.png

  • Add the value node name in the ‘Add value node’ tab (say Print) and press continue till   ‘Add Value Attributes’ tab.

/wp-content/uploads/2014/09/7_550950.png

  • Enter the value attributes as shown below.

/wp-content/uploads/2014/09/47_550952.png

  • Press continue till Select View type and select Form View with configurable check box checked as shown below.

/wp-content/uploads/2014/09/8_550951.png

  • Press continue and then complete button which looks like the one given below.

/wp-content/uploads/2014/09/9_550953.png

  • Save all the requests that follows in to the previous package and transport requests where the component was stored initially.
  • Double click on the print view on Component Structure Browser as shown to get the View Structure of the Print view which we created in above steps./wp-content/uploads/2014/09/10_550954.png
  • Write the code for creating a button on the editor as shown below(You can insert code going to edit mode by clicking the Edit pencil button) and activate it by .

<thtmlb:button
id       = “PRINT”
onClick  = “PRINT”
design  =
“EMPHASIZED”
text     = “Rrint”
tooltip  =
“Rrint”
enabled =
“TRUE”
/>

/wp-content/uploads/2014/09/12_550959.png

  • Create an event named ‘PRINT’(Case sensitive with the ‘PRINT’ given for onClick in the above code) .

/wp-content/uploads/2014/09/13_550960.png

/wp-content/uploads/2014/09/14_550964.png

/wp-content/uploads/2014/09/15_550965.png

  • Double click on the Print event and write the following code and activate it.

METHOD eh_onprint.
 
DATAlr_entity TYPE REF TO if_bol_bo_property_access,
         lr_col   
TYPE REF TO if_bol_bo_col,
         lv_lot
TYPE string,
         lv_query
TYPE string,
         lv_url
TYPE string,
         lv_title
TYPE string,
         lr_obj     
TYPE REF TO if_bol_bo_property_access,
         lr_cn      
TYPE REF TO cl_bsp_wd_context_node,
         ls_params  
TYPE crmt_gsurlpopup_params,
         lr_popup   
TYPE REF TO if_bsp_wd_popup.

  lr_entity ?= me->typed_context->print->collection_wrapper->get_current( ).
 
IF lr_entity IS BOUND.
    lv_lot
= lr_entity->get_property_as_string( ‘PRINT’ ).
   
IF lv_lot IS NOT INITIAL.
     
CONCATENATE ‘scenario=P&guid=’ lv_lot INTO lv_query.
      lv_url
= cl_crm_web_utility=>create_url(
                  iv_path           
= ‘/sap/crm/zcrm_pdf’
                  iv_query          
= lv_query
                  iv_in_same_session
= ‘X’ ).
     
IF lv_url IS NOT INITIAL.
        lv_title
= cl_wd_utilities=>get_otr_text_by_alias( ‘CRM_UIU_BT_GEN/PRINT_PREVIEW_POPUP_TITLE’ ). “#EC NOTEXT
        lr_popup
me->comp_controller->window_manager->create_popup(
                 iv_interface_view_name
= ‘GSURLPOPUP/MainWindow’
                 iv_usage_name         
= ‘CUGSURLPopup’
                 iv_title              
= lv_title ).
        lr_cn
= lr_popup->get_context_node( ‘PARAMS’ ).
        lr_obj
= lr_cn->collection_wrapper->get_current( ).
        ls_params
url = lv_url.
        ls_params
height = ‘700’.                           “#EC NOTEXT
        lr_obj
->set_properties( ls_params ).
        lr_popup
->set_display_mode( if_bsp_wd_popup=>c_display_mode_plain ).
        lr_popup
->set_window_width( 700 ).
        lr_popup
->set_window_height( 700 ).
        lr_popup
->open( ).
     
ENDIF.
   
ENDIF.
 
ENDIF.

ENDMETHOD.

/wp-content/uploads/2014/09/16_550966.png

  • Go to the configuration tab of the Print View and click on ‘Edit’ button to get the editable window and click the button ‘Show Available Fields’, which will give you the field  ‘Number’ of type INT4 which was added in context node PRINT at time of view creation.

/wp-content/uploads/2014/09/17_550968.png

  • Select the button and add to the screen as shown and SAVE the changes by clicking save button.

/wp-content/uploads/2014/09/18_550969.png

/wp-content/uploads/2014/09/19_550973.png

  • Go to Runtime Repository editor on the side bar of screen and add the view to the main window and make it default.
  • Select View ‘Print’ from the F4 Pop Up.

/wp-content/uploads/2014/09/48_550977.png

  • Flag the View as default.

/wp-content/uploads/2014/09/49_550945.png

  • Add the component GSURLPOPUP to component Usage for the pop up window under ID: CUGSURLPopup, Component : GSURLPOPUP and save the Runtime Repository Editor from the save button below the tab .

/wp-content/uploads/2014/09/22_550978.png

/wp-content/uploads/2014/09/23_550976.png

/wp-content/uploads/2014/09/24_550979.png

Step 2: Create a simple SMART-FORM to display the input data entered . 

  • Goto the TCode SMARTFORMS and create a sample smart-form (Say ZPRINT).

/wp-content/uploads/2014/09/25_550981.png

/wp-content/uploads/2014/09/26_550982.png

  • Save the SMART_FORM to the package and the transport request that follows.
  • Go to Form Interface and add the import parameter PRINT of type INT4 as shown.

/wp-content/uploads/2014/09/27_550983.png

  • Create a Text box in the main window.

/wp-content/uploads/2014/09/28_550984.png

  • Insert the import parameter Print in to the text field added and activate the smart form.

/wp-content/uploads/2014/09/29_550985.png

/wp-content/uploads/2014/09/30_550994.png

  • Save, Activate and test the smart-form, you should be getting similar kind of output.

/wp-content/uploads/2014/09/32_550989.png

Step 3: Create SICF service to call the smart form, convert it to *.PDF, create url and return it to Web UI layer.

  • Go to the TCode SICF and enter the service path as /sap/crm/ , Service name: CRM_PDF_PRINT and execute.

/wp-content/uploads/2014/09/33_550995.png

  • Double click the service CRM_PDF_PRINT and see the handler class.

/wp-content/uploads/2014/09/34_550996.png

/wp-content/uploads/2014/09/35_550997.png

  • Come back and copy the service and paste it in different name (say zcrm_pdf).

/wp-content/uploads/2014/09/36_550998.png

  • Now the service is copied in clipboard, rename the current service ‘CRM_PDF_PRINT’ to a custom service (say ZCRM_PDF) and then paste the original service stored in clipboard under the same tree at same place(as shown in step 5 below).

Note : Be careful on this steps else you may resulting in deletion of standard service.  If in  doubt you may create a service from Create Host/Service from the button above at the same place with the configuration in the crm_pdf_print service.

/wp-content/uploads/2014/09/39_550999.png

/wp-content/uploads/2014/09/38_551003.png

  • Now paste the service crm_pdf_print stored in clipboard at the same place so that both the services(zcrm_pdf and crm_pdf_print) are available at same place.Activate both services.

/wp-content/uploads/2014/09/39_550999.png

  • Clear the service name field from the Filter Details block and click apply to see all the services. The service zcrm_pdf will be available in the path default_host > sap > crm.

/wp-content/uploads/2014/09/40_551005.png

  • Go to TCode SE24 and make a copy of the handler class (CL_CRM_PREVIEW_PDF), which in the handler class for the standard service crm_pdf_print.

/wp-content/uploads/2014/09/41_551007.png

/wp-content/uploads/2014/09/42_551008.png

  • Go to the method IF_HTTP_EXTENSION~HANDLE_REQUEST of the class and replace the current code with the below code.

/wp-content/uploads/2014/09/43_551009.png

Code :

method if_http_extension~handle_request.
  
data: ls_phio                type skwf_io,
         lt_file_info          
type sdokfilacis,
         ls_file_info          
like line of lt_file_info,
         lv_file_name          
type sdok_filnm,
         lv_mime_type          
type w3conttype,
         lv_file_size          
type sdok_fsize,
         lt_contents           
type sdokcntbins,
         ls_contents           
type sdokcntbin,
         ls_error              
type skwf_error,
         lv_error_msg          
type string,
         lv_chunksize          
type sybin2,
         xwa                   
type xstring,
         xwa_len               
type i,
         lv_contenttype        
type string,
         lv_filename           
type string,
         lv_contentdisposition 
type string,
         lv_appl_ns            
type string,
         lt_header_fields      
type tihttpnvp,
         lt_form_fields        
type tihttpnvp,
         lv_qry_string         
type string,
         ls_scenario           
type string,
         lv_devtype            
type rspoptype,
         lv_print_dest         
type rspopname,
        
begin of ls_attach_key,
           object_id
type soodk,
           attach_id
type soodk,
        
end of ls_attach_key,
         ls_attach_header
type sood2,
         lt_objcont      
type standard table of soli,
         ls_objcont      
like line of lt_objcont,
         ls_size         
type soodobjlen,
         ls_lines        
type i,
         ls_len          
type i,
         lv_fn_length    
type i,
         lv_fe_length    
type i,
         ls_guid         
type crmt_object_guid,
         ls_guid_str     
type string,
         ls_smart_form   
type tdsfname,
         ls_class        
type seoclsname,
         ls_method       
type seocmpname,
         ls_action_guid  
type string,
         lr_action_ppf   
type ref to if_action_ppf.
  
data  xwa_str          type string.
data : ls_header_guid type string,
      lv_pdf_xstring
type xstring,
      lv_pdf_length
type i,
      l_bin
type standard table of sdokcntbin.

ls_header_guid = server->request->get_form_field( ‘guid’ ).

call method me->get_pdf
       
exporting
          iv_guid
= ls_header_guid
       
importing
          ev_contents 
= lv_pdf_xstring.

    call function ‘SCMS_XSTRING_TO_BINARY’
       
exporting
         
buffer        = lv_pdf_xstring
       
importing
          output_length
= lv_pdf_length
       
tables
          binary_tab   
= l_bin.

lv_file_size = lv_pdf_length.
        lv_contenttype
= ‘application/pdf’.
       ls_guid_str
= ls_header_guid.
       
concatenate ls_guid_str ‘.pdf’ into lv_filename.
        lv_file_name
= lv_filename.

   server->response->append_data(
                           
data   = lv_pdf_xstring
                            length
= lv_pdf_length ).concatenate ‘inline; filename=’ lv_filename
   
into lv_contentdisposition.

  call method server->response->set_header_field
   
exporting
      name 
= ‘content-disposition’
     
value = lv_contentdisposition.

  call method server->response->set_header_field
   
exporting
      name 
= ‘content-type’
     
value = lv_contenttype.

  call method server->response->set_header_field
   
exporting
      name 
= ‘content-filename’
     
value = lv_filename.

  server->response->delete_header_field(
           name
= ‘Cache-Control’ ).                        “#EC NOTEXT

  server->response->delete_header_field(
           name
= ‘Expires’ ).

endmethod.                 “IF_HTTP_EXTENSION~HANDLE_REQUEST

  • Now in the method GET_PDF of the same class(ZCL_CRM_PREVIEW_PDF) paste the below code.

Code:

method get_pdf.

  data lt_otfdata        type tsfotf.
 
data lt_pdf_lines      type standard table of tline.
 
data: it_otf      type standard table of itcoo,
it_docs    
type standard table of docs,
it_lines   
type standard table of tline.
 
data:
st_job_output_info      
type ssfcrescl,
st_document_output_info 
type ssfcrespd,
st_job_output_options   
type ssfcresop,
st_output_options       
type ssfcompop,
st_control_parameters   
type ssfctrlop,
v_len_in                
type so_obj_len,
v_language              
type sflangu value ‘E’,
v_e_devtype             
type rspoptype,
v_bin_filesize          
type i,
v_name                  
type string,
v_path                  
type string,
v_fullpath              
type string,
v_filter                
type string,
v_uact                  
type i,
v_guiobj                
type ref to cl_gui_frontend_services,
v_filename              
type string,
v_fm_name               
type rs38l_fnam,
c_formname              
type tdsfname  ,
lv_st_job_output_info   
type ssfcresclotfdata,
lv_pdf                  
type               xstring ,*  pdf_tab                  LIKE tline OCCURS 0 WITH HEADER LINE,
ls_print                
type int4.

  c_formname = ‘ZPRINT’ .
 
call function ‘SSF_GET_DEVICE_TYPE’
   
exporting
      i_language   
= v_language
      i_application
= ‘SAPDEFAULT’
   
importing
      e_devtype    
= v_e_devtype.
  st_output_options
tdprinter = v_e_devtype.
  st_control_parameters
no_dialog = ‘X’.
  st_control_parameters
getotf = ‘X’.*……………..GET SMARTFORM FUNCTION MODULE NAME……………..*

  • Rename the handler class in SICF service we created (zcrm_pdf) from CL_CRM_PREVIEW_PDF to ZCL_CRM_PREVIEW_PDF (Path : zcrm_pdf>Handler List tab ).
  • The development is complete now you may go to the component and test the same. Go to the T-CODE : BSP_WD_CMPWB type the component name in the space provided and execute it.

/wp-content/uploads/2014/09/44_551010.png

  • You will be getting the browser opened with the component as shown below. Type some input number in the space provided and press the ‘Print’ button to display the SMARTFORM in a popup.

/wp-content/uploads/2014/09/45_551011.png

OUTPUT

/wp-content/uploads/2014/09/46_551012.png

To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. Faisal PC

    Hi Amal,

    Nice document.

    However, I believe u missed one point. The handler class of SICF service should be updated with the Z class. Right?

    Thanks,

    Faisal

    (0) 
  2. praveen ravula

    Hi I am trying the similar requirement. We need to show a popup on business partner menu button click as selection screen. And when user provide the selection screen details and click continue need to call the smartform output form in the PDF. I followed your steps for some reason the smartform PDF is not showing up in the popup. I tried the same in google chrome, it is showing but with very small page size. Can you please help me with some suggestions?pravula@@stepan.com

    (1) 
  3. Amal Aravind Post author

    Hope this helps.

    METHOD IF_HTTP_EXTENSION~HANDLE_REQUEST.
      DATA: ls_phio                TYPE skwf_io,
            lt_file_info           TYPE sdokfilacis,
            ls_file_info           LIKE LINE OF lt_file_info,
            lv_file_name           TYPE sdok_filnm,
            lv_mime_type           TYPE w3conttype,
            lv_file_size           TYPE sdok_fsize,
            lt_contents            TYPE sdokcntbins,
            ls_contents            TYPE sdokcntbin,
            ls_error               TYPE skwf_error,
            lv_error_msg           TYPE string,
            lv_chunksize           TYPE sybin2,
            xwa                    TYPE xstring,
            xwa_len                TYPE I,
            lv_contenttype         TYPE string,
            lv_filename            TYPE string,
            lv_contentdisposition  TYPE string,
            lv_appl_ns             TYPE string,
            lt_header_fields       TYPE tihttpnvp,
            lt_form_fields         TYPE tihttpnvp,
            lv_qry_string          TYPE string,
            ls_scenario            TYPE string,
            lv_devtype             TYPE rspoptype,
            lv_print_dest          TYPE rspopname,
      BEGIN OF ls_attach_key,
        object_id TYPE soodk,
        attach_id TYPE soodk,
      END OF ls_attach_key,
      ls_attach_header TYPE sood2,
      lt_objcont       TYPE STANDARD TABLE OF soli,
      ls_objcont       LIKE LINE OF lt_objcont,
      ls_size          TYPE sood-objlen,
      ls_lines         TYPE I,
      ls_len           TYPE I,
      lv_fn_length     TYPE I,
      lv_fe_length     TYPE I,
      ls_guid          TYPE crmt_object_guid,
      ls_guid_str      TYPE string,
      ls_smart_form    TYPE tdsfname,
      ls_class         TYPE seoclsname,
      ls_method        TYPE seocmpname,
      ls_action_guid   TYPE string,
      lr_action_ppf    TYPE REF TO if_action_ppf.
      DATA  xwa_str          TYPE string.
      DATA : ls_header_guid TYPE string,
             lv_Case_type TYPE string,
            lv_pdf_xstring TYPE xstring,
            lv_pdf_length TYPE I,
            l_bin TYPE STANDARD TABLE OF sdokcntbin.
    
      ls_header_guid = server->request->get_form_field( 'lv_case_key' ).
      lv_case_type = server->request->get_form_field( 'lv_case_type' ).
    
      CALL METHOD me->get_pdf
      EXPORTING
        iv_guid = ls_header_guid
        iv_case_type = lv_Case_type
      IMPORTING
        ev_contents  = lv_pdf_xstring.
    
      CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        BUFFER        = lv_pdf_xstring
      IMPORTING
        output_length = lv_pdf_length
      TABLES
        binary_tab    = l_bin.
    
      lv_file_size = lv_pdf_length.
      lv_contenttype = 'application/pdf'.
      ls_guid_str = ls_header_guid.
      CONCATENATE ls_guid_str '.pdf' INTO lv_filename.
      lv_file_name = lv_filename.
    
      server->response->append_data(
      DATA   = lv_pdf_xstring
            length = lv_pdf_length ).CONCATENATE 'inline; filename=' lv_filename
      INTO lv_contentdisposition.
    
      CALL METHOD server->response->set_header_field
      EXPORTING
        name  = 'content-disposition'
        VALUE = lv_contentdisposition.
    
      CALL METHOD server->response->set_header_field
      EXPORTING
        name  = 'content-type'
        VALUE = lv_contenttype.
    
      CALL METHOD server->response->set_header_field
      EXPORTING
        name  = 'content-filename'
        VALUE = lv_filename.
    
      server->response->delete_header_field(
      name = 'Cache-Control' ).                        "#EC NOTEXT
    
      server->response->delete_header_field(
      name = 'Expires' ).
     ENDMETHOD.                    "IF_HTTP_EXTENSION~HANDLE_REQUEST

     

    method get_pdf.
    
    
      data: lv_guid      type os_guid,
            lv_guid_char type guid_32.
      data lv_log_handle     type balloghndl.
      data lt_msg_handle     type bal_t_msgh.
      data ls_msg_handle     type balmsghndl.
      data ls_msg_text       type crmt_action_msg.
      data lt_otfdata        type tsfotf.
      data lt_pdf_lines      type standard table of tline.
      data lt_prot_handle    type bal_t_logh.
      data lv_text           type string.
      data lr_msg_service    type ref to cl_bsp_wd_message_service.
    
      data lr_action_ppf     type ref to if_action_ppf.
      data lr_exc            type ref to cx_root.
    
      data:lv_case_key type char12,
            lv_Case_type type char30.
    
      lv_case_key = iv_guid.
      lv_Case_type = iv_case_type.
      data:fmname      type rs38l_fnam,
            iv_smartform_name type TDSFNAME,
    
    
           wa_ctrlop   type ssfctrlop,       " Smart Forms: Control structure
           wa_outopt   type ssfcompop,       " SAP Smart Forms: Smart Composer (transfer) options.
           lt_otfdata1 type ssfcrescl,       " Smart Forms: Return value at end of form printing
           lt_pdf_tab  type table of tline,  " SAPscript: Text Lines
           lt_otf      type table of itcoo.      " OTF Structure
    
    
    
    
      call function 'SSF_FUNCTION_MODULE_NAME'
        exporting
          formname           = <iv_smartform_name>
        importing
          fm_name            = fmname
        exceptions
          no_form            = 1
          no_function_module = 2
          others             = 3.
      if sy-subrc <> 0.
    * Implement suitable error handling here
      endif.
    data:v_e_devtype             type        rspoptype,
          v_language              type        sflangu value 'E'.
    
      call function 'SSF_GET_DEVICE_TYPE'
        exporting
          i_language    = v_language
          i_application = 'SAPDEFAULT'
        importing
          e_devtype     = v_e_devtype.
    
    
    
      wa_ctrlop-getotf = 'X'.
      wa_ctrlop-no_dialog = 'X'.
      wa_outopt-tdprinter = v_e_devtype.
    
    
      call function fmname
        exporting
          control_parameters = wa_ctrlop
          output_options     = wa_outopt
    
          lv_case_key        = lv_case_key
    
        importing
          job_output_info    = lt_otfdata1
        exceptions
          formatting_error   = 1
          internal_error     = 2
          send_error         = 3
          user_canceled      = 4
          others             = 5.
      if sy-subrc <> 0.
    
      endif.
    
      lt_otf[] = lt_otfdata1-otfdata[].
    
      call function 'CONVERT_OTF'
        exporting
          format                = 'PDF'
        importing
          bin_filesize          = ev_size
          bin_file              = ev_contents
        tables
          otf                   = lt_otf
          lines                 = lt_pdf_lines
        exceptions
          err_max_linewidth     = 1
          err_format            = 2
          err_conv_not_possible = 3.
    
    
    endmethod.
    (0) 

Leave a Reply