There are many times when I want/need to have an interactive program submit a job to batch, and the interactive program would be so small it seems a waste to have such a program. For example the interactive program presents the user with a confirmation screen and then submits a program to batch that does all the processing. Perhaps the solution would be to have both the interactive and batch commands in the same program and it somehow know when it is running interactively or in batch.
Fortunately the 'Retrieve Job Attributes', RTVJOBA, command allows to retrieve which environment (interactive or batch) the job is running in. Combine this with subroutines in CL, see Subroutine in CL, and I can create a program that can run different commands in both environments.
The keyword in the RTVJOBA command that contains the environment is TYPE. It is a 1 character field that contains '0' (zero) if it is in batch or '1' if it is interactive.
In this example program, PGM001, I want to display a confirmation screen if the job is interactive, then if Enter is pressed it will submit itself to batch. By using subroutines I can separate the interactive commands from the batch ones, see below:
01 PGM PARM(&STRDTE &ENDDTE) 02 DCL VAR(&JOBTYPE) TYPE(*CHAR) LEN(1) 03 DCLF FILE(PGM001DF) 04 RTVJOBA TYPE(&JOBTYPE) 05 SELECT 06 WHEN COND(&JOBTYPE = '0') THEN(CALLSUBR SUBR(SR_BATCH)) 07 WHEN COND(&JOBTYPE = '1') THEN(CALLSUBR SUBR(SR_INTERAC)) 08 ENDSELECT /*============================================================*/ /* When running interactively */ 09 SUBR SUBR(SR_INTERAC) 10 SNDRCVF 11 IF COND(&IN03) THEN(RETURN) 12 SBMJOB CMD(CALL PGM(PGM001)) JOB(PGM001_TST) 13 ENDSUBR /*============================================================*/ /* When running in batch */ 14 SUBR SUBR(SR_BATCH) 98 ENDSUBR /*============================================================*/ 99 ENDPGM
The job type/environment is retrieved by the RTVJOBA command on line 4. If the value is '1' I call the subroutine to run the interactive commands, SR_INTERAC, line 7. If the value is '0' then I call the batch subroutine, SR_BATCH, line 6.
If you have not used the SELECT command you can learn about it in the post Select in CL.
The interactive subroutine just executes a display file, line 10, and submits the PGM001, itself, to batch, line 12.
If the program needed to pass parameters when submitting itself to batch it would need to be called with blank parameter(s) from the menu. For example if PGM002 needs 2 parameters when it is submitted to batch I would code the following in the menu:
CALL PGM(PGM002) PARM('' '')
You can learn more about the RTVJOBA command from the IBM website here.
This article was written for IBM i 7.1, and should work for earlier releases.
I have been informed if you are programming for a web or mobile app the method mentioned above may not work. But it will if you are programming in traditional green screen style.
To learn more read these two comments: