简体   繁体   中英

NetSuite SuiteScript - Using Classes

I'm very much a newbie to SuiteScript and not much more than that to Javascript.

I have a single file which contains all my functions and so forth which I upload to NetSuite and deploy - all good. But I want to create and use a Class, creating instances of it from the file I upload.

So how do I do it? Does the Class declaration need to be in another Javascript file? Can I do it all in one file? If I can do it all in one file, what's the syntax? I can't get it to work in one file; it always throws an error. If I can put it all in another file, what's the syntax in that file? And then how do I include it in the first file?

Here's a shortened version of my Javascript file:

define(['N/search'],
       
function (search)
{
    function fieldChanged(scriptContext)
    {
        // Code here where I create and run a search. I want to put each result
        // from the search into an instance of the class I want to create and
        // store all of those instances in an array.
    }

    return {
        fieldChanged: fieldChanged
    };
});

The class declaration, I know, looks like this:

class Result
{
    constructor(name) 
    {
        this.name = name;
    }
}

But where do I put that declaration?

Sorry if this is a really dumb question.

https://3en.cloud/insight/2018/1/26/using-es6-with-suitescript-20

You may find this link helpful.

The best idea is to wrap your class in a define([], function() {}) , returning the class. Then include that file in your SuiteScript file, like so define(['N/search', './myClass.js'], function(search, myClass) {}) .

Bottom line, you need typescript and make sure you use this tsconfig below. We use this at work religiously

tsconfig.json

{
  "compilerOptions": {
    "module": "amd",
    "target": "es5",
    "sourceMap": false,
    "removeComments": false,
    "experimentalDecorators": true,
    "outDir": "src/FileCabinet/SuiteScripts/dist/"
  },
  "compileOnSave": true,
  "exclude": [
    "node_modules"
  ]
}

example Class

export class MyNetsuiteClass extends NetsuiteActor {
​
    constructor(modules: INetsuiteModules) {
        super(modules);
    }
​
    GetAccountID(): string {
        try {
            return this.modules.runtime.accountId;
        } catch (e) {
            throw e.details || e.message || e.toString();
        }
    }
}

class usage in SS2.0

// declare the modules you want to inject
var modules = {record: record, search: search, runtime: runtime};
​
// fianlly, instantiate your class with all 3 required params
var myNetsuiteClass = new myCustomModule.MyNetsuiteClass(modules);
var accountId = myNetsuiteClass.GetAccountID();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM