Showing posts with label Workflow. Show all posts
Showing posts with label Workflow. Show all posts

Thursday, April 16, 2020

Calling Workflow When We Creating Sales order in SAP ABAP.


Trigger Workflow When We Creating Sales order in SAP ABAP.




Trigger Workflow Creating Sales order in SAP ABAP.

*DATA :lt_container TYPE STANDARD TABLE OF swr_cont,
*      ls_container TYPE swr_cont.
*IF sy-tcode = 'VA02'.
*  SELECT SINGLE kvgr2 FROM vbak
*    INTO lv_kvgr2
*    WHERE vbeln = vbak-vbeln.
*  IF vbak-kvgr2 = lv_kvgr2.
*  ELSE.
**    PERFORM call_workflow.
*    flag_kvgr2 = 'X'.
*    ls_container-element = 'DOC_NO'.
*    ls_container-value = vbak-vbeln.
*    APPEND ls_container TO lt_container.
*    CLEAR : ls_container.
*
*    CALL FUNCTION 'SAP_WAPI_START_WORKFLOW'
*      EXPORTING
*        task                      = 'WS999001233'
*        language                  = sy-langu
*        do_commit                 = 'X'
*        user                      = sy-uname
**       START_ASYNCHRONOUS        = ' '
**       DESIRED_START_DATE        =
**       DESIRED_START_TIME        =
**       DESIRED_START_ZONLO       = SY-ZONLO
**       IFS_XML_CONTAINER         =
**     IMPORTING
**       RETURN_CODE               =
**       WORKITEM_ID               =
**       NEW_STATUS                =
*      TABLES
*        input_container           = lt_container
**       MESSAGE_LINES             =
**       MESSAGE_STRUCT            =
**       AGENTS                    =
*              .
*    IF sy-subrc = 0.
*      EXPORT flag_kvgr2 FROM flag_kvgr2 TO MEMORY ID 'SY-UNAME'.
*    ENDIF.
*  ENDIF.
*ENDIF.

Thursday, February 13, 2020

Multilevel Workflow for Sales order change [VA02] In SAP ABAP.


Multilevel Workflow for Sales order change [VA02]

    For this type of workflow first we have to find out maximum level for the approval.
    For this we have to create activity.

 



Here we have to mention class name and its method as well as if it required then you to create some variable at container tab for import or export parameter.


Above binding is inside the task now below binding is outside the task.


Class method for finding maximum level for approver.


Method for get maximum level of approver
METHOD get_maxlevel.
  
DATA:ls_max_level TYPE zwf_dyn_vbak-zalevel.
  
DATA:lv_inita TYPE uname.

  lv_inita 
=  wfinitiator+2(12).

  
IF zterm 'X'.
    
SELECT MAXzalevel )
     
FROM zwf_dyn_vbak
     
INTO ls_max_level
     
WHERE approveby lv_inita AND field_name 'ZTERM'.
    max_level 
ls_max_level.
  
ENDIF.

  
IF kvgr2 'X'.
    
SELECT MAXzalevel )
      
FROM zwf_dyn_vbak
      
INTO ls_max_level
      
WHERE approveby lv_inita AND field_name 'AVGR2'.
    max_level 
ls_max_level.

  
ENDIF.
ENDMETHOD.


Next activity is we have to generate loop step in workflow.





After getting maximum level for approver next we have to create an instance for generate link for sales order in email. Below screen for instance binding inside the task.


Next screen is outside binding after task is created.


Now in loop we have to increment the counter using custom container.







Code for Email ID with Approver level:
METHOD get_emailid.

  
DATAis_wf_vbak TYPE zwf_dyn_vbak,
        is_addsmtp 
TYPE bapiadsmtp.

  
DATA lv_inita TYPE uname,
         lv_add 
TYPE bapiaddr3.
  
DATA return TYPE bapiret2.

  lv_inita 
wfinitiator+2(12).
  
IF zterm 'X'.
    
SELECT SINGLE *
    
FROM zwf_dyn_vbak
    
INTO is_wf_vbak
   
WHERE approveby lv_inita AND zalevel apprv_level AND             field_name 'ZTERM'.

    
IF sy-subrc 0.
      email_app 
is_wf_vbak-a_emailid.
      email_init 
is_wf_vbak-i_emailid.
    
ENDIF.
  
ENDIF.

  IF kvgr2 'X'.
    
SELECT SINGLE *
    
FROM zwf_dyn_vbak
    
INTO is_wf_vbak
   
