Backend Suitelets are suitelets that do not generate any UI elements.
Their sole purpose is to execute backend logic, which can then be parsed by other parts of a custom application.
Just like a Suitelet that builds NetSuite pages, a backend Suitelet is invoked by making HTTP GET or POST calls to a NetSuite-generated Suitelet URL.
They Provide a service for backend logic to other SuiteScripts, or to other external hosts outside of NetSuite.
Note: Check Available without login field on Deployment record.
Sample Script:
The following script returns Customer account details from Netsuite to the suitelet called with customer Id as parameter
define([ ‘N/record’ ,’N/search’,], function(record,search) {
/**
* @description function for generating date and time in format dd/mm/yyyy
* @returns {*}
*/
function generateDate(today){
try {
//Setting up Date Format
var yyyy = today.getFullYear();
var mm = today.getMonth() + 1;
mm = (mm < 10 ? '0' : '') + mm;
var dd = (today.getDate() < 10 ? '0' : '') + today.getDate();
var updatedDate = dd+"/"+mm+"/"+yyyy;
// log.debug("updatedDate",updatedDate);
return updatedDate;
}catch (e) {
log.debug("error @ generateDate")
}}
/**
* @description function for fetching customer Netsuite ID from MagentoID
* @returns {*}
*/
function fetchCustomerById(customerMagentoId){
try{
let customerSearchObj = search.create({
type: "customer",
columns: ['internalid', 'companyname', 'custentity_jj_magentoid'],
filters: [['custentity_jj_magentoid', 'equalto', customerMagentoId.toString().trim()],
"AND",
["isinactive", "is", "F"]]
});
let searchResultCount = customerSearchObj.runPaged().count;
log.debug("customer Search result count", searchResultCount);
let customerData = {"count":"","internalId":""};
customerData.count = searchResultCount;
customerSearchObj.run().each(function (result) {
customerData.internalId = result.getValue({name: 'internalid'});
return true;
});
log.debug("customerData", customerData);
return customerData; // customer exist
}catch (e) {
log.debug("error @ fetchCustomerById ", e);
}
}
/**
* @description function for fetching sales order count for current month
* @returns {*}
*/
function fetchSalesOrderCount(customerId){
let endDate = new Date();
//endDate.setDate(endDate.getDate() + 1);
endDate = generateDate(endDate);
let startDate = new Date();
startDate.setMonth(startDate.getMonth() - 1);
startDate = generateDate(startDate);
log.debug("startDate",startDate);
log.debug("endDate",endDate);
var salesorderSearchObj = search.create({
type: "transaction",
filters:
[
["type", "anyof", "SalesOrd"],
"AND",
["mainline", "is", "T"],
"AND",
["customer.internalid", "anyof", customerId],
"AND",
["trandate", "onorafter", startDate],//"18/10/2021"],
"AND",
["trandate", "onorbefore",endDate]//"21/11/2021"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = salesorderSearchObj.runPaged().count;
log.debug("salesorderSearchObj result count", searchResultCount);
return searchResultCount;
}
/**
* @description function for fetching estimate count for current month
* @returns {*}
*/
function fetchEstimateCount(customerId){
let endDate = new Date();
endDate = generateDate(endDate);
let startDate = new Date();
startDate.setMonth(startDate.getMonth() - 1);
startDate = generateDate(startDate);
log.debug("startDate",startDate);
log.debug("endDate",endDate);
var estimateSearchObj = search.create({
type: "transaction",
filters:
[
["type", "anyof", "Estimate"],
"AND",
["mainline", "is", "T"],
"AND",
["customer.internalid", "anyof", customerId],
"AND",
["trandate", "onorafter", startDate],
"AND",
["trandate", "onorbefore", endDate]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = estimateSearchObj.runPaged().count;
log.debug("estimateSearchObj result count", searchResultCount);
return searchResultCount;
}
onRequest = (scriptContext) => {
if (scriptContext.request.method === 'GET') {
try {
log.debug(“scriptContext”,scriptContext);
//Customer ID is extracted from suitelet parameter
var customerMagentoId = scriptContext.request.parameters.ID;
let customerDetails = fetchCustomerById(customerMagentoId);
log.debug("customerDetails", customerDetails);
if (customerDetails.count === 0) {
var response = {message: "Customer Doesn't Exist"}
return JSON.stringify(response);
}
let customerId = customerDetails.internalId;
if (customerId) {
var objRecord = record.load({
type: record.Type.CUSTOMER,
id: customerId,
isDynamic: true
});
let notifications = {};
var responseBody =
{
acc_balance: "",
this_month_due: "",
due_date: "",
acc_limit: "",
this_month_orders: "",
this_month_quote: "",
eligible_for_account_payment: "",//(boolean 1- yes, 0 for no),
credit_limit: "",
used_credit: "",
available_credit: "",
notifications: notifications
}
notifications.comments = objRecord.getValue("custentity_jj_notifications_sibo_1148");
responseBody.acc_balance = objRecord.getValue("depositbalance");
responseBody.this_month_due = objRecord.getValue("balance");
let dueDays = objRecord.getValue("daysoverdue");
let dueDate = new Date();
dueDate.setDate(dueDate.getDate() - dueDays);
dueDate = generateDate(dueDate);
responseBody.due_date = dueDate;
responseBody.acc_limit = objRecord.getValue("custentity_jj_account_limit_sibo_1148");
responseBody.credit_limit = objRecord.getValue("creditlimit");
let dueBalance = objRecord.getValue("balance");
if (responseBody.credit_limit) {
if (dueBalance < 0) {
responseBody.available_credit = responseBody.credit_limit + dueBalance;
} else {
responseBody.available_credit = responseBody.credit_limit - dueBalance;
}
responseBody.used_credit = responseBody.credit_limit - responseBody.available_credit;
} else {
responseBody.available_credit = 0;
responseBody.used_credit = 0;
}
responseBody.this_month_quote = fetchEstimateCount(customerId);
responseBody.this_month_orders = fetchSalesOrderCount(customerId);
if (responseBody.available_credit > 0) {
responseBody.eligible_for_account_payment = 1;
} else {
responseBody.eligible_for_account_payment = 0;
}
}
scriptContext.response.write( JSON.stringify(responseBody));
}
catch (err) {
log.error({title: "Error @ onRequest", details: err.message});
}
}
}
return {onRequest:onRequest,
};
});