Create a Backend Model

Backend models handle data transactions with NetSuite using SuiteScript.

Note:

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:

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:

Related Topics

Services and Backend Models in Custom Modules
Define a JSON Object to Represent a NetSuite Record
Create a Service to Handle HTTP Requests

General Notices