Jira Code: Prof – 21
When a user logs into the NetSuite account through customer centre user cannot create a SO from there, in order to tackle this situation we have created the custom form and then from the custom form, user can create the sales order.
/**
* @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", ["subsidiary", "anyof", "@CURRENT@"]
],
columns: [
"itemid",
"salesdescription",
//"baseprice",
"onlinecustomerprice",
"internalid",
"unitstype",
"custitem_specsheet",
"quantityavailable",
"quantityonorder",
"class",
search.createColumn({
name: "pricelevel",
join: "pricing"
}),
search.createColumn({
name: "unitprice",
join: "pricing"
}),
"pricinggroup"
]
});
log.debug('itemSearchObj', JSON.stringify(itemSearchObj));
return itemSearchObj.runPaged({
pageSize: searchPageSize
});
} catch (e) {
log.debug({
title: 'runSearch',
details: e.message
});
}
}
function findcustomerpricing() {
try {
var pricingarray = {};
var userId = runtime.getCurrentUser().id;
log.debug({
title: "userId",
details: userId
});
var custRecord = record.load({
type: record.Type.CUSTOMER,
id: userId,
isDynamic: true
});
var numLines = custRecord.getLineCount({
sublistId: 'grouppricing'
});
var grouppricing = [];
for (var k = 0; k < numLines; k++) {
var groupId = custRecord.getSublistValue({
sublistId: 'grouppricing',
fieldId: 'group',
line: k
});
log.debug({
title: "groupId",
details: groupId
});
var level = custRecord.getSublistValue({
sublistId: 'grouppricing',
fieldId: 'level_display',
line: k
});
grouppricing.push({
groupId: groupId,
level: level
});
log.debug({
title: "level",
details: level
});
}
var itempricing = [];
var numLines = custRecord.getLineCount({
sublistId: 'itempricing'
});
for (var k = 0; k < numLines; k++) {
var itemId = custRecord.getSublistValue({
sublistId: 'itempricing',
fieldId: 'item',
line: k
});
log.debug({
title: "itemId",
details: itemId
});
var itemlevel = custRecord.getSublistValue({
sublistId: 'itempricing',
fieldId: 'level_display',
line: k
});
log.debug({
title: "Item level",
details: level
});
var itemprice = custRecord.getSublistValue({
sublistId: 'itempricing',
fieldId: 'price',
line: k
});
itempricing.push({
itemId: itemId,
itemlevel: itemlevel,
itemprice: itemprice
});
}
return {
itempricing: itempricing,
grouppricing: grouppricing,
userId: userId
}
} catch (e) {
log.debug("e", e);
}
}
function checkprice(id, priceobj, pricinggroup, check) {
try {
if (!check) {
for (var i = 0; i < priceobj.itempricing.length; i++) {
if (priceobj.itempricing[i].itemId == id) {
if (priceobj.itempricing[i].itemlevel == "Custom") {
return "Custom"
}
return priceobj.itempricing[i].itemlevel
}
}
for (var i = 0; i < priceobj.grouppricing.length; i++) {
if (priceobj.grouppricing[i].groupId == pricinggroup) {
log.debug("afa", priceobj.grouppricing[i].level);
return priceobj.grouppricing[i].level
}
}
return "A"
} else {
for (var i = 0; i < priceobj.itempricing.length; i++) {
if (priceobj.itempricing[i].itemId == id) {
return priceobj.itempricing[i].itemprice
}
}
}
} catch (e) {
log.debug("e", e);
}
}
function fetchSearchResult(pagedData, pageIndex) {
try {
var searchPage = pagedData.fetch({
index: pageIndex
});
var results = new Array();
var priceobj = findcustomerpricing();
log.debug({
title: "priceobj",
details: priceobj
});
var item_array = [];
searchPage.data.forEach(function(result) {
// fetching values
// from saved search
// results
var internalId = result.id;
var level = result.getText({
name: "pricelevel",
join: "pricing"
});
var pricinggroup = result.getValue({
name: "pricinggroup"
});
var pricinggroupv = result.getText({
name: "pricinggroup"
});
var clasvalue = result.getText({
name: 'class'
});
var clas = clasvalue.split(":", 1);
log.debug({
title: "xclass",
details: clas
});
log.debug({
title: pricinggroupv,
details: pricinggroup
});
var customerlevel = checkprice(internalId, priceobj, pricinggroup, false);
log.debug({
title: "customerlevel",
details: customerlevel
});
var name = result.getValue({
name: 'itemid'
});
var description = result.getValue({
name: 'salesdescription'
});
var price = result.getValue({
name: "unitprice",
join: "pricing"
});
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'
});
if (customerlevel == "Custom") {
var price = checkprice(internalId, priceobj, pricinggroup, true)
}
if (customerlevel == level || customerlevel == pricinggroup || customerlevel == "Custom") {
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'
});
var last = last_date.substring(0, last_date.indexOf(" "));
results[index].date = last;
}
}
} 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
});
}
}
});