SDF Installation SuiteScript File Example
The following code sample is the SuiteCloud Development Framework (SDF) installation script that is saved in the File Cabinet folder and named migrate.js.
When the migrate.js
SuiteScript file is triggered by deployment of SuiteApp v4.1 to an account with SuiteApp v4.0, it creates a “My New Field” transaction body field on the custom transaction record named “My Vendor Payment” and sets a default value for instances in the account.
These are the tasks that occur when migrate.js
is executed:
-
Accesses the
fromVersion
andtoVersion
parameters to get the current SuiteApp version installed to the account and the SuiteApp version to be installed when the SDF SuiteApp is deployed. If the account version is 4.0.0, the migrate function will run. -
The migrate function sets up the account for the 4.1.0 version by updating transaction records with a new custom transaction body field and setting it to a default value based on options from an existing custom transaction type.
-
The migrate function runs the following:
-
Creates an ad-hoc search that finds instances of the My Vendor Payment record. Uses an each function to iterate through the results.
-
During the iteration, each My Vendor Payment instance is loaded and the value of My New Field (
custbody_install_script
) is set to the value of thecustscript
1
parameter (also know as script custom field). That parameter isval_a
of thecustomlist_transaction_field
list. My New Field takes values from the same list (customlist_transaction_field
).
-
/**
* @NApiVersion 2.x
* @NScriptType SDFInstallationScript
*/
define(['N/record', 'N/search', 'N/runtime'], function(record, search, runtime) {
function migrate(context) {
if (context.fromVersion === '4.0.0' && context.toVersion === '4.1.0') {
var tSearch = search.create({
type: search.Type.TRANSACTION,
columns: [{
name: 'internalid'
}],
filters: [{
name: 'recordtype',
operator: search.Operator.IS,
values: ['customtransaction_install_script']
}]
});
tSearch.run().each(function(result) {
var tranInternalId = result.getValue({name: 'internalid'});
var tran = record.load({
type: 'customtransaction_install_script',
id: tranInternalId
});
var fieldValue = runtime.getCurrentScript().getParameter({name: 'custscript1'});
tran.setValue({
fieldId: 'custbody_install_script',
value: fieldValue
});
tran.save();
})
}
}
return {
run: function run(context) {
migrate(context);
}
};
});