file.write()
Description
file.write() inserts a new record into a database file, in the same way as the RPG WRITE operation. It is called on the file instance created by pjs.defineTable(), using the internal file name (for example, productsp.write()). The file must be defined with write: true.
By default, the record is built from the current values of the record format fields in the calling scope. Alternatively, an output data structure can be passed to supply the record data.
Syntax
file.write()
file.write(recordFormatName)
file.write(recordFormatName, outputDataStructure)Parameters
Record format name (String, optional) - The name of the record format to write. This parameter is required when the file has more than one record format, and may be omitted for files with a single record format.
Output data structure (Data structure reference, optional) - A Profound.js data structure that supplies the record data. If specified, the record is built from the data structure instead of the individual fields in the calling scope. If the data structure is not qualified, wrap the parameter with the pjs.ds() API.
Return Value
None.
Exception Handling
If the operation fails, a Profound.js Error is thrown with the following properties:
name - Always
"Profound.js Error".message - A description of the problem.
statusCode - A numeric file status code, or
0when the error has no specific code. The same code is returned by file.status().
Conditions that cause this method to throw include:
Profound.js Error 1211 (I/O operation to a closed file) - The file is not open, for example a file defined with
userOpen: truethat has not been opened with file.open() yet.Not valid for WRITE operation - The file was not defined with
write: truein the pjs.defineTable() configuration. ThestatusCodeis0.Record does not exist - The record format name passed does not exist in the file.
Record name is required - The record format name was omitted, but the file has more than one record format.
Profound.js Error 1021 (Tried to write a record that already exists) - The file has unique keys and a record with the same key already exists.
Profound.js Error 1299 (Other I/O error detected) - IBM i reported another error, such as a referential constraint or trigger failure. The
messageincludes the IBM i message id.
Examples
Example 1: Write a New Record
The record format fields are set in the calling scope and then written to the file as a new record.
pjs.defineTable("productsp", { write: true });
// Set the record format fields.
prid = 500;
prname = "Widget";
prprice = 9.99;
// Insert the record. The file has one record format, so no name is needed.
productsp.write();Example 2: Write a Record From a Data Structure
The file is defined as qualified, so its fields are not created in the calling scope. A data structure defined with likerec is populated and passed to write(). null is passed for the record format name because the file has one format.
pjs.defineTable("productsp", { write: true, qualified: true });
pjs.define("product", { type: "data structure", qualified: true,
likerec: { intrecname: "productsp.productsr" } });
product.prid = 500;
product.prname = "Widget";
product.prprice = 9.99;
productsp.write(null, product);Example 3: Handle a Duplicate Key
The file has a unique key. A write that would create a duplicate throws error 1021, which the program catches so it can report the problem instead of ending.
pjs.defineTable("productsp", { write: true });
prid = 500;
prname = "Widget";
try {
productsp.write();
}
catch (error) {
if (error.statusCode === 1021) {
// A product with id 500 already exists.
}
else {
throw error; // some other problem; let it propagate
}
}RPG Equivalent
WRITE
Requirements
This API requires the Profound.js Connector module.