Another Way to Pass Parms to SBMJOB
October 5, 2011 Bruce Guetzkow
Note: The code accompanying this article is available for download here. A couple of years ago I needed to develop a process where a group of items could be selected from a screen and a report listing those items could be generated. On the surface, a simple task. Of course, if you give a mouse a cookie, they’re going to want a glass of milk, or another report. . . and another. Again, not too complicated. All that should be needed is a way to submit a call to a variable list of programs. As long as each program being called has the exact same parameter list, you can use a program variable to contain the program name and submit the call. What happens if the parameter list is not the same for each program? What if the parameters for each program are vastly different? Although you can dynamically build the Submit Job (SBMJOB) command with different parameter lists, there is another way. Environment variables can be passed from a calling job to a called job in much the same manner as the Local Data Area (*LDA) is passed. When a job is submitted, a copy of the *LDA from the calling job is passed to the called job. There are now two separate jobs, each with its own independent copy of the data. Environment variables can be passed by changing one parameter on the SBMJOB command: CPYENVVAR(*YES) - the default is *NO Before you can submit the job you first need to set your environment variables. There are both CL commands and APIs at your disposal for managing them. The commands are:
There are 10 APIs, but the three of most interest are:
You will find prototypes for these APIs in the downloadable code. Environment variables are a “name=value” pair. The case-sensitive names can be just about anything, but should be something meaningful to you. You should also use the WRKENVVAR command to make sure that you don’t replace any existing variables. In the calling program, setting or changing an environment variable in RPG is as easy as: putenv( 'MyEnvVar=' + value ); Remember that the environment variable name is case-sensitive. There must not be a space either before or after the equal sign. Values assigned must always be character, so if you have a date or numeric value you’ll have to convert it to character first. You can also check the return value to confirm that the environment variable has been set successfully (success is a return value of zero): if ( putenv( 'MyEnvVar=' + value ) = *zeros ); You can submit the call to a program without passing a single parm as the values will be retrieved from the environment variables, provided that you change the CPYENVVAR parm on the SBMJOB command as indicated above. In the called program, retrieving the environment variables is just as easy: charvalue = %str( getenv( 'MyEnvVar' ) ); or numvalue = %dec( %str( getenv( 'MyEnvVar' ) ) : dec : pos ); Since the “getenv” API returns a pointer you’ll need to use the “Get” or “Store Null-Terminated String” (%STR) built-in function to assign the value to a program variable. The first example is for character values, the second for numeric. All environment variables are stored as character, so you’ll need to use an appropriate method to convert that character value to the numeric or date data type desired. The result of this process is that you have a simple method of using the SBMJOB command from any HLL program, and no restriction as to the number of parms a program can have. You can learn more about environment variables in the IBM Information Center. Bruce Guetzkow, an independent IBM i programming consultant with GmanTech Consulting in southeastern Wisconsin, is a firm believer in practical programming. For over 25 years he has developed applications for IBM systems from mainframe to System/36 to IBM i on Power. You can read his Website blog, follow him on Twitter (@gmantechi), or catch him at a meeting of the Wisconsin Midrange Computer Professional Association (WMCPA), where he is the current webmaster. Send your questions or comments for Bruce to Ted Holt via the IT Jungle Contact page.
|