Jira Code: PROF
Create a sales order and quote in a customer centre based on the customer selected in the suitelet form. The results are shown based on the pagination.
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
* @Script PROF-21 SL Create SO in Customer Center
* @Scriptid customscript_prof21_createso_cc
* @Dependency PROF-21 CS CreateSO in CC fromSLcheck, PROF-21 CS create SO in CC
* for multipage
* @Author Jobin and Jismi IT Services LLP
* @Description To search the matching item of current customer and display the
* items on item page in customer center.
*/
define(['N/ui/serverWidget', 'N/search', 'N/redirect' , 'N/record', 'N/runtime'],
function (serverWidget, search, redirect, record,runtime) {
var PAGE_SIZE = 100;
var SEARCH_ID = 'customsearch887';
var CLIENT_SCRIPT_FILE_ID = 13789;
var CLIENT_SCRIPT_ID = 13788;
function onRequest(context) {
if (context.request.method == 'GET') {
try{
var form = serverWidget.createForm({
title : 'ITEMS',
hideNavBar : false
});
form.clientScriptFileId = CLIENT_SCRIPT_FILE_ID;
// Get parameters
var pageId = parseInt(context.request.parameters.page);
var scriptId = context.request.parameters.script;
var deploymentId = context.request.parameters.deploy;
// Add sublist that will show results
var sublist = form.addSublist({
id : 'custpage_table',
type : serverWidget.SublistType.LIST,
label : 'Items'
});
// Add columns to be shown on Page
sublist.addField({
id : 'custpage_check',
label : '<center>CHECKBOX</center>',
type : serverWidget.FieldType.CHECKBOX
});
sublist.addField({
id : 'custpage_id',
label : 'Internal Id',
// label : '<center>Internal Id</center>',
type : serverWidget.FieldType.TEXT
});
sublist.addField({
id : 'custpage_name',
label : 'Name',
type : serverWidget.FieldType.TEXT
});
sublist.addField({
id: 'custpage_desc',
label: 'Description',
type: serverWidget.FieldType.TEXT
});
sublist.addField({
id: 'custpage_class',
label: 'Class',
type: serverWidget.FieldType.TEXT
});
sublist.addField({
id: 'custpage_unittype',
label: 'UnitType',
type: serverWidget.FieldType.TEXT,
align: serverWidget.LayoutJustification.LEFT
});
sublist.addField({
id: 'custpage_avail',
label: 'Qty Available',
type: serverWidget.FieldType.TEXT
});
sublist.addField({
id: 'custpage_order',
label: 'Qty OnOrder',
type: serverWidget.FieldType.TEXT
});
sublist.addField({
id: 'custpage_price',
label: 'BasePrice',
type: serverWidget.FieldType.TEXT
});
sublist.addField({
id: 'custpage_lastdate',
label: 'Last Date Ordered',
type: serverWidget.FieldType.TEXT
});
// Run search and determine page count
var retrieveSearch = runSearch(SEARCH_ID, PAGE_SIZE);
var pageCount = parseInt(retrieveSearch.count / PAGE_SIZE);
// Set pageId to correct value if out of index
if (!pageId || pageId == '' || pageId < 0)
pageId = 0;
else if (pageId >= pageCount)
pageId = pageCount - 1;
// Add buttons to simulate Next & Previous
if (pageId != 0) {
form.addButton({
id : 'custpage_previous',
label : 'Previous',
functionName : 'getSuiteletPage(' + scriptId + ', ' + deploymentId + ', ' + (pageId - 1) + ')'
});
}
if (pageId != pageCount - 1) {
form.addButton({
id : 'custpage_next',
label : 'Next',
functionName : 'getSuiteletPage(' + scriptId + ', ' + deploymentId + ', ' + (pageId + 1) + ')'
});
}
// Add drop-down and options to navigate to specific page
var selectOptions = form.addField({
id : 'custpage_pageid',
label : 'Page Index',
type : serverWidget.FieldType.SELECT
});
for (i = 0; i < pageCount; i++) {
if (i == pageId) {
selectOptions.addSelectOption({
value : 'pageid_' + i,
text : ((i * PAGE_SIZE) + 1) + ' - ' + ((i + 1) * PAGE_SIZE),
isSelected : true
});
} else {
selectOptions.addSelectOption({
value : 'pageid_' + i,
text : ((i * PAGE_SIZE) + 1) + ' - ' + ((i + 1) * PAGE_SIZE)
});
}
}
// Get subset of data to be shown on page
var addResults = fetchSearchResult(retrieveSearch, pageId);
// Set data returned to columns
var j = 0;
addResults.forEach(function (result) {
sublist.setSublistValue({
id : 'custpage_id',
line : j,
value : result.id
});
sublist.setSublistValue({
id : 'custpage_name',
line : j,
value : result.name
});
if(result.description)
{
sublist.setSublistValue({
id : 'custpage_desc',
line : j,
value : result.description
});
}
else
{
sublist.setSublistValue({
id : 'custpage_desc',
line : j,
value : null
});
}
if(result.price)
{
sublist.setSublistValue({
id : 'custpage_price',
line : j,
value : '<center>'+result.price+'</center>'
});
}
else
{
sublist.setSublistValue({
id : 'custpage_price',
line : j,
value : null
});
}
if(result.unittyp)
{
sublist.setSublistValue({
id : 'custpage_unittype',
line : j,
value :'<center>'+result.unittyp+'</center>'
});
}
else
{
sublist.setSublistValue({
id : 'custpage_unittype',
line : j,
value : null
});
}
if(result.available)
{
sublist.setSublistValue({
id : 'custpage_avail',
line : j,
value : '<center>'+result.available+'</center>'
});
}
else
{
sublist.setSublistValue({
id : 'custpage_avail',
line : j,
value : null
});
}
if(result.order)
{
sublist.setSublistValue({
id : 'custpage_order',
line : j,
value : '<center>'+result.order+'</center>'
});
}
else
{
sublist.setSublistValue({
id : 'custpage_order',
line : j,
value : null
});
}
if(result.clas)
{
sublist.setSublistValue({
id : 'custpage_class',
line : j,
value : result.clas
});
}
else
{
sublist.setSublistValue({
id : 'custpage_class',
line : j,
value : null
});
}
if (result.date) {
// log.debug({
// title : 'result.date',
// details : result.date
// });
sublist.setSublistValue({
id : 'custpage_lastdate',
line : j,
value : result.date
});
} else {
sublist.setSublistValue({
id : 'custpage_lastdate',
line : j,
value : null
});
}
j++
});
form.addButton({
id : 'custpage_quote',
label : 'CREATE QUOTE',
functionName: 'createQuote'
});
form.addButton({
id : 'custpage_so',
label : 'CREATE SO',
functionName: 'createsalesorder'
});
// form.addSubmitButton({
// label : 'CREATE SO'
// });
// sublist.displayType = serverWidget.SublistDisplayType.NORMAL;
form.clientScriptFileId = CLIENT_SCRIPT_ID;
context.response.writePage(form);
}
catch(e)
{
log.debug({
title: 'getSuiteletPage',
details: e.message
});
}
}
}
return {
onRequest : onRequest
};
function runSearch(searchId, searchPageSize) {
try
{
// creating saved search
var itemSearchObj = search.create({
type: "item",
filters: [
["isinactive","is","F"],
"AND",
["isonline","is","T"],
"AND",
["pricing.pricelevel","anyof","2","3","5","1"],
"AND",
["subsidiary","anyof","@CURRENT@"],
"AND",
["pricing.customer","anyof","@CURRENT@"]
],
columns: [
"itemid",
"salesdescription",
"baseprice",
"internalid",
"unitstype",
"custitem_specsheet",
"quantityavailable",
"quantityonorder",
"class"
]
});
log.debug('itemSearchObj', JSON.stringify(itemSearchObj));
return itemSearchObj.runPaged({
pageSize : searchPageSize
});
}
catch(e)
{
log.debug({
title: 'runSearch',
details: e.message
});
}
}
function fetchSearchResult(pagedData, pageIndex) {
try
{
var searchPage = pagedData.fetch({
index : pageIndex
});
var results = new Array();
var item_array=[];
searchPage.data.forEach(function (result) { // fetching values
// from saved search
// results
var internalId = result.id;
var name = result.getValue({
name : 'itemid'
});
var description = result.getValue({
name : 'salesdescription'
});
var price = result.getValue({
name : 'baseprice'
});
var unittyp = result.getText({
name : 'unitstype'
});
var spec = result.getValue({
name : 'custitem_specsheet'
});
var available = result.getValue({
name : 'quantityavailable'
});
var order = result.getValue({
name : 'quantityonorder'
});
var clasvalue = result.getText({
name : 'class'
});
var clas = clasvalue.split(":",1);
log.debug({
title : "xclass",
details : clas
});
item_array.push(internalId);
results.push({
"id" : internalId,
"name" : name,
"description" : description,
"price" : price,
"unittyp" : unittyp,
"spec" : spec,
"available" : available,
"order" : order,
"clas" : clas,
"date" :''
});
});
try{
// log.debug({details:item_array ,title:"item_array"});
// get customer id
var customer_id = runtime.getCurrentUser().id;
// log.debug({
// title : "customer_id",
// details : customer_id
// });
// search to get last order date
var salesorderSearchObj = search.create({
type: "salesorder",
filters: [
["type","anyof","SalesOrd"],
"AND",
["customer.internalidnumber","equalto",customer_id],
"AND",
["item","anyof",item_array]
],
columns: [
search.createColumn({
name: "item",
summary: "GROUP"
}),
search.createColumn({
name: "datecreated",
summary: "MAX",
sort: search.Sort.ASC
})
]
});
var searchresult = salesorderSearchObj.run().getRange({
start : 0,
end : 1000
});
var date_array=new Array;
for(var i=0;i<searchresult.length;i++){
var item_name=searchresult[i].getValue({
name: 'item',
summary: 'GROUP'
});
var index =item_array.indexOf(item_name);
if(index > -1){
var last_date=searchresult[i].getValue({
name : 'datecreated',
summary: 'MAX'
});
results[index].date=last_date;
}
}
}
catch(e){
log.debug({
title: 'lastorderdate',
details: e.message
});
}
log.debug({
title: 'results',
details: results
});
return results;
}
catch(e)
{
log.debug({
title: 'result',
details: e.message
});
}
}
});
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
* @Script PROF-21 CS CreateSO in CC fromSLcheck
* @Scriptid customscript_prof21_createso_checkvalue
* @Dependency PROF-21 SL Create SO in Customer Center
* @Author Jobin and Jismi IT Services LLP
* @Description Retrieving id of selected items .
*/
define(['N/record','N/search','N/runtime','N/currentRecord'],
function(record,search,runtime,currentRecord) {
var selecteditem_array=new Array;
var item_obj;
function createQuote()
{
try
{
var userRole = runtime.getCurrentUser().role;
var user = runtime.getCurrentUser() ;
if(userRole==1030 || userRole==14)
{
var records = currentRecord.get();
var itemdetails = getrefundvalues(records);
console.log("inq");//alert('inn');
function getrefundvalues(record) {
var numLines = record.getLineCount({
sublistId : 'custpage_table'
});
for (var i = 0; i < numLines; i++) {
var currentItem = {};
var check = record.getSublistValue({ //getting value from checked box
fieldId : 'custpage_check',
sublistId : 'custpage_table',
line : i
});
if (check) {
currentItem.product = record.getSublistValue({
fieldId : 'custpage_id',
sublistId : 'custpage_table',
line : i
});
selecteditem_array.push(currentItem.product);
item_obj=('selecteditem_array', JSON.stringify(selecteditem_array)); //converting to object
//log.debug(item_obj);
//console.log(item_obj);
}
}
}
}
window.location.href="https://system.na2.netsuite.com/app/accounting/transactions/estimate.nl?iscustomercenter=1&itemArray="+item_obj+"&whence=";
}
catch(e)
{
console(e);
}
}
function createsalesorder()
{
try
{
var userRole = runtime.getCurrentUser().role;
var user = runtime.getCurrentUser() ;
if(userRole==1030 || userRole==14)
{
var records = currentRecord.get();
var itemdetails = getrefundvalues(records);
console.log("inq");//alert('inn');
function getrefundvalues(record) {
var numLines = record.getLineCount({
sublistId : 'custpage_table'
});
for (var i = 0; i < numLines; i++) {
var currentItem = {};
var check = record.getSublistValue({ //getting value from checked box
fieldId : 'custpage_check',
sublistId : 'custpage_table',
line : i
});
if (check) {
currentItem.product = record.getSublistValue({
fieldId : 'custpage_id',
sublistId : 'custpage_table',
line : i
});
selecteditem_array.push(currentItem.product);
item_obj=('selecteditem_array', JSON.stringify(selecteditem_array)); //converting to object
//log.debug(item_obj);
//console.log(item_obj);
}
}
}
}
window.location.href="https://system.na2.netsuite.com/app/accounting/transactions/salesord.nl?iscustomercenter=1&itemArray="+item_obj+"&whence=";
}
catch(e)
{
log.debug({
title: 'result',
details: e.message
});
}
}
function fieldChanged(context) {
try
{
// Navigate to selected page
if (context.fieldId == 'custpage_pageid') {
var pageId = context.currentRecord.getValue({
fieldId : 'custpage_pageid'
});
pageId = parseInt(pageId.split('_')[1]);
window.location = "https://system.na2.netsuite.com/app/site/hosting/scriptlet.nl?script=310&deploy=1&compid=447968&whence=&page="+pageId;
}
}
catch(e)
{
log.debug({
title: 'error',
details: e.message
});
}
}
return {
fieldChanged:fieldChanged,
createQuote: createQuote,
createsalesorder: createsalesorder
};
});