WHERE approveby lv_inita AND zalevel apprv_level AND             field_name 'AVGR2'.

    
IF sy-subrc 0.
      email_app 
is_wf_vbak-a_emailid.
      email_init 
is_wf_vbak-i_emailid.
    
ENDIF.
  
ENDIF.
ENDMETHOD.


Now next activity is user decision for whether user has approve or not sales order.
In this user decision we have to create rule. After this we have to set flag whether it is approved or reject. If user has reject the sales order then we have to set loop level with max level.
Next screen for set flag for approve.


Below screen is for set flag for rejection.


After Reject flag set then set the loop level and max level same.


After the approve or reject process we have to check the flag if it is approve or reject.






Binding for approve sales order.



Code for set billing block as well as for approve and reject sales order.



method SET_BILLING_BLOCK.

  
DATA:ls_tvfst TYPE tvfst.

  
IF zterm 'X'.
  
SELECT SINGLE faksp vtext
    
FROM tvfst
    
INTO CORRESPONDING FIELDS OF ls_tvfst
    
WHERE faksp '41' AND spras 'EN'.

  
UPDATE vbak
    
SET faksk ls_tvfst-faksp
    
WHERE vbeln vbeln.
ENDIF.

IF kvgr2 'X'.
  
SELECT SINGLE faksp vtext
    
FROM tvfst
    
INTO CORRESPONDING FIELDS OF ls_tvfst
    
WHERE faksp '41' AND spras 'EN'.

  
UPDATE vbak
    
SET faksk ls_tvfst-faksp
    
WHERE vbeln vbeln.
ENDIF.
endmethod.


Method for approve sales order

method APPROVE_SALES_ORDER.
  
IF zterm 'X'.
    
UPDATE vbak
      
SET zzterm1 ' '
      
where vbeln vbeln.
  
ENDIF.

  
IF kvgr2 'X'.
    
UPDATE vbak
      
SET zzkvgr2 ' '
      
where vbeln vbeln.
  
ENDIF.
endmethod.

Method for Reject sales order with attachment file for reject reason.

METHOD reject_sales_order.
  
IF zterm 'X' OR kvgr2 'X'.
    
DATA lv_docid TYPE so_entryid,
           lv_docid_tmp 
TYPE char90,
           lt_attach 
TYPE STANDARD TABLE OF swr_object.

    
DATA ls_attach TYPE swr_object.
    
DATA flag TYPE flag.

    
FIELD-SYMBOLS <ls_rejtext> TYPE solisti1.

    
CALL FUNCTION 'SAP_WAPI_GET_ATTACHMENTS'
      
EXPORTING
        workitem_id 
im_wiid
      
TABLES
        attachments 
lt_attach.

    
IF NOT lt_attach[] IS INITIAL.
      
READ TABLE lt_attach INTO ls_attach INDEX 1.
      
IF sy-subrc EQ 0.
        
REPLACE ALL OCCURRENCES OF 'SOFM' IN ls_attach-object_id WITH space.
        
SHIFT ls_attach-object_id LEFT DELETING LEADING space.
*      CONDENSE LV_DOCID NO-GAPS.
        lv_docid_tmp 
ls_attach-object_id.
*      CONDENSE LV_DOCID_TMP NO-GAPS.
        lv_docid 
lv_docid_tmp.
      
ENDIF.
    
ENDIF.
***FM to  get content text for Dicument key
    
CALL FUNCTION 'SO_DOCUMENT_READ_API1'
      
EXPORTING
        document_id                
lv_docid
      
TABLES
        object_content             
text
      
EXCEPTIONS
        document_id_not_exist      
1
        operation_no_authorization 
2
        x_error                    
3
        
OTHERS                     4.
    
IF sy-subrc <> 0.
* Implement suitable error handling here
    
ELSE.
      
DELETE text WHERE line+0(3) = '000'.
      
LOOP AT text ASSIGNING <ls_rejtext>.
        <ls_rejtext>+0
(3) = ' '.
      
ENDLOOP.
    
ENDIF.
  
ENDIF.
ENDMETHOD.



After approve from all level we have to remove the billing block.


method UPDATE_VBAK.
DATA:ls_vbak TYPE vbak.

  
SELECT SINGLE *
    
FROM vbak
    
INTO ls_vbak
    
WHERE vbeln vbeln.

    
IF ls_vbak-zzterm1 ' ' and ls_vbak-zzkvgr2 ' '.
      
UPDATE vbak
        
SET faksk ' '
        
where vbeln =  vbeln.
    
ENDIF.
endmethod.