N/dataStore Module Script Samples

The following Suitelet sample inserts a document into the TEST_COLLECTION collection in the default data store. It writes the insertion result or error message to the response.

Deploy this script as a Suitelet and open its deployment URL to run it. Each request inserts a new document.

          /**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/dataStore'], function (dataStore) {
    async function onRequest(context) {
        try {
            const collection = dataStore.getCollection('TEST_COLLECTION');

            const result = await collection.insertOne({
                message: 'Hello from the Suitelet',
                count: 1,
                enabled: true
            });

            context.response.write('Inserted successfully:\n' + JSON.stringify(result));
        } catch (error) {
            context.response.write('Insert failed: ' + error.name + ': ' + error.message);
        }
    }

    return { onRequest };
}); 

        

Related Topics

General Notices