This is a follow-up on our previous post where we have announced the release of a process for migrating any script from SS1 to SS2 without re-implementing the logic (https://www.ketka.eu/2021/08/03/migrate-from-suitescript-1-0-to-2-x-in-the-blink-of-an-eye/).
The signature of the entry point functions changes between SuiteScript 1 and 2. Parameters object types of SS1 don't exist in SS2, and vice versa.
For this reason, when migrating an entry point function from SS1 to SS2 we need some adjustments in order to avoid re-writing any code.
Let's take for example a Before Load entry point function of a User Event script, and consider this simplest pseudo-code snippet:
function beforeLoad(type, form, request) {
// Do something, using parameters "type", "form" and "request".
// This can be any very complex set of operations and calls to sub-functions.
[...]
}
For using the same whole code without any change, and migrate the script to SS2 using our tools, the pseudo-code is adjusted as follows:
function beforeLoad(context) {
// Re-create the SS1 entry point parameters
var type = context.type;
var form = new nlobjForm({ss2Form: context.form});
var request = new nlobjRequest({ss2Request: context.request});
// Call the old (unchanged) entry point function
oldBeforeLoad(type, form, request);
}
function oldBeforeLoad(type, form, request) {
// Do something, using parameters "type", "form" and "request".
// This can be any very complex set of operations and calls to sub-functions.
[...]
}
Some explanations on how it practically works:
Important: we might need to use the SS2 "context" parameter somewhere else in our code, assuming that we want to develop it further after the migration.
In order to not change every JS function in the stack from the entry point till where we need to use it, the migrated code can access a global object where the "context" entry point parameter has been stored.
The available global objects vary depending on the script type and context.
In this article we have analyzed a Before Load example, the same logic can be applied for migrating any other entry point function and script type.