getRemainingUnits()

The getRemainingUnits() function (server-side) lets you verify the number of governance usage units left for a particular server-side script before reaching the limit. In NetSuite CPQ Configurator, the maximum number of usage units allowed for each server-side script is 927.

Syntax

Use this syntax for the getRemainingUnits() function:

            getRemainingUnits(); 

          

Return Value

The getRemainingUnits() function returns the number of govern usage units left for a particular server-side script.

Parameters

The getRemainingUnits() function doesn't require any parameters.

Examples

The following examples show how to use the getRemainingUnits() function.

Measuring Governance Usage Units During an Iterative Process

This example shows how to use the getRemainingUnits() function in a server-side script to ensure that the loop that creates records doesn't exceed the governance unit limit. If the server-side action runs out of usage units, it exits the loop. For more information about tracking governance usage units during an iterative process, see Performing Server Requests in Batches.

              // Server-side action
let records = scratchpad.records,
    count = records.length,
    weight = 0;
do {
    let remaining = getRemainingUnits();
    if (remaining < weight) {
        let unprocessed = count-scratchpad.index; // Records to be created on the following requests
        console.log('Operation completed. Records left:', unprocessed);
        restartAction();
    }
    let record = records[scratchpad.index];
    createRecord(record).done((data) => {
        scratchpad.output.push(data);
    });
    if (!weight) {
        weight = remaining - getRemainingUnits(); // How many units are consumed by a single iteration
    }
    scratchpad.index++;
} while (scratchpad.index < count); 

            

Measuring Governance Usage Units for a Specific Operation

This example shows how to calculate the number of governance units used by a specific operation. This approach is useful for monitoring resource usage when debugging and optimizing script performance.

              // Inspect this iteration for debugging purposes

const current = getRemainingUnits();

// ... Performs all actions required to measure the unit load

const saved = saveFile({
    name: `TempFile${i}.txt`, // File name
    folder: '88644',
    content: `FILE_CONTENT ${i}`,
    'async': false 
});

// ... Performs more actions

// Calculates remaining units
const after = getRemainingUnits();

// Gets the number of units per operation
console.log(current - after); 

            

Related Topics

General Notices