Passing Parameters to Profound.js Modules
Overview
In order for your modules to accept parameters, you need to define the module to have input parameters. In addition, you need to set the refParm property to the variables that are being passed in.
Defining the Parameters
Inside the module, bind each function argument to a strongly typed field with pjs.define() and the refParm property:
function myModule(parm1, parm2) {
pjs.define("parm1", { type: "char", length: 20, refParm: parm1 });
pjs.define("parm2", { type: "packed", length: 7, decimals: 2, refParm: parm2 });
//Continue pjs logic here.
}Exporting the Parameters
You also need to export the parameters and their definitions when exporting your module:
module.exports.run = myModule;
module.exports.parms = [
{ type: "char", length: 20 },
{ type: "packed", length: 7, decimals:2 }
];Complete Example
The example below puts both halves together and shows the IBM i side of the call. It defines a module that receives a customer name and a credit limit, logs what it received, and changes the credit limit.
custupd.js
// modules/MYAPP/custupd.js
//
// Called from IBM i with two parameters:
// 1. a 20-character customer name (input)
// 2. a packed(7,2) credit limit (input and output)
//
// Both are passed BY REFERENCE, so a value assigned to creditLimit inside this
// module is visible to the caller when the module returns.
function custupd(custName, creditLimit) {
// Bind each JavaScript argument to a strongly typed field using refParm.
// The type, length and decimals MUST match what the caller declares --
// if they do not, the parameter buffer is read incorrectly.
pjs.define("custName", { type: "char", length: 20, refParm: custName });
pjs.define("creditLimit", { type: "packed", length: 7, decimals: 2, refParm: creditLimit });
console.log(`Received ${custName.trim()} with a credit limit of ${creditLimit}`);
// Assigning to the field writes back through refParm to the caller's variable.
creditLimit = 5000.00;
}
// The entry point Profound.js calls.
module.exports.run = custupd;
// The parameter definitions the IBM i side uses to size the buffer it sends.
// This list must match the pjs.define() calls above, in the same order.
module.exports.parms = [
{ type: "char", length: 20 },
{ type: "packed", length: 7, decimals: 2 }
];The module can then be called from IBM i. The declared type and length of each CL variable must match the corresponding entry in module.exports.parms:
Calling the module with PJSCALL
/* Call the module from a CL program or an IBM i command line. */
/* MODULE - the module name; the .js extension can be omitted */
/* DIRECTORY - the subdirectory of "modules" that holds it; specify */
/* *PATHLIST to search the pathlist instead */
DCL VAR(&NAME) TYPE(*CHAR) LEN(20) VALUE('ACME SUPPLY')
DCL VAR(&LIMIT) TYPE(*DEC) LEN(7 2) VALUE(1000.00)
PJSCALL MODULE(custupd) DIRECTORY(MYAPP) PARM(&NAME &LIMIT)
/* The module logs: Received ACME SUPPLY with a credit limit of 1000.00 */
/* */
/* NOTE: the PJSCALL *command* does not write changed values back into CL */
/* variables, so &LIMIT is still 1000.00 here. Use the PJSCALL *program* */
/* or a proxy program when you need the updated value returned. */As shown in the example, the PJSCALL command does not return changed parameter values to CL variables. If the caller needs the values the module changed, use the PJSCALL program or a proxy program instead.