Skip to Content
Author's profile photo Sumeet Gehlot

Implementation of a Custom Genil Model to Create Component in Web UI

In my previous blog   http://scn.sap.com/community/crm/webclient-ui-framework/blog/2012/08/06/creating-a-genilbol-object-model

I created a custom GenIL object model to create and save bol objects. In this blog I am going to use that custom GenIL component to create and save the data in database.

1.> Open a Transaction BSP_WD_CMPWB .

2.> Enter Component name as ZCUST_CMP and click on create

3.> Right Click on Model -> Add Model

4.>  Add Component Set name as  ZCSET_CUST which you had created while creating Genil Model.

5.>  You can see the Component Structure Browser , expanding Model will show your

      Root Object Customer , Key Structure , Attribute Structure. 

6.> Create a Custom Controller named as ZCuCo so that later on you can navigate or pass the data between views.

 

7.>  Add Customer as a Model Node and a BOL entity -> Click on Next till Complete.

8.>  Double click on custom controller , you can see list of attributes of a context node you added.

9.>  Right Click on View and Create Over View Page name as OverViewPage .

10.>  Create a New View using Create -> Define Name as CreateCustomer.

11.> Add Model Node and BOL entity as Customer and Bind it with Custom Controller.

12.> Select a View Type form View as required and Click on Complete.

13.> Click on Configuration Tab and Add the Available fields.

14.>  Here you can change the field Properties using Hold down ALT+Field.

15.>  Next Step is assignment of View to OverViewPage.

16.>  then Assign OverViewPage to Windows

17.>   Next Step is about to add View to Display assignment blocks of OverViewPage.

18.> Click on Test Button.

19.> Here the fields are not bounded , to achieve this  a custom bol objects and created a custom genil class,

then first you have to laod that bol object and a component set , you can use this code in Do_init_context method

so that initially when you run crm_ui , it will initailizes the  component set and bol objects.

Instead of Initialization in Do_init_context , Lets create a button Create (to Load bol Objects) and

and Save to update in database (ZMast_Cust).

20.> Declare a global attribute GT_BUTTONS in a implementation class.

21.> Click on Page Attributes and add the following code for BSP Page.

     <%@page language=”abap” %>

     <%@extension name=”chtmlb” prefix=”chtmlb” %>

     <%@extension name=”thtmlb” prefix=”thtmlb” %>
     <%@extension name=”uhtmlb” prefix=”uhtmlb” %>
     <%
      DATA lv_xml TYPE string.
      lv_xml = controller->configuration_descr->get_config_data( ) %>
      <chtmlb:config xml  = “<%= lv_xml %>”
      mode = “RUNTIME” />
      <uhtmlb:toolbar id              = “Toolbar”
       buttons         = “<%= controller->gt_buttons %>”
       maxButtonNumber = “3” />

22.> Redefine Do_init_context method and add the code to trigger events on button.

DATA : lr_entity TYPE REF TO cl_crm_bol_entity,
          lv_collection TYPE REF TO if_bol_bo_col.

* Define Buttons

   DATA ls_button TYPE crmt_thtmlb_button.
   REFRESH gt_buttons.

   ls_buttonid       = ‘Create’.                          
   ls_buttontext     = ‘Create’.
   ls_buttonon_click = ‘Create’.                         
   ls_buttonenabled  = abap_true.
   ls_buttontype     = cl_thtmlb_util=>gc_icon_create.
   APPEND ls_button TO gt_buttons.

   ls_buttonid       = ‘Save’.                           
   ls_buttontext     = ‘Save’.
   ls_buttonon_click = ‘Save’.                        

   ls_buttonenabled  = abap_true.
   ls_buttontype     = cl_thtmlb_util=>gc_icon_save.
   APPEND ls_button TO gt_buttons.

ENDMETHOD.

23.> Here the fields are not bounded ,to achieve this  a custom bol objects and created a

custom genil class, then first you have to load that bol object and a component set , you

can also use this code in Do_init_context so that initially when you run crm_ui , it will initializes

the component set and bol objects.

