How to write ABAP program to trigger PC based on file in application server
In this post, I will detail on how to write an ABAP program which checks for a specific file in the application server and triggers a process chain. If no file is found, it just displays a message and does not do anything.
Once the program is ready, you need to schedule a job with this program which runs in the background every 30 minutes for example to check the file and trigger the PC.
Sample Scenario:
The program checks for sales file in the folder /usr/sap/DEV/XYZ/Reports in the DEV system and similarly in /usr/sap/QA/XYZ/Reports and /usr/sap/PROD/XYZ/Reports folders in the QA and PROD systems respectively.
If the file is found, it triggers the relevant PC and leaves a message. If the file is not found, it generates a message and terminates.
REPORT zsample_pc_trigger. * -------Description--------------------------------- " --------------------------------------------------" *------Description-----------------------------------* * This program checks the files exist in XYZ Reports folder * in application file path directory, if exists then respective * process chain triggers, if no files then no PC triggers * Owner: Akanksha Verma * Job created for this program at regular interval(Job Name: ZPC_TRIGGER) *------------------------------------------------------* *AUTHOR : AK Verma *Date : 17 Feb 2017 *---------Changes---------------------------------------* * 1) Done by: * 2) Date: * 3) Change description: *--------------------------------------------------------* TYPES: BEGIN OF itab1 , rec(1000) TYPE c, END OF itab1. DATA : itab TYPE TABLE OF itab1, wa TYPE itab1. DATA: f_fname TYPE localfile . " Final file name. DATA: p_file TYPE localfile. " To capture all file name DATA: ifile TYPE TABLE OF salfldir WITH HEADER LINE. DATA: p_path TYPE salfile-longname." VALUE '/usr/sap/DEV/XYZ/Reports'. DATA: va_files TYPE C LENGTH 6. DATA: lv_chain TYPE rspc_chain. IF sy-sysid EQ 'DEV'. p_path = '/usr/sap/DEV/XYZ/Reports'. ELSEIF sy-sysid EQ 'QA'. p_path = '/usr/sap/QA/XYZ/Reports'. ELSEIF sy-sysid EQ 'PROD'. p_path = '/usr/sap/PROD/XYZ/Reports'. ENDIF. * To get all file in application file folder CALL FUNCTION 'RZL_READ_DIR_LOCAL' EXPORTING name = p_path TABLES file_tbl = ifile EXCEPTIONS argument_error = 1 not_found = 2 OTHERS = 3. DELETE ifile WHERE name CS 'Consolidated'. "Ifile contains all file names in application folder LOOP AT ifile. IF ifile-name CS '.csv'. SKIP. ELSEIF ifile-name CS '.xml'. SKIP. ELSE. DELETE ifile. ENDIF. ENDLOOP. " CHECK IF FILE EXIST IF ifile[] IS INITIAL. MESSAGE 'No Files to process' TYPE 'I'. EXIT. ELSE. MESSAGE 'File exist and continue of process' TYPE 'I'. ENDIF. " files exists? LOOP AT ifile. IF ifile-name CS 'SALES'. IF ifile-name CS 'TARGET'. lv_chain = 'ZSALES_PC_PLN'. elseif ifile-name CS 'ACTUAL'. lv_chain = 'ZSALES_PC_ACT'. ENDIF. ELSEIF ifile-name CS 'FINANCE'. lv_chain = 'ZFIN_PC'. ELSEIF ifile-name CS 'HR'. lv_chain = 'ZHR_PC'. ENDIF. CHECK lv_chain IS NOT INITIAL. " trigger relevant PC CALL FUNCTION 'RSPC_API_CHAIN_START' EXPORTING i_chain = lv_chain i_synchronous = rs_c_false i_simulate = rs_c_false i_dont_wait = rs_c_true * IMPORTING * E_LOGID = EXCEPTIONS failed = 1 OTHERS = 2 . IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. " write log MESSAGE 'Process triggered for PC' && '-' && lv_chain TYPE 'I'. " each program execution only one PC triggers EXIT. ENDLOOP.
Save, check and activate your program.
Now for creating a job, go to transaction SM36
Then click on "Step" option and give the program name.
Save it and click on the Start condition button. Schedule it at every hour
Check the "Periodic job" check box and save.
Now the job is scheduled and can be monitored in SM37.
Notice than the job log displays the message whether the file is found or not and which PC has been triggered if the file was found.
If no files are found, the job log displays no files to process.
Comments
Post a Comment