Define Methods for HTTP Actions in SuiteScript

Learn to define HTTP action methods in SuiteScript backend models for effective data transactions in NetSuite.

·2 min read·View Oracle Docs

TL;DR

A backend model in SuiteScript must define methods for handling HTTP actions, enabling effective data transactions with NetSuite's API. This article explains common practices using SuiteScript 1.0.

What is a Backend Model?

Backend models are JavaScript components that play a vital role in data interactions with NetSuite. They utilize SuiteScript functionalities to manage data operations, such as retrieving, creating, and validating records.

How to Define Methods for HTTP Actions?

Overview

When creating backend models, each HTTP action (such as GET, POST, or DELETE) needs a corresponding method. These methods manage data transactions through the SuiteScript API. The example below illustrates how to implement the search and create methods in a backend model using SuiteScript 1.0.

Example Code

Here’s an example of a backend model that defines methods to handle search and creation of records:

javascript
1"text-purple-400">define(
2 'QuestionsAndAnswers.Model'
3, ['SC.Model', 'Application']
4, "text-purple-400">function (SCModel, Application)
5{
6 'use strict';
7
8 "text-purple-400">return SCModel.extend({
9 name: 'QuestionsAndAnswers',
10 validation: {
11 question: {required: true, msg: 'The question is required'}
12 },
13
14 search: "text-purple-400">function(page)
15 {
16 "text-purple-400">var filters = [
17 "text-purple-400">new nlobjSearchFilter('custrecord_q_and_a_answer', null, 'isnot', '')
18 ],
19 columns = [
20 "text-purple-400">new nlobjSearchColumn('custrecord_q_and_a_queston'),
21 "text-purple-400">new nlobjSearchColumn('custrecord_q_and_a_answer'),
22 "text-purple-400">new nlobjSearchColumn('created')
23 ];
24
25 "text-purple-400">return Application.getPaginatedSearchResults({
26 record_type: 'customrecord_q_and_a',
27 filters: filters,
28 columns: columns,
29 page: parseInt(page, 10) || 1
30 });
31 },
32
33 create: "text-purple-400">function(data)
34 {
35 "text-purple-400">this.validate(data);
36
37 "text-purple-400">var question = nlapiCreateRecord('customrecord_q_and_a');
38 question.setFieldValue('custrecord_q_and_a_queston', data.question);
39 "text-purple-400">var question_id = nlapiSubmitRecord(question);
40
41 "text-purple-400">return data;
42 }
43 });
44});

Breakdown of the Example

  • Search Method: This method is responsible for retrieving records. It sets up search filters and columns for querying data from NetSuite records.

    • Filters: Use nlobjSearchFilter to define conditions for the search. In this case, it filters for answers that are not empty.
    • Columns: Define the columns to retrieve using nlobjSearchColumn.
    • Pagination: The method fetches paginated results to handle large datasets efficiently.
  • Create Method: This creates a new record in NetSuite. It validates the incoming data, prepares a new record, inserts the data, and submits the record.

Conclusion

Defining methods to handle HTTP actions in backend models is essential for effective data management in SuiteScript applications. By implementing these methods, developers can create robust solutions that interact seamlessly with NetSuite’s data.

Key Takeaways

  • Every backend model must define methods for handling HTTP methods.
  • The search and create methods are fundamental for data manipulation.
  • Proper validation ensures data integrity before submitting records.

Source: This article is based on Oracle's official NetSuite documentation.

Frequently Asked Questions (4)

Does defining HTTP action methods in SuiteScript apply to both SuiteScript 1.0 and 2.0?
The article specifically provides examples and guidance using SuiteScript 1.0. It does not mention SuiteScript 2.0.
Do I need to validate data before creating a record in NetSuite using SuiteScript?
Yes, the create method example includes a validation step to ensure data integrity before creating the record in NetSuite.
How does pagination work in the search method for SuiteScript backend models?
Pagination in the search method is managed by fetching a specific page of results, allowing efficient handling of large datasets. The page number is specified in the method call.
What are the functions of filters and columns in the search method using SuiteScript?
Filters define the search conditions using nlobjSearchFilter, and columns specify which data fields to retrieve using nlobjSearchColumn.
Source: Define Methods to Handle HTTP Actions Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in SuiteScript

View all SuiteScript articles →