METHOD eh_oncreate.

   DATA : lr_entity       TYPE REF TO cl_crm_bol_entity                                             

             lv_collection  TYPE REF TO if_bol_bo_col.

                                     
   DATA : lr_core        TYPE REF TO cl_crm_bol_core,
             lr_fac          TYPE REF TO cl_crm_bol_entity_factory,
             lt_params     TYPE        crmt_name_value_pair_tab,
             ls_params     TYPE        crmt_name_value_pair,
             lr_ent          TYPE REF TO cl_crm_bol_entity.

   TRY.


* Try to Get the instance of Root Object Customer


       lr_core = cl_crm_bol_core=>get_instance( ).
       lr_core->start_up( ‘ZCUST’ ).
     CATCH cx_crm_genil_general_error.
       EXIT.
   ENDTRY.

* Get Entity factory of Customer
        lr_fac = lr_core->get_entity_factory( ‘Customer’ ).
        lt_params = lr_fac->get_parameter_table( ).    
           TRY.

* Create table structure
           lr_ent = lr_fac->create( lt_params ).
           IF lr_ent IS BOUND.


* Add Parameters
             me->typed_context->customer->collection_wrapper->add( iv_entity = lr_ent ).
             CHECK lr_ent->lock( ) = abap_true.
           ENDIF.
   CATCH cx_crm_genil_model_error.
   EXIT.
   CATCH cx_sy_ref_is_initial.
   ENDTRY.
 
ENDMETHOD.

24.> Method to save data into database using Save button.

METHOD eh_onsave.

   DATA : lr_customer       TYPE REF TO cl_crm_bol_entity,
             lr_core              TYPE REF TO cl_crm_bol_core,
             lr_trans             TYPE REF TO if_bol_transaction_context,
             lv_success           TYPE abap_bool,
             lv_commit            TYPE char1,
             ls_customer           TYPE zattr_cust,
             lr_msg_service       TYPE REF TO cl_bsp_wd_message_service.

   DATA : lv_custno TYPE zcustno.

* Get the root object Instance
          lr_core = cl_crm_bol_core=>get_instance( ).
       CHECK lr_core IS BOUND.
       lr_core->modify( ).

*  Get the data of  the current instance.


   lr_customer  ?= me->typed_context->customer->collection_wrapper->get_current( ).

*  Get the Customer Number using get property as value

   CALL METHOD lr_customer->if_bol_bo_property_access~get_property_as_value
     EXPORTING
       iv_attr_name = ‘CUSTNO’
     IMPORTING
       ev_result    = lv_custno.

   ls_customercustno = lv_custno.

   CHECK lr_customer IS BOUND.


   CALL METHOD lr_customer->if_bol_bo_property_access~get_properties
     IMPORTING
       es_attributes = ls_customer.

* Get Transaction of the current entity.


   lr_trans ?= lr_customer->get_transaction( ).
   CHECK lr_trans IS BOUND.
   CHECK lr_trans->check_save_possible( ) EQ abap_true.


   lv_success = lr_trans->save( ).

   IF lv_success = ‘X’.
     lr_trans->commit( ).
   ELSE.
     lr_trans->rollback( ).
   ENDIF.
ENDMETHOD.

25.> Open UI and Test the component -> Click on Create then Enter the details

       -> Click on Save.

26.> Check The Entry in a Database Table.

Regards,

Sumeet Gehlot

SAP CRM Practice.


