Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions suitecloud-legacy-solutions-catalog/rest-file-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# dependencies
**/node_modules/**

# ides
**/.idea/**

# test coverage folder
**/coverage/**

# suitecloud
project.json
35 changes: 35 additions & 0 deletions suitecloud-legacy-solutions-catalog/rest-file-api/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Copyright (c) 2021 Oracle and/or its affiliates.

The Universal Permissive License (UPL), Version 1.0

Subject to the condition set forth below, permission is hereby granted to any
person obtaining a copy of this software, associated documentation and/or data
(collectively the "Software"), free of charge and under any and all copyright
rights in the Software, and any and all patent rights owned or freely
licensable by each licensor hereunder covering either (i) the unmodified
Software as contributed to or provided by such licensor, or (ii) the Larger
Works (as defined below), to deal in both

(a) the Software, and
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
one is included with the Software (each a “Larger Work” to which the Software
is contributed by such licensors),

without restriction, including without limitation the rights to copy, create
derivative works of, display, perform, and distribute the Software and make,
use, sell, offer for sale, import, export, have made, and have sold the
Software and the Larger Work(s), and to sublicense the foregoing rights on
either these or other terms.

This license is subject to the following condition:
The above copyright notice and either this complete permission notice or at
a minimum a reference to the UPL must be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions suitecloud-legacy-solutions-catalog/rest-file-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<p align="center"><a href="#"><img width="250" src="oracle_netsuite_logo.png"></a></p>

# Summary
This SuiteScript project uses a Restlet script to create a new file to be attached to vendor records after a purchase order is saved. Data to post to the newly created file will be defined in a client script.

# Key Concepts
This sample demonstrates the following concepts:

* **SuiteScript 2.1** – This SuiteScript version is the latest available version and includes new language features that are not part of SuiteScript 2.0. This sample uses some SuiteScript 2.1 features, such as arrow functions. For more information, see [SuiteScript 2.1](https://system.netsuite.com/app/help/helpcenter.nl?fid=chapter_156042690639.html).

* **Client script type** – Client scripts are scripts that are executed by predefined event triggers in the client browser. They can validate user-entered data and auto-populate fields or sublists at various form events. For more information, see [SuiteScript 2.x Client Script Type](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4387798404.html).

* **Restlet script type** - A RESTlet is a SuiteScript that you make available for other applications to call. It can be called from either an external application or from another script within NetSuite. A RESTlet executes only when it is called and in some cases, returns a value to the calling application.

RESTlets can be useful when you want to bring data into NetSuite from another system, or if you want to extract data from NetSuite. RESTlets can also be used in combination with other scripts to customize the behavior of a page within NetSuite. For more information, see [SuiteScript 2.x RESTlet Script Type](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4387799403.html).

# SuiteScript Version
This sample uses SuiteScript 2.1. For more information about this SuiteScript version, see [SuiteScript 2.1](https://system.netsuite.com/app/help/helpcenter.nl?fid=chapter_156042690639.html).

# Loaded Modules
This sample loads and uses the following SuiteScript modules:

* **N/https** – To communicate between SuiteScript scripts and RESTlets. For more information, see [N/https](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4418229131.html).
* **N/url** - To determine URL navigation paths within NetSuite and format URL strings. For more information, see [N/url](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4358552918.html).
* **N/file** - To work with files within NetSuite. You can use this module to upload files to the NetSuite File Cabinet, as well as send files as attachments without uploading them to the File Cabinet. For more information, see [N/file Module](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4205693274.html).
* **N/record** - To work with NetSuite records. You can use this module to create, delete, copy, load, or make changes to a record. For more information, see [N/record Module](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4267255811.html).
* **N/log** – To log errors that occur during execution. For more information, see [N/log Module](https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4574548135.html).

# Script Type and Entry Points
This sample uses a user event script type with the following entry points:

* `post` – Defines the function that is executed when a post request is sent to a RESTlet. Typically used for posting, such as creating a NetSuite record.
* `saveRecord` -
Defines the function that is executed when a record is saved (after the submit button is pressed but before the form is submitted).

# Test the Solution
1. Go to Transactions > Purchases > Enter Purchase Orders.
2. Fill out the required fields for the purchase order.
3. Click Save.

When the record is saved, a new file will be created and attached to the vendor record of the purchase order. To view the attached file on the vendor record:

1. Go to Lists > Relationships > Vendors.
2. Click View on the vendor record used on the purchase order.
3. Click the Communication tab.
4. Under the Files subtab, confirm the file was attached to the record.

<p align="center"><a href="#"><img width="250" src="vendor-record.png"></a></p>

## [License](./LICENSE.txt)
Copyright (c) 2021 Oracle and/or its affiliates The Universal Permissive License (UPL), Version 1.0.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import script from '../src/FileCabinet/SuiteScripts/cs_call_restlet'

import url from 'N/url'
import https from 'N/https'
import CurrentRecord from 'N/currentRecord/instance'

jest.mock('N/url')
jest.mock('N/currentRecord/instance')
jest.mock('N/record')
jest.mock('N/https')

beforeEach(() => {
jest.clearAllMocks()
})

CurrentRecord.Type = {
VENDOR: 'VENDOR'
}

describe('client script test', () => {
it('should test saveRecord function', async () => {
// given
const context = {
currentRecord: CurrentRecord
}
url.resolveScript.mockReturnValue(
'/app/site/hosting/restlet.nl?script=158&deploy=1')

// when
await script.saveRecord(context)

// then
expect(CurrentRecord.getValue).toHaveBeenCalled()
expect(https.post.promise).toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import script from '../src/FileCabinet/SuiteScripts/rs_file_api'

import file from 'N/file'
import File from 'N/file/instance'

import record from 'N/record'

import log from 'N/log'

jest.mock('N/file')
jest.mock('N/file/instance')
jest.mock('N/record')
jest.mock('N/log')

beforeEach(() => {
jest.clearAllMocks()
})

describe('restlet script test', () => {
it('should test for errors in post', () => {
// given
const requestBody = {
files: [File]
}

File.name = 'filename1'

// when
script.post(requestBody)

// then
expect(log.error).toHaveBeenCalled()
})
it('should test post function', () => {
// given
const requestBody = {
files: [File],
recordType: 'VENDOR',
internalId: 889
}

File.name = 'filename1'
File.folder = 'foldername1'
File.fileType = 'PLAINTEXT'
File.contents = 'hello world'

file.create.mockReturnValue(File)
File.save.mockReturnValue(123)

// when
script.post(requestBody)

// then
expect(record.attach).toHaveBeenCalledWith({
record: {
type: 'file',
id: 123
},
to: {
type: 'VENDOR',
id: 889
}
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { defineConfig } from 'eslint/config'
import globals from 'globals'
import jsdoc from 'eslint-plugin-jsdoc'
import neostandard from 'neostandard'

export default defineConfig([
...neostandard(),
{
files: ['**/*.js'],
plugins: { jsdoc },
languageOptions: {
globals: {
...globals.browser,
...globals.jest,
...globals.amd,
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
ecmaVersion: 2018,
sourceType: 'script',
},

rules: {
'prefer-const': 'error',
'jsdoc/require-jsdoc': 'warn',
'no-var': 'error',
'no-duplicate-imports': 'error',
'no-label-var': 'error',
'no-unsafe-finally': 'error',
'@stylistic/indent': ['warn', 2],
'no-unused-vars': 'error',
'@stylistic/quotes': ['warn', 'single'],
'no-undef-init': 'error',
'@stylistic/no-trailing-spaces': 'off',
'@stylistic/max-len': [
'error', {
code: 80,
tabWidth: 4,
}],
'prefer-arrow-callback': 'error',
'max-lines': ['error', 500],
'@stylistic/function-call-argument-newline': ['error', 'never'],
'@stylistic/array-element-newline': ['error', 'never'],
'vars-on-top': 'error',
},
}])
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const SuiteCloudJestConfiguration = require('@oracle/suitecloud-unit-testing/jest-configuration/SuiteCloudJestConfiguration')
const cliConfig = require('./suitecloud.config')

module.exports = SuiteCloudJestConfiguration.build({
projectFolder: cliConfig.defaultProjectFolder,
projectType: SuiteCloudJestConfiguration.ProjectType.ACP,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"typeAcquisition": {
"include": ["jest"]
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading