Schedule recurring background job – CRM_TERRMAN_PROC_REL
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)
- Create a custom event using SM62
- Schedule a job using SM36. It should be a two-step periodic job
- Step 1: the main program which needs to run as background job
- Step 2: the program BTC_EVENT_RAISE to raise the custom event
- Raise the custom event manually once by executing BTC_EVENT_RAISE (in background or foreground)
Pros:
- It doesn’t need any coding
Cons:
- Event created for this purpose can only be used for execution of one program. For more than one program more events needs to be created.
- If the report execution in background takes very small time then it is very difficult to stop the job execution. It keeps executing in background forever
Solution 2: Write a custom program which keeps executing main program in background
Pros:
- This single program can be used to schedule different programs’ execution in background
- The recurrence can easily be controlled by stopping execution of custom program in SM37
Cons:
- It needs a custom program
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.