NetSuite Restlet Sample Program Exploits New Power

NetSuite’s new Restlet API is promising for developing external software applications that interact with NetSuite’s rich data and business functionality.  For example, imagine a large professional services organization that needs to track timesheets in the NetSuite Advanced Projects module.  Most professionals are highly mobile.  Instead of logging into NetSuite to do your timesheet via a web browser, imagine a small iPhone or Android app that allows you to quickly enter data.  Restlet is the key to make this happen without making a big investment.

This article illustrates how to connect up and create timesheet entries from a Linux Perl program.  This example should help bridge the key concepts to hook up to NetSuite through the new Restlet API.  The article assumes you have a good understanding of how to create and deploy scripts in the NetSuite environment.  The example consists of three files:

  1. restlet.js: NetSuite program to receive an insert time entry request.  It validates data and produces information in the execution log.
  2. timebill.pl: Perl program that reads timesheet entry data from timebill.csv file.  This file has hard coded NetSuite credential information.
  3. timebill.csv: tab delimited file in the following format: Date <tab> Client: Project <tab> Task <tab> Time in Decimal format <tab> Memo <newline – for new record>

Step 1: Deploy restlet.js

After enabling the NetSuite Restlet API create a new NetSuite Script as type Restlet.  Set it up with the following parameters:

  • Post Function: “CreateTimebills”
  • Deploy the function with a Log Level of “Debug”
  • Get the External URL.  In our case, it was “https://rest.netsuite.com/app/site/hosting/restlet.nl?script=73&deploy=1 “

Here is the restlet.js code:

function CreateTimebills(datain)
{
 var output = '';
 //nlapiLogExecution('DEBUG','createRecord',(typeof datain.timebill));
 var msg = validateTimeBills(datain);
 if (msg)
{
var err = new Object();
err.status = "failed";
err.message= msg;
return err;
}
var timebills = datain.timebill;
for (var timebillobject in timebills)
{
var timebill = timebills[timebillobject];
var trandate = timebill.trandate;
var customer = timebill.customer;
var casetaskevent = timebill.casetaskevent;
var hours = timebill.hours;
var memo = timebill.memo;
var timebill = nlapiCreateRecord( 'timebill' );
timebill.setFieldValue( 'trandate', trandate);
timebill.setFieldText( 'customer', customer);
timebill.setFieldText( 'casetaskevent', casetaskevent);
timebill.setFieldValue( 'memo', memo);
timebill.setFieldValue( 'hours', hours);
var timebillid = nlapiSubmitRecord( timebill );
nlapiLogExecution('DEBUG','Timebill ' + timebillid + ' successfully created',timebillid);
}
return;
}
function validateTimeBills(datain)
{
var timebills = datain.timebill;
var returnMessage = "";
for (var timebillobject in timebills)
{
var timebill = timebills[timebillobject];
var trandate = timebill.trandate;
var customer = timebill.customer;
var casetaskevent = timebill.casetaskevent;
var hours = timebill.hours;
var memo = timebill.memo;
if (isNaN(nlapiStringToDate(trandate)))
{
returnMessage += "Invalid date: '" + trandate + "'\n";
}
if (customer == '')
{
returnMessage += "Customer entry cannot be blank.'\n";
}
if (casetaskevent == '')
{
returnMessage += "Case Task Event entry cannot be blank.'\n";
}
if (hours == '')
{
returnMessage += "Hours cannot be blank.'\n";
}
if (memo == '')
{
returnMessage += "Memo cannot be blank.'\n";
}
}
if (returnMessage)
{
nlapiLogExecution('DEBUG','Validation Error',returnMessage);
return returnMessage;
}
}

Step 2: Edit timebill.pl

Edit timebill.pl with your specific NetSuite account ID, username, password, and role.  Here is the code:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Headers;
my $inputfilelocation = "timebill.csv";
my $scriptdeployment = "https://rest.netsuite.com/app/site/hosting/restlet.nl?script=73&deploy=1";
my $account = "TSTDRV365788";
my $email = "developer\@prolecto.com";
my $password = "<my-password-here>";
my $role = "15";
my $jsonString = &getJSONStringFromFile($inputfilelocation);
my $result = &submitRestlet($account, $email, $password, $role, "POST", $scriptdeployment , $jsonString);
print $result;
exit;
sub submitRestlet ()
{
(my $account, my $email, my $password, my $role, my $method, my $deploymentURL,my $request) = @_;
# define the HTTP header
my $objHeader = HTTP::Headers->new;
$objHeader->push_header('Authorization' => "NLAuth nlauth_account=$account, nlauth_email=$email, nlauth_signature=$password, nlauth_role=$role");
$objHeader->push_header('Content-Type' => 'application/json');
# make the call
my $objRequest = HTTP::Request->new(
$method,
$deploymentURL,
$objHeader,
$request
);
my $content = "";
# deal with the response
my $objUserAgent = LWP::UserAgent->new;
my $objResponse = $objUserAgent->request($objRequest);
if (!$objResponse->is_error)
{
return $objResponse->content;
}
else
{
return $objResponse->error_as_HTML;
}
}
sub getJSONStringFromFile()
{
(my $inputfilelocation) = @_;
open (FILE, $inputfilelocation);
my $output = "";
$output = qq{
"timebill":[
};
while (<FILE>)
{
chomp;
(my $trandate,   my $customer, my $casetaskevent, my $hours, my $memo) = split("\t");
$output .= "{";
$output .= qq{
"trandate":"$trandate",
"customer":"$customer",
"casetaskevent":"$casetaskevent",
"hours":"$hours",
"memo":"$memo"
};
$output .= "},";
}
close (FILE);
chop($output);
$output .= qq{
]
};
$output = "{$output}";
return $output;
}

Step 3: Create Timesheet Entry Data

In timebill.csv, create some data as input to insert as NetSuite timesheet entries.  Here are a couple sample entries.  Note the clients: projects, and tasks must match what is in already in NetSuite to work:

12/05/2011        SmartTech : Test Project 005      Test Task 005 (Task)     .75        Meeting on Specifications
12/05/2011        SmartTech : Test Project 005      Test Task 005 (Task)     1          Meeting with Management

The timebill.csv file should be placed in the same local directory as timebill.pl.

Step 4: Execute timebill.pl

Now you can run timebill.pl to see the program work.  If all goes right, it will take a few moments and return a code.  Go to the NetSuite script deployment log and see if the timesheet entries were created.  If so, check the timesheet system to find the new entries.

This article should help you kickstart your restlet system into a working prototype.  Stay tuned as we will be writing another article on how we are using restlets to create an Android / iOS app.

Social Share:
  • Facebook
  • LinkedIn
  • TwitThis
  • Ping.fm
  • E-mail this story to a friend!
  • Print this article!
| Tags: , , , , , , , , , , , , , , | Category: NetSuite, Technical | 2 Comments

One Comment

  1. Joe Son says:

    This is Awesome! I’ve been looking for examples on Restlets.

    Thank you!

One Trackback

  1. [...] NetSuite Restlet Sample Program Exploits New Power Posted in Automation, NetSuite SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail Similar posts [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


× two = 18