RESTlet Scripts as XML Definitions

RESTlets are generally used to transfer data from another system into NetSuite or to extract data from NetSuite. RESTlets can also be used in combination with other scripts to customize the behavior of a page in NetSuite. RESTlets are SuiteScripts that can be called by an external application or from NetSuite. When called, the RESTlet executes, and in some cases returns a value to the calling application.

The RESTlet script object can be created in a SuiteCloud project for SuiteCloud Development Framework (SDF) and deployed to a target NetSuite account. RESTlet scripts can also be imported from a target NetSuite account into a SuiteCloud project. For information about importing SDF custom objects, files, and scripts from a target NetSuite account into a SuiteCloud project, see Account Component Imports to SuiteCloud Projects.

For information about working with a RESTlet script object from your SuiteCloud project, see the following topics:

For information about working with RESTlet scripts, see the following topics:

Components of a Script Object

There are three components that are required for the script object:

  1. The Scriptid Attribute: Provide a readable scriptid attribute for the script record and scriptdeployment structure by adding an underscore to the default value followed by a meaningful name for the object. The script record’s scriptid attribute must start with a customscript_ prefix and each scriptdeployment structure’s scriptid attribute must start with a customdeploy_ prefix.

  2. The Script Record: Represented in XML by the object. The object contains several elements that define it, including the object name and a reference to the location of the script file.

  3. The Script Deployment Record: Represented in XML by the scriptdeployments structure. A script object may contain multiple script deployments.

Components of a RESTlet Script Object

The following components are required for a RESTlet script object to work:

  • The Script Record: Represented in XML by the RESTlet script object. The object contains several elements that define it, including the name of the RESTlet script object, and a reference to the location of the RESTlet script file.

    The following example shows a RESTlet script object named Restlet Example that references a RESTlet script file named RestletExample.js and has the script id customscript_restletexample.

                    <Restlet scriptid="customscript_restletexample">
        <isinactive>T</isinactive>
        <name>Restlet Example</name>
        <notifyowner>T</notifyowner>
        <scriptfile>[/SuiteScripts/RestletExample.js]</scriptfile>
        <scriptdeployments>
            <scriptdeployment scriptid="customdeploy_restletexample">
                <isdeployed>T</isdeployed>
                <loglevel>DEBUG</loglevel>
                <status>TESTING</status>
                <title>Restlet Example</title>
            </scriptdeployment>
        </scriptdeployments>
    </Restlet> 
    
                  
  • The Script Deployment Record: Represented in XML by the scriptdeployments structure. It is contained within the RESTlet script object where you can define certain values such as the title and status for your script deployments.

    The following example shows a scriptdeployments structure with the title RestletExample and a status value set to TESTING.

                    <Restlet scriptid="customscript_Restletexample">
        <isinactive>T</isinactive>
        <name>RESTlet Example</name>
        <notifyowner>T</notifyowner>
        <scriptfile>[/SuiteScripts/RestletExample.js]</scriptfile>
        <scriptdeployments>
            <scriptdeployment scriptid="customdeploy_restletexample">
                <isdeployed>T</isdeployed>
                <loglevel>DEBUG</loglevel>
                <status>TESTING</status>
                <title>Restlet Example</title>
            </scriptdeployment>
        </scriptdeployments>
    </Restlet> 
    
                  

For more information about:

Setting Values for a Script Object

Each script object has a set of values that are required to successfully validate its script record and script deployment record.

The following elements are required to successfully validate your script record:

  • name —Provide a meaningful name for your script record.

  • scriptfile —Reference the appropriate script file. This must be a JavaScript file (.js).

The following element is required to successfully validate your script deployments:

  • status —Provide a value for the status element. The default value is TESTING. The only values accepted for all scripts except scheduled scripts are:

    • RELEASED: The script will run in the accounts of all specified audience members.

    • TESTING: The script will execute for the script owner and specified audience.

    The remaining possible values for the status element are:

    • COMPLETED

    • INPROGRESS

    • INQUEUE

    • NOTSCHEDULED

    • SCHEDULED

Setting Values for a RESTlet Script Record

Provide a readable scriptid attribute for the RESTlet script object and scriptdeployment structure by adding an underscore to the default value, followed by a meaningful name for the object. The RESTlet script object’s scriptid attribute must start with a “customscript_” prefix and each scriptdeployment structure’s scriptid attribute must start with a “customdeploy_” prefix.

The following elements are required to successfully validate your RESTlet script object:

  • name —Provide a meaningful name for your script record.

  • scriptfile —Reference the appropriate script file. This must be a JavaScript file (.js).

The following elements are required to successfully validate your script deployments:

  • title —Provide a meaningful title for your script deployment record.

  • status —Provide a value for the status element. The default value is TESTING. Possible values are:

    • RELEASED: The script will run in the accounts of all specified audience members.

    • TESTING: The script will execute for the script owner and specified audience.

Example of a RESTlet Script Object

The following example shows a RESTlet script object named Restlet Example that references a RESTlet script file named RestletExample.js and has the script id customscript_restletexample.

            <Restlet scriptid="customscript_restletexample">
    <isinactive>T</isinactive>
    <name>Restlet Example</name>
    <notifyowner>T</notifyowner>
    <scriptfile>[/SuiteScripts/RestletExample.js]</scriptfile>
    <scriptdeployments>
        <scriptdeployment scriptid="customdeploy_restletexample">
            <isdeployed>T</isdeployed>
            <loglevel>DEBUG</loglevel>
            <status>TESTING</status>
            <title>Restlet Example</title>
        </scriptdeployment>
    </scriptdeployments>
</Restlet> 

          

The following example shows a RESTlet script file named RestletExample.js that is referenced in the RESTlet script object. This is an example of a RESTlet script that can create multiple contact records when it has been called. For more examples of RESTlet scripts, see SuiteScript 2.x RESTlet Script and Request Examples.

            /**
 * @NApiVersion 2.x
 * @NScriptType restlet
 */
define([ 'N/record' ], function(record) {
   return {
      post : function(restletBody) {
         var restletData = restletBody.data;
         for ( var contact in restletData) {
            var objRecord = record.create({
               type : record.Type.CONTACT,
               isDynamic : true
            });
            var contactData = restletData[contact];
            for ( var key in contactData) {
               if (contactData.hasOwnProperty(key)) {
                  objRecord.setValue({
                     fieldId : key,
                     value : contactData[key]
                  });
               }
            }
            var recordId = objRecord.save({
               enableSourcing : false,
               ignoreMandatoryFields : false
            });
            log.debug(recordId);
         }
      }
   }
}); 

          

Related Topics

General Notices