{"id":7842,"date":"2025-09-13T01:45:12","date_gmt":"2025-09-12T20:15:12","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=automating-sap-reports-how-to-schedule-background-jobs-via-abap"},"modified":"2025-09-13T01:45:12","modified_gmt":"2025-09-12T20:15:12","slug":"automating-sap-reports-how-to-schedule-background-jobs-via-abap","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=automating-sap-reports-how-to-schedule-background-jobs-via-abap","title":{"rendered":"Automating SAP Reports: How to Schedule Background Jobs via ABAP"},"content":{"rendered":"<p><strong>Scenario:<\/strong><\/p>\n<ul>\n<li>\n<p>Business wants to run a <strong>Sales Order Report<\/strong> (ZSD_SALES_ORDER_REPORT) every night automatically in the background.<\/p>\n<\/li>\n<li>\n<p>Instead of manual execution, a <strong>background job<\/strong> is scheduled from the ABAP program.<\/p>\n<\/li>\n<\/ul>\n<p><strong>CODE:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>REPORT z_schedule_bg_job.<\/p>\n<p>DATA: lv_jobname TYPE tbtcjob-jobname VALUE &#8216;Z_SALES_ORDER_JOB&#8217;,<br \/>\nlv_jobcount TYPE tbtcjob-jobcount,<br \/>\nlv_subrc TYPE sy-subrc.<\/p>\n<p>PARAMETERS: p_variant TYPE variant OPTIONAL.<\/p>\n<p>START-OF-SELECTION.<\/p>\n<p>&#8221; 1. Open Job<br \/>\nCALL FUNCTION &#8216;JOB_OPEN&#8217;<br \/>\nEXPORTING<br \/>\njobname = lv_jobname<br \/>\nIMPORTING<br \/>\njobcount = lv_jobcount<br \/>\nEXCEPTIONS<br \/>\ncant_create_job = 1<br \/>\ninvalid_job_data = 2<br \/>\njobname_missing = 3<br \/>\nOTHERS = 4.<\/p>\n<p>IF sy-subrc &lt;&gt; 0.<br \/>\nMESSAGE &#8216;Unable to create background job&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<\/p>\n<p>&#8221; 2. Submit Report to Job<br \/>\nCALL FUNCTION &#8216;JOB_SUBMIT&#8217;<br \/>\nEXPORTING<br \/>\njobname = lv_jobname<br \/>\njobcount = lv_jobcount<br \/>\nreport = &#8216;ZSD_SALES_ORDER_REPORT&#8217;<br \/>\nvariant = p_variant<br \/>\nEXCEPTIONS<br \/>\nbad_priparams = 1<br \/>\njobname_missing = 2<br \/>\njob_notex = 3<br \/>\nprogram_missing = 4<br \/>\nvariant_missing = 5<br \/>\nOTHERS = 6.<\/p>\n<p>IF sy-subrc &lt;&gt; 0.<br \/>\nMESSAGE &#8216;Error submitting report to job&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<\/p>\n<p>&#8221; 3. Close Job (Schedule for immediate or background processing)<br \/>\nCALL FUNCTION &#8216;JOB_CLOSE&#8217;<br \/>\nEXPORTING<br \/>\njobcount = lv_jobcount<br \/>\njobname = lv_jobname<br \/>\nstrtimmed = &#8216;X&#8217; &#8221; Start immediately<br \/>\nlanguage = sy-langu<br \/>\nEXCEPTIONS<br \/>\ncant_start_immediate = 1<br \/>\ninvalid_startdate = 2<br \/>\njobname_missing = 3<br \/>\njob_close_failed = 4<br \/>\njob_nosteps = 5<br \/>\nOTHERS = 6.<\/p>\n<p>IF sy-subrc = 0.<br \/>\nMESSAGE |Background Job { lv_jobname } scheduled successfully.| TYPE &#8216;S&#8217;.<br \/>\nELSE.<br \/>\nMESSAGE &#8216;Error closing job&#8217; TYPE &#8216;E&#8217;.<br \/>\nENDIF.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How it works<\/strong><\/p>\n<ol>\n<li>\n<p><strong>JOB_OPEN<\/strong> \u2192 Creates the background job and returns a job count.<\/p>\n<\/li>\n<li>\n<p><strong>JOB_SUBMIT<\/strong> \u2192 Assigns a report (with optional variant) to the job.<\/p>\n<\/li>\n<li>\n<p><strong>JOB_CLOSE<\/strong> \u2192 Schedules the job for execution (immediately or at a specified time).<\/p>\n<\/li>\n<\/ol>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,422,421,177],"class_list":["post-7842","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-automation","question_tags-background-jobs","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7842"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7842"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7842"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}