N/dataset Module Script Samples

The following script samples demonstrate how to use the features of the N/dataset module:

Create a Dataset, Run the Dataset, and List All Existing Datasets

The folllowing sample creates a dataset with three columns, one of which is based on a join. Then, all datasets are listed and the newly created dataset is loaded and reviewed.

Note:

This sample script uses the require function so you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (a script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

// The following script creates a dataset with three columns, one of which is based on a join, and a condition. Then, all datasets are listed and the newly created dataset is loaded and reviewed.

require(['N/dataset', 'N/query'], function(dataset, query){ 
    var myTransactionDateColumn = dataset.createColumn({
        fieldId: 'trandate',
        alias: 'date'
    });
    var myJoin = dataset.createJoin({
        fieldId: 'createdby',
        target: 'entity'
    });
    var myNameColumn = dataset.createColumn({
        fieldId: 'lastname',
        alias: 'name',
        join: myJoin
    });
    var myTransactionIdColumn = dataset.createColumn({
        fieldId: 'tranid',
        alias: 'id'
    });
    var myTotalColumn = dataset.createColumn({
        fieldId: 'foreigntotal',
        alias: 'total'
    });
    var myColumns = [myTransactionIdColumn, myNameColumn, myTransactionDateColumn, myTotalColumn];
    var myCondition = dataset.createCondition({
        column: myNameColumn,
        operator: query.Operator.EMPTY
    });
    var myDataset = dataset.create({
        type: 'transaction',
        columns: myColumns,
        condition: myCondition
    });

    // List all created datasets
    var allDatasets = dataset.list();
    log.audit({
        title: 'All datasets:',
        details: allDatasets
    });

    // Review the newly created dataset components (in the log)
    log.audit({
        title: 'My Dataset = ',
        details: myDataset
    });
   
    // Now run the dataset
    var runResult = myDataset.run();
    var runPagedResult = myDataset.runPaged({
        pageSize: 2
    });
    log.audit({
        title: 'myDataset runResult: ',
        details: runResult
    });
    log.audit({
        title: 'myDataset runPagedResult: ',
        details: runPagedResult
    });
}); 

          

List All Datasets and Load the First Dataset

The following sample lists all existing datasets and then loads the first dataset.

Note:

This sample script uses the require function so you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (a script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

// The following script lists all existing datasets and then loads the first dataset.

require(['N/dataset'], function(dataset){ 
    // List all created datasets
    var allDatasets = dataset.list();
    log.audit({
        title: 'All datasets:',
        details: allDatasets
    });
    
    // Load the first dataset
    var myFirstDataset = dataset.load({
        id: allDatasets[0].id
    });
    log.audit('myFirstDataset:', myFirstDataset);
}); 

          

General Notices