Entry Point Script Validation Examples

Here are some examples of correct and incorrect SuiteScript scripts:

Example of a Valid Script that Includes All Required Elements

The following script is valid. It includes a define statement, a return statement with an entry point, and an entry point function that corresponds with the entry point. Plus, the @NScriptType JSDoc tag matches the script type interface, and the @NApiVersion JSDoc tag is correct.

          /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

define(['N/record'], (record) => {
    function myBeforeSubmitFunction(context) {
        if (context.type !== context.UserEventType.CREATE)
            return;
        let customerRecord = context.newRecord;
        customerRecord.setValue({
            fieldId: 'comments', 
            value: 'Please follow up with this customer!'
        });
    }
    return {
        beforeSubmit: myBeforeSubmitFunction
    };
}); 

        

Example of an Invalid Script — Missing Return Statement

The following script is invalid because it doesn't have a return statement. If you try to upload a script that doesn't include a return statement, the system returns the error “SuiteScript 2.0 entry point scripts must implement one script type function.” For more information, see SCRIPT_OF_API_VERSION_20_MUST ....

          /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

define(['N/record'], (record) => {
    function myBeforeSubmitFunction(context) {
        if (context.type !== context.UserEventType.CREATE)
            return;
        let customerRecord = context.newRecord;
        customerRecord.setValue({
            fieldId: 'comments',
            value: 'Please follow up with this customer!'
        });
    }
}); 

        

Example of an Invalid Script — Missing Define Statement

The following script is invalid because it uses a require statement instead of a define statement. If you try to upload a script that includes a require statement, the system returns the error “SuiteScript 2.0 entry point scripts must implement one script type function.” For more information, see SCRIPT_OF_API_VERSION_20_MUST .... For more information about using the require statement, see require Function.

          /**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */

require(['N/record'], (record) => {
    function myBeforeSubmitFunction(context) {
        if (context.type !== context.UserEventType.CREATE)
            return;
        let customerRecord = context.newRecord;
        customerRecord.setValue({
            fieldId: 'comments',
            value: 'Please follow up with this customer!'
        });
    }
}); 

        

Example of an Invalid Script — Missing @NScriptType JSDoc Tag

The following script is invalid because it doesn't have an @NScriptType JSDoc tag. You can upload the script file, however, if you try to create a script record for the script, the system returns the error “@NScriptType is required for 2.0 entry point script.” For more information, see MISSING_SCRIPT_TYPE.

          /**
 * @NApiVersion 2.1
 */

define([ 'N/record' ], (record) => {
    function myBeforeSubmitFunction(context) {
        if (context.type !== context.UserEventType.CREATE)
            return;
        let customerRecord = context.newRecord;
        customerRecord.setValue({
            fieldId: 'comments',
            value: 'Please follow up with this customer!'
        });
    }
    return {
        beforeSubmit : myBeforeSubmitFunction
    };
}); 

        

Example of an Invalid Script — Missing @NApiVersion JSDoc Tag

The following script is invalid because it doesn't have an @NApiVersion JSDoc tag. If you try to upload a script file that doesn't include the @NApiVersion JSDoc tag, the system returns the error “Failed to validate script file.” For more information, see FAILED_TO_VALIDATE_SCRIPT_FILE.

          /**
 * @NScriptType UserEventScript
 */

define(['N/record'], function(record) {
    function myBeforeSubmitFunction(context) {
        if (context.type !== context.UserEventType.CREATE)
            return;
        let customerRecord = context.newRecord;
        customerRecord.setValue({
            fieldId: 'comments',
            value: 'Please follow up with this customer!'
        });
    }
    return {
        beforeSubmit : myBeforeSubmitFunction
    };
}); 

        

Related Topics

General Notices