Call NetSuite RESTlets Example | MuleSoft Documentation
# Call NetSuite RESTlets Example
This example shows how to:
  * Create a script that calls NetSuite RESTlets that are deployed in NetSuite instances to create, retrieve, and delete a customer record
  * Deploy the RESTlet script
  * Configure the application’s properties to connect to NetSuite and deploy the app


## Before You Begin
To follow this example, you must have:
  * Java 8, 11, or 17
  * Anypoint Studio 7.0.x
  * Mule 4.2.1 or later
  * DataWeave 2.0
  * Access to a NetSuite instance with credentials in the `mule-app.properties` file


## Create and Deploy a Script as a RESTlet
Create and then deploy a script that creates, retrieves, and deletes a customer record:
  1. Create a sample JavaScript script:
```
// Get a standard NetSuite record
function getRecord(datain)
{
  return nlapiLoadRecord(datain.recordtype, datain.id);
  // for example, recordtype="customer", id="769"
}
// Create a standard NetSuite record
function createRecord(datain)
{
  var err = new Object();
  // Validate if mandatory record type is set in the request
  if (!datain.recordtype)
  {
    err.status = "failed";
    err.message= "missing recordtype";
    return err;
  }
  var record = nlapiCreateRecord(datain.recordtype);
  for (var fieldname in datain)
  {
   if (datain.hasOwnProperty(fieldname))
   {
    if (fieldname != 'recordtype' && fieldname != 'id')
    {
     var value = datain[fieldname];
      if (value && typeof value != 'object')
      // ignore other type of parameters
      {
       record.setFieldValue(fieldname, value);
      }
    }
   }
  }
  var recordId = nlapiSubmitRecord(record);
  nlapiLogExecution('DEBUG','id='+recordId);
  var nlobj = nlapiLoadRecord(datain.recordtype,recordId);
  return nlobj;
}
// Delete a standard NetSuite record
function deleteRecord(datain)
{
 nlapiDeleteRecord(datain.recordtype, datain.id);
 // for example: recordtype="customer", id="769"
}
```

  2. Enable SuiteScript and NetSuite Web Services in your account:
    1. Log in to NetSuite.
    2. Click **Set Up > Company > Enable Features > SuiteCloud**:
  3. Upload the file you previously created:
    1. Go to **Customization > Scripting > Scripts > New**.
    2. Select the script file, click **Create Script Record** , and select **RESTlet** :
  4. Complete the form using the content of the script you uploaded, and deploy the script:
  5. Select your audience to view the following page:
  6. Write down the script and deploy numbers that appear in the external URL, because you need them to call the RESTlet later.


## Create a Mule Project
In Studio, create a new Mule project and configure the credentials, deployed script, and deploy IDs to connect to NetSuite:
  1. In Studio, select **File > New > Mule Project**.
  2. Enter a name for your Mule project and click **Finish**.
  3. In **Package Explorer** , open the `src/main/resources/mule-app.properties` file that is located in the Studio project you created.
  4. Configure the following values:
```
netsuite.email=
netsuite.password=
netsuite.account=
netsuite.roleId=
netsuite.applicationId=
netsuite.subsidiary=
netsuite.script=
netsuite.deploy=
```

The `netsuite.script` and `netsuite.deploy` values are the values you wrote down in the last step of Create and Deploy a Script as a RESTlet.


## Add the Connector to Your Mule Project
Add NetSuite Connector to your Mule project to automatically populate the XML code with the connector’s namespace and schema location and add the required dependencies to the project’s `pom.xml` file:
  1. In the **Mule Palette** view, click **(X) Search in Exchange**.
  2. In **Add Modules to Project** , type `netsuite` in the search field.
  3. Click the connector name in **Available modules**.
  4. Click **Add**.
  5. Click **Finish**.
The `netsuite.script` and `netsuite.deploy` values are the values you wrote down in the last step of Create and Deploy a Script as a RESTlet.


## Add the Connector to Your Mule Project
Add NetSuite Connector to your Mule project to automatically populate the XML code with the connector’s namespace and schema location and add the required dependencies to the project’s `pom.xml` file:
  1. In the **Mule Palette** view, click **(X) Search in Exchange**.
  2. In **Add Modules to Project** , type `netsuite` in the search field.
  3. Click the connector name in **Available modules**.
  4. Click **Add**.
  5. Click **Finish**.


