Trigger a workflow when PR is created using Class and events in ABAP.

-------------------------------------------------------Creating custom class-------------------------------------------------------------------

Step 1: Create a custom class.

Step 2: Give the interfaces.


Step 3: Create an event with parameter E_F_BANFN type BANFN.


Step 4: Create attributes.


Step 5: Create constructor with importing parameter I_F_BANFN.

Step 6: Implement the constructor.

                METHOD constructor.
            DATA l_f_instid TYPE sibflpor-instid.
            l_f_instid i_f_banfn.
            g_r_lpor-catid 'CL'.
            g_r_lpor-typeid 'ZCL_AB_WF_MM_PR_APPROVAL'.
            g_r_lpor-instid l_f_instid.
        ENDMETHOD.

Step 7: Implement the method - BI_PERSISTENT~FIND_BY_LPOR.

    METHOD BI_PERSISTENT~FIND_BY_LPOR.
    *    result = new zcl_ab_wf_pr_approval1( lpor ).
    *- begin of local data
        DATAl_o_ref_doc     TYPE REF TO ZCL_AB_WF_MM_PR_APPROVAL.
        DATAl_r_doc_key    TYPE banfn.
    *- end   of local data

    * instid is the key of the object
        l_r_doc_key :lpor-instid.

        TRY.
            CREATE OBJECT l_o_ref_doc
              EXPORTING
                i_f_banfn l_r_doc_key.

          CATCH cx_bo_error .
    *------ object not found
            RETURN.
        ENDTRY.
    * Load the current instance to result
        result l_o_ref_doc.
      ENDMETHOD.

Step 8: Implement the method BI_PERSISTENT~LPOR.

     method BI_PERSISTENT~LPOR.
          result g_r_lpor.
     endmethod.

Step 9: Implement the methods.

        BI_PERSISTENT~REFRESH

        BI_OBJECT~DEFAULT_ATTRIBUTE_VALUE

        BI_OBJECT~EXECUTE_DEFAULT_METHOD

        BI_OBJECT~RELEASE

Step 10: Save and activate.

Step 11: Create a method register_wf with importing parameter i_f_banfn.

    METHOD register_wf.

    DATA:l_f_objtype          TYPE sibftypeid,
           l_f_objkey           TYPE sibfinstid,
           l_o_event_parameters TYPE REF TO if_swf_ifs_parameter_container,
           l_f_param_name       TYPE swfdname,
           l_r_msg_key          TYPE scx_t100key,
           l_f_id               TYPE char10.

    CONSTANTS l_c_clsnm TYPE sibftypeid VALUE 'ZCL_AB_WF_MM_PR_APPROVAL',
                l_c_event TYPE sibfevent  VALUE 'CREATE',
                l_c_011   TYPE symsgno    VALUE 011.

    * Setting values of Event Name
    l_f_objtype l_c_clsnm" Your Class Name

    * Instantiate an empty event container
    CALL METHOD cl_swf_evt_event=>get_event_container
      EXPORTING
        im_objcateg  cl_swf_evt_event=>mc_objcateg_cl
        im_objtype   l_c_clsnm
        im_event     l_c_event
      RECEIVING
        re_reference l_o_event_parameters.

    * Set up the name/value pair to be added to the container
    l_f_param_name  'E_F_BANFN'.  " parameter name of the event
    l_f_id          i_f_banfn.
    l_f_objkey      i_f_banfn.

    * Add the name/value pair to the event conainer
    TRY.
        CALL METHOD l_o_event_parameters->set
          EXPORTING
            name  l_f_param_name
            value l_f_id.
    ENDTRY.

    * Raise the event passing the prepared event container
    TRY.
        CALL METHOD cl_swf_evt_event=>raise
          EXPORTING
            im_objcateg        cl_swf_evt_event=>mc_objcateg_cl
            im_objtype         l_f_objtype
            im_event           l_c_event
            im_objkey          l_f_objkey
            im_event_container l_o_event_parameters.
    ENDTRY.

    COMMIT WORK.
  ENDMETHOD.
Step 12: Save and activate.

-------------------------------------------------------------Creating workflow-----------------------------------------------------------------------

Step 1: Goto T-code SWDD.

Step 2: Click on create and save.

Step 3: Select workflow container and create fields.

Step 4: In our case creating pr number, instance for class, object for class and business object.

            * PR number-- Importing and exporting.


            *  Instance for class - Importing and exporting.


            *  Business object -- importing and exporting.


            * Instance for workflow-- Importing and exporting.

            * Create an extended field.


            * Create object key field.


            * Create object type field.


Step 5: Save and activate up to this.

Step 6: Click on GOTO --> basic data and click on starts event tab. Select category CL and give class name and event.

         Note: Binding must be imp and active is also imp.


            *   Generate binding and activate the class. 


             * Save and back.


Step 7: Create activity for Workflow Linkage.
            * 
 Binding must be needed.
            * 
 Main Binding (Below of task).
                
Step 8: Save and activate.

        * Final workflow.
-----------------------------------------------Raise an event for custom class--------------------------------------------------------------------------------
Step 1: Create BADI implementation for ME_PROCESS_REQ_CUST.
            * Implement the POST method.
                METHOD if_ex_me_process_req_cust~post.
        IF sy-tcode 'ME51N'.
          DATAl_o_ref TYPE REF TO ZCL_AB_WF_MM_PR_APPROVAL.
        *------    CREATE OBJECT l_o_ref( ).
          CREATE OBJECT l_o_ref
            EXPORTING
              i_f_banfn im_banfn.
          CALL METHOD l_o_ref->register_wf
            EXPORTING
              i_f_banfn im_banfn.
        ENDIF.
      ENDMETHOD.
----------------------------------------------------------------Testing the workflow-------------------------------------------------------------------------------


            * Goto ME51N T- Code.
            * Create PR.
            * Goto T-code SWIA.

********************************Thank You*****************************

Comments

Popular posts from this blog

New syntax for append- VALUE (new syntax 7.4+) in ABAP

Read statement new syntax in ABAP. (7.4+).

Concatenation new syntax( 7.4+ ).