Classes

Note:

If your script contains SuiteScript 2.1 syntax that includes classes and will be included in a bundle, the @NModuleScope JSDoc tag must be set to SameAccount or TargetAccount.

You can create classes to represent common structures:

          /**
 * @NApiVersion 2.1
 */
define(['N/currency', 'N/query'],
    (currency, query) => {
        // Define a class called Transaction
        class Transaction {
            constructor(amount, currency, date, id) {
                this.amount = amount;
                this.currency = currency;
                this.date = date;
                this.id = id;
            }

            // Create a helper function to check if the transaction exists
            exists() {
                return this.id && query.create({
                    type: query.Type.TRANSACTION,
                    condition: query.createCondition({
                        fieldId: 'id',
                        operator: query.Operator.IS,
                        values: this.id
                    })
                 }).run().results.length > 0;
            }

            // Create a helper function to convert transaction amount to a desired currency
            toCurrency(targetCurrency) {
                return this.amount * currency.exchangeRate({
                    date: this.date,
                    source: this.currency,
                    target: targetCurrency
                });
            }
        }

        // Define Sale and Purchase classes that inherits helper methods from the Transaction class
        class Sale extends Transaction {
            constructor(amount, currency, date, customer, id) {
                super(amount, currency, date, id)
                this.customer = customer;
            }
        }
 
        class Purchase extends Transaction {
            constructor(amount, currency, date, vendor, id) {
                super(amount, currency, date, id)
                this.vendor = vendor;
            }
        }

        // Use case: A user attempts to create a Sale or Purchase object from a search or query
        // result set. The user can use the createSale() and createPurchase() functions
        // to create the desired objects.
        return {
            createSale(amount, currency, date, customer, id) {
                return new Sale(amount, currency, date, customer, id);
            },
            createPurchase(amount, currency, date, vendor, id) {
                return new Purchase(amount, currency, date, vendor, id);
            }
        }
    }
); 

        

Related Topics

General Notices