## Create the App Flow
  1. In the Studio canvas, click **Configuration XML**.
  2. Delete all the contents after the line `<?xml version="1.0" encoding="UTF-8"?>`.
  3. Copy and paste the following XML after the line `<?xml version="1.0" encoding="UTF-8"?>`:
```
<mule xmlns:netsuite-restlet="http://www.mulesoft.org/schema/mule/netsuite-restlet"
   xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
   xmlns:http="http://www.mulesoft.org/schema/mule/http"
   xmlns="http://www.mulesoft.org/schema/mule/core"
   xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/netsuite-restlet http://www.mulesoft.org/schema/mule/netsuite-restlet/current/mule-netsuite-restlet.xsd">
	<configuration-properties file="mule-app.properties" doc:name="Configuration properties"/>
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<netsuite-restlet:rest-config name="NetSuite_Rest_config" doc:name="NetSuite Rest config" >
		<netsuite-restlet:restlet-token-connection
				consumerKey="${netsuite.consumerKey}"
				consumerSecret="${netsuite.consumerSecret}"
				tokenId="${netsuite.tokenId}"
				tokenSecret="${netsuite.tokenSecret}"
				account="${netsuite.account}"
				readTimeout="${netsuite.readTimeout}"
				connectionTimeout="${netsuite.connectTimeout}"/>
	</netsuite-restlet:rest-config>
	<flow name="html-form-flow">
    <http:listener config-ref="HTTP_Listener_config" path="/" doc:name="/"/>
    <parse-template location="form.html" doc:name="Parse Template"/>
  </flow>
  <flow name="restletGet">
    <http:listener config-ref="HTTP_Listener_config" path="/get" doc:name="/get"/>
    <ee:transform doc:name="Transform GET Input" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
{
  "id": attributes.queryParams.id,
  "recordtype": attributes.queryParams.recordtype
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
    <netsuite-restlet:call-restlet-get config-ref="NetSuite_Rest_config" script="${netsuite.script}" deploy="${netsuite.deploy}" doc:name="Call RESTlet (GET)" />
    <ee:transform doc:name="to JSON" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
    <logger level="INFO" doc:name="Logger"/>
  </flow>
  <flow name="restletPost">
    <http:listener config-ref="HTTP_Listener_config" path="/post" doc:name="/post"/>
    <ee:transform doc:name="Transform POST Input" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
    <netsuite-restlet:call-restlet-post config-ref="NetSuite_Rest_config" deploy="${netsuite.deploy}" script="${netsuite.script}" doc:name="NetSuite RESTlet (POST)"/>
    <ee:transform doc:name="to JSON" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
    <logger level="INFO" doc:name="Logger"/>
  </flow>
  <flow name="restletDelete">
    <http:listener config-ref="HTTP_Listener_config" path="/delete" doc:name="/delete"/>
    <ee:transform doc:name="Transform DELETE Input" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
{
  "id": attributes.queryParams.id,
  "recordtype": attributes.queryParams.'recordtype'
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
    <netsuite-restlet:call-restlet-delete config-ref="NetSuite_Rest_config" deploy="${netsuite.deploy}" script="${netsuite.script}" doc:name="NetSuite RESTlet (DELETE)"/>
    <set-payload value="Record deleted successfully" doc:name="Set Payload"/>
    <logger level="INFO" doc:name="Logger"/>
  </flow>
	</mule>
```

  4. Save the project.


### About the Flows
  1. The `html-form` flow renders the HTML form with a `parseTemplate` component:
  2. The `restletGet` flow calls the GET function of a RESTlet:
  3. The `restletPost` flow calls the POST function of a RESTlet:
  4. The `restletDelete` flow calls the DELETE function of a RESTlet:


## Run the App
```
Run, deploy, and verify the app:
```

  1. Click **Global Elements** at the base of the project’s canvas.
  2. In **Global Configuration Elements** , select **NetSuite Rest config** and click **Edit**.
  3. Click **Test Connection** to ensure there is connectivity with the sandbox.
A success message appears:
  4. Click the project name in **Package Explorer** and click **Run > Run As > Mule Application**. In the console, look for the message `Mule is up and kicking` to verify that the application started successfully.
  5. Open a browser and access the URL `http://localhost:8081`. You can see that the application deployed:


## See Also
  * Introduction to Anypoint Connectors
  * NetSuite Connector Overview
  * NetSuite Connector Reference
  * MuleSoft Help Center