Create a Backend Model
Backend models handle data transactions with NetSuite using SuiteScript.
The following code samples use SuiteScript 1.0 functions and objects only. These examples do not include SuiteScript 2.0 capabilities.
define(
'QuestionsAndAnswers.Model'
, ['SC.Model', 'Application']
, function (SCModel, Application)
{
'use strict';
return SCModel.extend({
name: 'QuestionsAndAnswers'
, validation: {
question: {required: true, msg: 'The question is required'}
}
search: function(page)
{
var filters = [
new nlobjSearchFilter('custrecord_q_and_a_answer', null, 'isnot', '')
]
, columns = [
new nlobjSearchColumn('custrecord_q_and_a_queston')
, new nlobjSearchColumn('custrecord_q_and_a_answer')
, new nlobjSearchColumn('created')
];
return Application.getPaginatedSearchResults({
record_type: 'customrecord_q_and_a'
, filters: filters
, columns: columns
, page: parseInt(page, 10) || 1
});
}
create: function(data)
{
this.validate(data);
var question = nlapiCreateRecord('customrecord_q_and_a');
question.setFieldValue('custrecord_q_and_a_queston', data.question);
var question_id = nlapiSubmitRecord(question);
return data;
}
});
});
This example demonstrates the following tasks that are common to all backend models. The following sections provide more detail about each task:
Define Dependencies
Like frontend modules, backend models use RequireJS to define dependencies. See Dependencies for more information.
All backend models must include the following dependencies:
-
SC.Model — defines the base class for backend models. All backend models should extend this module.
-
Application — provides useful methods, including server-side search helpers.
Extend from SC.Model
All backend models must return an object that is extended from SC.Model.
return SCModel.extend()
SC.Model defines the base model which defines core functionality, including validation.
Define the Validation Object
The validation
object defines any validation requirements for the JSON object.
validation: {
question: {required: true, msg: 'The question is required'}
}
The Backbone.Validation module uses this object to perform validation. Any validation errors are returned to the frontend application through the service. The SC.Model module includes Backbone.Validation as a dependency.
In the above example, the required
attribute indicates that the question
property of the JSON object must have a value. If this property is undefined, the backend model returns the message specified by the msg
property.
Define Methods to Handle HTTP Actions
A backend model must define a method to handle each of the HTTP actions supported by a service. These methods frequently use the SuiteScript API to handle data transactions with NetSuite. For example, the search
method in the above example shows how a backend model retrieves data from a NetSuite record:
search: function(page)
{
var filters = [
new nlobjSearchFilter('custrecord_q_and_a_answer', null, 'isnot', '')
]
, columns = [
new nlobjSearchColumn('custrecord_q_and_a_queston')
, new nlobjSearchColumn('custrecord_q_and_a_answer')
, new nlobjSearchColumn('created')
];
return Application.getPaginatedSearchResults({
record_type: 'customrecord_q_and_a'
, filters: filters
, columns: columns
, page: parseInt(page, 10) || 1
});
}
In this example, the search
method performs the following:
-
Creates an array containing search filters. Each search filter is defined by calling the
nlobjSearchFilter
method. Since this model is only retrieving data for one record, only one search filter is required. -
Creates an array for each column of the NetSuite record. In this example, each column corresponds to a property in the JSON object.
-
Calls the
getPaginatedSearchResults
of the Application module. This method returns a JSON object based on the parameters it receives. This JSON object contains all of thecustomrecord_q_and_a
records in NetSuite.