Salesforce and NetSuite are the industry’s leading enterprise SAAS providers – Salesforce for CRM and NetSuite for ERP or “back-office” solutions. With the prevalent usage of the two platforms, connecting these lets you manage your business and underlying processes more effectively – creating a seamless lead-to-cash process.
There are two primary ways to integrate these platforms:
- By using third party integration like Boomi, Celigo
- By using the NetSuite API, to integrate both CRMs
Which is the most suitable option?
Using a pre-built third party integration is a convenient option but,
- It is not flexible
- It is expensive
Restlet integration on the contrary supports customization, and is also cost-effective.
How to integrate NetSuite & Salesforce using Restlet?
The use case here is to create customer record in NetSuite through Restlet, when the new account is already created in Salesforce – moving all the details like address, contact, etc. from Salesforce to Customer Record in NetSuite.
- Add remote site setting in Salesforce to add NetSuite remote Url.
- Create restlet script in NetSuite as below:
- After creating a restlet script, Go to Document -> Files -> SuiteScripts
Create js as below to add the new customer in NetSuite:
// 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);
}
}
}
}
nlapiLogExecution(‘DEBUG’,’zip=’+datain.zip);
record.selectNewLineItem(‘addressbook’);
record.setCurrentLineItemValue(‘addressbook’,’city’, datain.city);
record.setCurrentLineItemValue(‘addressbook’,’zip’, datain.zip);
record.setCurrentLineItemValue(‘addressbook’, ‘country’, ‘US’);
record.setCurrentLineItemValue(‘addressbook’,’label’,’billing address’);
record.commitLineItem(‘addressbook’);
record.selectNewLineItem(‘contact’);
record.setCurrentLineItemValue(‘contact’,’contactrole’,’-10′);
record.setCurrentLineItemValue(‘contact’, ‘firstname’, datain.firstname);
record.commitLineItem(‘contact’);
var recordId = nlapiSubmitRecord(record);
nlapiLogExecution(‘DEBUG’,’id=’+recordId);
var nlobj = nlapiLoadRecord(datain.recordtype,recordId);
return nlobj;
}
Select the folder to add script. Click on “Add File” button to add new script.[/vc_column_text][vc_column_text]
- Click on Customization -> Scripting -> Scripts -> New. Select type “RESTlet”. Add name and script created in the previous step, and click “Save”. On deploying this script, a url will be generated that is used as endpoint in Salesforce
- Third step is to add authentication request, end point and data transfer using the rest call in Salesforce. Please note that the endpoint will be different for different scripts added in NetSuite.
- public static void createNSCustomerRecord(account acc){
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
String endpoint = currNetSuiteSettings.NSEndpointCreateCustomer__c;
req.setEndpoint(endpoint);
string custId;
//Set Method and Endpoint and Body
req.setMethod(‘POST’);
req.setTimeout(119990);
Http http = new Http();
String responseBody;
req.setHeader(‘Content-Type’,’application/json’);string NetSuiteProductionAccount = currNetSuiteSettings.NetSuiteProductionAccount__c;
string NetSuiteProductionUserName = currNetSuiteSettings.NetSuiteProductionUserName__c;
string NetSuiteProductionPassword = currNetSuiteSettings.NetSuiteProductionPassword__c;
String authorizationheader = ‘NLAuth nlauth_account=’+NetSuiteProductionAccount+’,
nlauth_email=’+NetSuiteProductionUserName+’,nlauth_signature=’;
nlauth_email= nlauth_email+NetSuiteProductionPassword;//Construct Authorization and Content header
req.setHeader(‘Authorization’, authorizationHeader);string recordType = ‘customer’;
string accountId = ”;
accountId = acc.Id;
system.debug(‘accountId===>’+accountId);
//you need the minimum field pattern for whatever entity you are posting, refer to their API guide
req.setBody
(‘{“recordtype”:”‘+recordType+'”,”entityid”:”‘+acc.name+'”,”accountId”:”‘+accountId+'”);
system.debug(‘setBody’+req);try {
res = http.send(req);
responseBody = res.getBody();
system.debug(‘responseBody’+responseBody
JSONParser parser = JSON.createParser(responseBody );
while (parser.nextToken() != null) {
System.debug(‘Current token: ‘ + parser.getCurrentToken());
// Advance to the next value.
parser.nextValue();
// Get the field name for the current value.
String fieldName = parser.getCurrentName();
if(fieldName == ‘id’)
{
// Get the textual representation of the value.
System.debug(‘fieldName ==id’);
custId = parser.getText();
system.debug(‘custId====>’+custId);
break;
}
}try {
List <account> acc1 = [select NetSuiteCustomerId__c from account where id= :acc.id];if(acc1.isEmpty() == false) {
acc1[0].NetSuiteCustomerId__c = custId;
update acc1;
}
} catch(System.CalloutException e) {
System.debug(‘Callout error: ‘+ e);
}} catch(System.CalloutException e) {System.debug(res.toString());}
}
From response body, we can fetch internal id of customer record in NetSuite and save it in custom field for future updates.
- public static void createNSCustomerRecord(account acc){
- To call above function for creating customer records whenever new account is created or updated in Salesforce, we can write batch classes in Salesforce. Here is the sample code:
- global class NetSuiteBatchApexWebCalloutClass Implements Database.Batchable<account>, Database.AllowsCallouts{global List<account> accountsToTarget;
private NetSuiteBatchApexWebCalloutClass(){}
Global NetSuiteBatchApexWebCalloutClass(Set<String> accRecIds)
{
accountsToTarget = [SELECT Id, Name,owner.Name,NetSuiteCustomerId__c FROM Account where Id IN : accRecIds];
}global Iterable<account> start(database.batchablecontext BC){
return (accountsToTarget);
}
global void execute(Database.BatchableContext BC, List<account> scope){
for(Account a : scope){
NetSuiteWebserviceCallout.createNSCustomerRecord(a);
}
}
global void finish(Database.BatchableContext info){ }//global void finish loop
}
- global class NetSuiteBatchApexWebCalloutClass Implements Database.Batchable<account>, Database.AllowsCallouts{global List<account> accountsToTarget;
Benefits of Restlet NetSuite & Salesforce Integration
These steps are very handy in syncing NetSuite and Salesforce without any bloated middleware, enabling you to:
- Custom integrate
- Save high integration costs
- Synchronize Customers and Contacts in real time
- Convert Opportunities to Quotes or Sales Orders
- Avoid duplicity and re-entry
- Streamline lead-to-cash processes
- Have back office visibility from within Salesforce