
Introduction to Business Requirement:
I felt this need while working on SAP CRM upgrade project where the customer was making use of territory management and they were making changes to the customer <-> territory relationships very frequently. In CRM 7 where the territory solution is rule based, making changes to the customer assignment in territory on web UI and updating this relationship in table CRMD_TERR_ACCREL are two different steps. You cannot expect the right territory manager to be determined while creating an order before both steps are executed. Step 2 is executing report CRM_TERRMAN_PROC_REL in background. According to SAP best practices you should be scheduling this report to execute in background at regular interval (of say 3 hours) but what happens to the orders which are created after a change is made to the customer assignment to territory and before program CRM_TERRMAN_PROC_REL executed in background as per the schedule?
Here comes the need for scheduling this report program to execute in background forever.
I wanted to achieve this by configuration (it’s a best practice) so I started a discussion on SCN for this where I got a satisfactory answer but it landed me into another issue. Below is the link to that discussion:
http://scn.sap.com/thread/3884326
Conclusion: There are different ways of achieving this
Solution 1: By configuration (as answered in the discussion/thread above)
Pros:
Cons:
Solution 2: Write a custom program which keeps executing main program in background
Pros:
Cons:
Program code:
*&---------------------------------------------------------------------*
*& Report ZSCHEDULE_RECURRING_JOB
*&---------------------------------------------------------------------*
REPORT zschedule_recurring_job.
DATA: lv_jname TYPE btcjob,
lv_jcnt TYPE btcjobcnt,
ls_prn_param TYPE pri_params,
lt_joblist TYPE TABLE OF tbtcjob,
lv_active TYPE boolean.
PARAMETERS: p_pause TYPE i DEFAULT 10, " Pause between execution of two instances
p_prog TYPE PROGNAME DEFAULT 'CRM_TERRMAN_PROC_REL',
p_var TYPE BTCVARIANT DEFAULT 'INITIAL'.
DO.
IF lv_active EQ abap_false.
WAIT UP TO p_pause SECONDS.
* Prepare job name
lv_jname = p_prog.
* Job open
CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = lv_jname
IMPORTING
jobcount = lv_jcnt
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc = 0.
* Submit the report to execute in background
SUBMIT (p_prog) USING SELECTION-SET P_VAR TO SAP-SPOOL
SPOOL PARAMETERS ls_prn_param
WITHOUT SPOOL DYNPRO
VIA JOB lv_jname NUMBER lv_jcnt
AND RETURN.
IF sy-subrc = 0.
* Job close
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = lv_jcnt
jobname = lv_jname
strtimmed = 'X'
EXCEPTIONS
cant_start_immediate = 1
invalid_startdate = 2
jobname_missing = 3
job_close_failed = 4
job_nosteps = 5
job_notex = 6
lock_failed = 7
OTHERS = 8.
ENDIF.
ENDIF.
ENDIF.
lv_active = abap_true.
CLEAR lt_joblist.
CALL FUNCTION 'BP_FIND_JOBS_WITH_PROGRAM'
EXPORTING
abap_program_name = p_prog
status = 'R' " Running
TABLES
joblist = lt_joblist
EXCEPTIONS
no_jobs_found = 1
program_specification_missing = 2
invalid_dialog_type = 3
job_find_canceled = 4
OTHERS = 5.
IF lt_joblist IS INITIAL.
lv_active = abap_false.
ELSE.
WAIT UP TO 2 SECONDS.
ENDIF.
ENDDO.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.