Assigned Tags

      20 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      Nice blog and resoureful, can i hav what for  the usage of Create button?

      Thanks,

      Savaridassan P

      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      It's Very Good Blog.........

      Keep it postings plz

      Thanks

      sana

      Author's profile photo Former Member
      Former Member

      Nice blog !!! This blog helped me to understand the concepts of genil bol and ui

      Thanks cheers 🙂 🙂

      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      Nice blog. Now can you please let me know how can i show this Z-component to Web UI screen? so that it can work standalone.

      Regards

      Saurabh

      Author's profile photo Former Member
      Former Member

      Hi Sumeet...

      Superb Document 🙂

      Please attach screenshots for the steps 12 to 17.

      Regards,

      Shahir Mirza

      Author's profile photo Former Member
      Former Member

      Nice Blog

      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      Superb document ... really helpful for all the beginners as well as for all those who have conceptual doubts... keep it up.

      Regards,

      Vishal Jauhari

      Author's profile photo Arjun Pawar
      Arjun Pawar

      Very good and helful blog...thanks for sharing 🙂

      Author's profile photo Former Member
      Former Member

      Very Nice Blog.

      Although I required to do couple of changes(below mentioned) to make it work.

      @Step 20 -> Did not required to declare a GT_BUTTONS in the implementation class.

      @Step 21 ->  Code Snippet below to Include CREATE & SAVE buttons.

      Click on CreateCustomer.htm page and add the following code for BSP Page.

      <%@page language="abap" %>

      <%@extension name="thtmlb" prefix="thtmlb" %>

      <%@extension name="chtmlb" prefix="chtmlb" %>

      <%@extension name="bsp" prefix="bsp" %>

      <chtmlb:config mode = "RUNTIME"

                      xml  = "<%= controller->configuration_descr->get_config_data( ) %>" />

                       <thtmlb:button design  = "EMPHASIZED"

                                id      = "CREATE"

                                onClick = "CREATE"

                                tooltip = "<%= page->otr_trim( 'CRM_UIU_BP/UPDATEBUTTON' ) %>"

                                text    = "CREATE" />

                        <thtmlb:button design  = "EMPHASIZED"

                                id      = "SAVE"

                                onClick = "SAVE"

                                tooltip = "<%= page->otr_trim( 'CRM_UIU_BP/UPDATEBUTTON' ) %>"

                                text    = "SAVE" />

      @Step 22 -> Redefine Do_init_context method (to bound the fields)

      DATA :    lr_entity TYPE REF TO cl_crm_bol_entity,

                   lv_collection TYPE REF TO if_bol_bo_col.

         DATA :    lr_core        TYPE REF TO cl_crm_bol_core,

                   lr_fac          TYPE REF TO cl_crm_bol_entity_factory,

                   lt_params     TYPE        crmt_name_value_pair_tab,

                   ls_params     TYPE        crmt_name_value_pair,

                   lr_ent          TYPE REF TO cl_crm_bol_entity.

         TRY.

      * Try to Get the instance of Root Object Customer

             lr_core = cl_crm_bol_core=>get_instance( ).

             lr_core->start_up( 'VCUST' ).

           CATCH cx_crm_genil_general_error.

             EXIT.

         ENDTRY.

      * Get Entity factory of Customer

         lr_fac = lr_core->get_entity_factory( 'Customer' ).

         lt_params = lr_fac->get_parameter_table( ).

         TRY.

      * Create table structure

             lr_ent = lr_fac->create( lt_params ).

             IF lr_ent IS BOUND.

      * Add Parameters

               me->typed_context->customer->collection_wrapper->add( iv_entity = lr_ent ).

      *        CHECK lr_ent->lock( ) = abap_true.

             ENDIF.

           CATCH cx_crm_genil_model_error.

             EXIT.

           CATCH cx_sy_ref_is_initial.

         ENDTRY.


      @ Step 23 -> METHOD  eh_oncreate


      METHOD eh_oncreate.

         DATA : lr_collection TYPE REF TO if_bol_bo_col,

                lr_core TYPE REF TO cl_crm_bol_core,

                lr_fac TYPE REF TO cl_crm_bol_entity_factory,

                lt_params TYPE crmt_name_value_pair_tab.

         lr_core = cl_crm_bol_core=>get_instance( ).

         lr_fac = lr_core->get_entity_factory( 'Customer' ).

         lt_params = lr_fac->get_parameter_table( ).

         CHECK lr_core IS BOUND.

         TRY.

             CALL METHOD lr_core->root_create

               EXPORTING

                 iv_object_name  = 'Customer'

                 iv_create_param = lt_params

                 iv_number       = 1

               RECEIVING

                 rv_result       = lr_collection.

      * Set entactions col to the context node!!

             me->typed_context->customer->set_collection( lr_collection ).

           CATCH cx_crm_unsupported_object .

         ENDTRY.

      ENDMETHOD.

      Author's profile photo Sumeet Gehlot
      Sumeet Gehlot
      Blog Post Author

      Hi Vinay,

      Thanks for your ratings....

      The code i supposed to write in Do_init_context so that when you run crm_ui , it will automatically initializes  the component set and bol objects , but i thought of adding a code in on click button.

      and yes we can write the code in .htm page , and may i have to correct writing the code for buttons in do_prepare output method.

      Thanks.

      Sumeet

      Author's profile photo zafar karnalkar
      zafar karnalkar

      Hi,

      I have done all the steps as mention in above document, also added the code inDO_INIT_CONTEXT and other EH_ONCREATE  and EH_ONSAVE,  but when I ma testing it system is giving the UI screen with message as below.

      CUSTNAMEnot bound

      TELEPHONEnot bound

      CITYnot bound

      etc.

      Regards,

      Zafar

      Author's profile photo Sumeet Gehlot
      Sumeet Gehlot
      Blog Post Author

      Hi,

      Try to use and cjheck in debugging that instance are getting created.

      Get BOL core instance

               lr_core = cl_crm_bol_core=>get_instance( ).

               lr_core->load_component_set( 'ZCUST' ).



      Regards,

      Sumeet

      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      Nice post. But i am facing a few problem after implementing the code:

      1) I have added the code what u mentioned in eh_oncreate in Do_init_context method as well as in event eh_oncreate.

      when i run the application first time, everything works fine, i am able to enter the values in the field and clicking on save button, saves the data in the table zmast_cust.

      but the new data that is saved is now displayed on form view in greyed out mode. clicking on create button, doesnt initialize the fields..the fields still remains greyed out. please help.

      regards,

      b. chowdhury

      Author's profile photo Hansen Li
      Hansen Li

      Hello Vinay,

      According to you method, there be an error as shown in picture. Do you know how to resolve it.

      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      I have followed all the steps to created custom component using custom genil , but the fields on the View are not editable .

      Can you help ?

      Thanks

      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      Good contribution.

      Thank you for your nice blog.

      Regards,

      Hiranya

      Author's profile photo Sumeet Gehlot
      Sumeet Gehlot
      Blog Post Author

      Hi Hiranya,

      Event handlers are required to be able to react to user actions on the Web UI.

      For example, if a user clicks a button on the web UI, this triggers an event that can be processed accordingly by an event handler method.

      Event handlers are implemented as methods of the view controller.

      Within the view controller , DO_HANDLE_EVENT method in the view controller   distributes the  events to the generated event handler methods of the view.

      To create an event handler, you must select the Event handler level in the view controller of the component workbench and start the create option for the context menu.

      When the event handler is being created, it receives the name of the event that is entered in the wizard, including the EH_ON prefix.

      The event name is case sensitive.

      Regards,

      Sumeet

      Author's profile photo Former Member
      Former Member

      Hi Sumeet,

      I have completed "Implementation of a Custom Genil Model to Create Component in Web UI".

      Thank you for your suggestion. I have created new event (as your suggestion) and follow step 6 until 26 again.

      Then i can click button "Create" and "Save" and save input data to database table already.

      It's very great for me.

      Thank you very much indeed.

      Regards,

      Hiranya

      Author's profile photo Former Member
      Former Member

      Hi Sumeet Gehlot,

      can u plz help this

       

      lr_fac = lr_core->get_entity_factory( 'Name of the root object you created' ).

      my root object is 'BTPartner'

      lt_params = lr_fac->get_parameter_table( ).

      TRY.

      lr_ent = lr_fac->create( lt_params ).

      lr_ent is intial

      can u please suggest

      Author's profile photo Sumeet Gehlot
      Sumeet Gehlot
      Blog Post Author

      Hi Padmavati,

       

      BTPartner is a dependent object of BTAdminH so you cannot create factory object for the same.

      You can refer GENIL_MODEL_BROWSER tcode for Root and dependent objects

      Can you tell me your requirement what exactly you need.

       

      Thanks for reaching to me.

      Regards,

      Sumeet