Sunday, February 10, 2019

Prevent User Event script running on CSV import

With the preference Run Server SuiteScript and Workflows enabled (Located in Setup > Import/Export > CSV Import Preferences) User Event scripts that are deployed on a record will also run when CSV imports for that record are performed.  To prevent this happening for a script or for part of a script use this if statement at the beginning of the code:

var currentContext = nlapiGetContext();

if(currentContext.getExecutionContext() != 'cvsimport') {

And at the end of the code close the if statement:

}

See simple example below:

function myBeforeSubmitFunction(type, form) {

    var currentContext = nlapiGetContext();

    if(currentContext.getExecutionContext() != 'cvsimport') {

        //Insert all code here to prevent it from running on CSV import

    }

}

More advanced example for preventing only part of a script from running:

function myBeforeSubmitFunction(type, form) {

    //Execute code here always

    var currentContext = nlapiGetContext();

    if(currentContext.getExecutionContext() != 'cvsimport') {

        //Code not for CSV import

    } else {

        //Code only run if CSV import is run

    }

    //Execute code here always

}

Note: After enabling the Run Server SuiteScript and Workflows preference it will also cause workflows to run on CSV imports.  Changing existing Workflows so that the Context condition excludes CSV Import might also be required.

No comments:

Post a Comment