Using formulas within nlapiSearchRecord columns - netsuite

When using adhoc nlapiSearchRecord within scripts, such as:
var filters = new Array();
var columns = new Array();
filters[0] = new nlobjSearchFilter('isinactive', null, 'is', 'F' );
filters[1] = new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0);
filters[2] = new nlobjSearchFilter('internalid', 'vendor', 'anyof', vendorid );
columns[0] = new nlobjSearchColumn('itemid');
columns[1] = new nlobjSearchColumn('entityid', 'vendor');
columns[2] = new nlobjSearchColumn('vendorcode');
columns[3] = new nlobjSearchColumn('formulanumeric');
columns[3].setFormula('{vendorcost}');
columns[4] = new nlobjSearchColumn('vendorpricecurrency');
columns[5] = new nlobjSearchColumn('isinactive');
columns[6] = new nlobjSearchColumn('vendor');
columns[7] = new nlobjSearchColumn('averagecost');
columns[8] = new nlobjSearchColumn('lastpurchaseprice');
columns[9] = new nlobjSearchColumn('custitem_costrepl');
var searchresults = nlapiSearchRecord('item', null, filters, columns );
How do I retrieve the value of the formula in column 3?
I've tried searchresults.getValue(column[3])) and searchresults.getValue(3)) but both fail.
The reason for this, is some of our vendorcost values are 4 or 5 decimal places, but the most decimal precision the plain search returns is 3.

You are ignoring the fact that searchResults is an array
And there are multiple ways:
Option#1 //works always
searchresults[INDEX].getValue(searchresults[INDEX].getAllColumns()[3]);
Option#2 //works always - same as what you tried, but, you missed array
searchresults[i].getValue(columns[3]);
Option#3 //use only if you have one numeric formula
searchresults[INDEX].getValue('formulanumeric');

var filters = new Array();
filters[0] = new nlobjSearchFilter('isinactive', null, 'is', 'F');
filters[1] = new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0);
filters[2] = new nlobjSearchFilter('internalid', 'vendor', 'anyof', vendorid);
var columns = new Array();
columns[0] = new nlobjSearchColumn('itemid');
columns[1] = new nlobjSearchColumn('entityid', 'vendor');
columns[2] = new nlobjSearchColumn('vendorcode');
columns[3] = new nlobjSearchColumn('formulanumeric').setFormula('{vendorcost}'); // you can use formula inline
columns[4] = new nlobjSearchColumn('vendorpricecurrency');
columns[5] = new nlobjSearchColumn('isinactive');
columns[6] = new nlobjSearchColumn('vendor');
columns[7] = new nlobjSearchColumn('averagecost');
columns[8] = new nlobjSearchColumn('lastpurchaseprice');
columns[9] = new nlobjSearchColumn('custitem_costrepl');
var searchresults = nlapiSearchRecord('item', null, filters, columns);
for (var i = 0; searchresults != null && i < searchresults.length; i++) {
var vendorcost = searchresults[i].getValue(columns[3]); // gives you the formula field value
}

Related

If Else statement inside the nlapiSearchRecord

Badly need help..
I am trying to add a condition inside my nlapiSearchRecord API but the condition is not being followed. Though it is running fine with no errors, the suitelet is looking at the first parameter even if it should follow the else statement.
Please refer at the line: var arrSearchResults = nlapiSearchRecord(searchRecordType, null, arrSearchFilters, (searchRecordType = 'transaction') ? arrSearchColumnsTrans : arrSearchColumnsCustomers) || [];
Even though the searchRecordType is 'customers', it still follows the arrSearchColumnsTrans, not the arrSearchColumnsCustomers
var searchRecordType = request.getParameter ('custpage_rectype');
var searchLookupText = request.getParameter ('custpage_lookuptext');
var searchLookupField = request.getParameter ('custpage_lookupfield');
var searchParameter = request.getParameter ('custpage_searchparam');
var arrSearchFilters = [new nlobjSearchFilter(searchLookupField, null, searchParameter, searchLookupText)];
var arrSearchColumnsTrans = [new nlobjSearchColumn('internalid'),
new nlobjSearchColumn('type'),
new nlobjSearchColumn('entity')];
var arrSearchColumnsCustomers = [new nlobjSearchColumn('internalid'),
new nlobjSearchColumn('entityid'),
new nlobjSearchColumn('companyname')];
var arrSearchResults = nlapiSearchRecord(searchRecordType, null, arrSearchFilters,
(searchRecordType = 'transaction') ? arrSearchColumnsTrans : arrSearchColumnsCustomers) || [];
var objVal = {};
for (var i = 0, ii = arrSearchResults.length; i < ii; i++)
{
var val = arrSearchResults[i];
objVal[val.getId()] = {};
objVal[val.getId()].tranid = val.getValue('tranid');
objVal[val.getId()].internalid = val.getValue('internalid');
objVal[val.getId()].type = val.getValue('type');
objVal[val.getId()].entityid = val.getValue('entityid');
objVal[val.getId()].entity = val.getText('entity');
}
Your ternary statement doesn't actually have a conditional:
(searchRecordType = 'transaction') ? ...
Because you're using =, you're just assigning the value of 'transaction' to searchRecordType. If you want to compare the two values instead, you'll want to use == or ===.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality
Thank you!
All good now..
var arrSearchResults = (searchRecordType === 'transaction') ? nlapiSearchRecord(searchRecordType, null, arrSearchFilters, arrSearchColumnsTrans) : (searchRecordType === 'item') ? nlapiSearchRecord(searchRecordType, null, arrSearchFilters, arrSearchColumnsItems) : nlapiSearchRecord(searchRecordType, null, arrSearchFilters, arrSearchColumns) || [];

how to get script name and id from scriplet in netsuite

How could we get all script name and script id in array I want it to be shown in dropdown in my application.
Here is my code which is only giving me filename I am not getting external url like this
https://4512314-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=692&deploy=1
here is my code
function searchScriptFile()
{
var folderid = 'SuiteScripts';
var filters = new Array();
filters[0] = new nlobjSearchFilter('name', null, 'is', folderid);
var columns = new Array();
var filename = new nlobjSearchColumn('name', 'file');
var fileid = new nlobjSearchColumn('internalid', 'file');
var path = new nlobjSearchColumn('url', 'file');
columns[0] = filename;
columns[1] = fileid;
columns[2] = path;
var searchResult = nlapiSearchRecord('folder', null , filters , columns);
var res = new Array();
if(searchResult) {
for (var i = 0 ; i < searchResult.length; i++) {
thisfile = searchResult[i].getValue(filename);
thisfileid = searchResult[i].getValue(fileid);
thispath = searchResult[i].getValue(path);
res[i] = thisfile + ' ' + thisfileid + ' ' + thispath;
nlapiLogExecution('DEBUG', 'Test', thisfileid + ' ' + thisfile);
}
return res[27];
}
}
You need to search for the script deployments, not the files in the file cabinet.
var scriptdeploymentSearch = nlapiSearchRecord("scriptdeployment",null,
[
],
[
new nlobjSearchColumn("title").setSort(false),
new nlobjSearchColumn("scriptid"),
new nlobjSearchColumn("script"),
new nlobjSearchColumn("scriptid","script",null),
new nlobjSearchColumn("recordtype"),
new nlobjSearchColumn("status"),
new nlobjSearchColumn("isdeployed"),
new nlobjSearchColumn("scripttype"),
new nlobjSearchColumn("ismobilemenu"),
new nlobjSearchColumn("apiversion","script",null)
]
);
You need to run search on scriptdeployment(as suggested above by dcrs) and not on folder. Although I didn't find any way to fetch externalurl of the script-deployement, guess NetSuite doesn't support it yet.
Here is the basic code to on ScriptDeploymemts though
search.create({
type: 'scriptdeployment',
columns: ['script.scriptfile', 'title',
'scriptid', 'script.apiversion']
}).run().getRange({ start: 0, end: 1000 });

Suitescript to return all items supplied by a vendor as a search object

Re-entering the code as follows, with combined suggestions from #bknights & #prasun
function main_GetVendorItems(request, response) {
response.write(JSON.stringify(getVendorItems(request)));
}
function getVendorItems(request) {
var vendorid = request.getParameter('vendor');
nlapiLogExecution('DEBUG', 'searchRes', 'Searching For Vendor ID: '+vendorid );
var filters = new Array();
var columns = new Array();
filters[0] = new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0);
filters[1] = new nlobjSearchFilter('othervendor', null, 'is', [vendorid] );
columns[0] = new nlobjSearchColumn('itemid');
columns[1] = new nlobjSearchColumn('entityid', 'vendor');
columns[2] = new nlobjSearchColumn('vendorcost');
columns[3] = new nlobjSearchColumn('vendorcode');
columns[4] = new nlobjSearchColumn('vendorpricecurrency');
var searchresults = nlapiSearchRecord('item', null, filters, columns );
//for test test each element of the array
(searchresults || []).forEach(function(res){
nlapiLogExecution('DEBUG', 'searchRes', res instanceof nlobjSearchResult);
})
return searchresults;
}
The calling function as below:
function test () {
var vendorID = nlapiGetFieldValue('custrecordvpr_supplier'); alert('searching for vendor ID: '+vendorID );
var url = nlapiResolveURL('SUITELET', 'customscriptls_getvendoritems', 'customdeployls_getvendoritems', true);
var params = {}
params['vendor'] = vendorID;
var response = nlapiRequestURL(url, params);
var VendorItemsSublist = response.getBody();
nlapiSetFieldValue('custrecordvpr_comment',VendorItemsSublist );
}
I've got a comment field on my custom record/form which shows the returned object. On the code above, what's really strange, is I'm not getting any information being added to the execution log,even the first entry where it should post the vendor id being searched for.
I've checked the script and deployment records, and there is nothing untoward or amiss there... I have got to be missing something extremely simple.
Incidentally, the code is being called by a "Test" button on my custom form.
It looks like you are not writing properly to the response object in Suitelet.
function main_GetVendorItems(request, response) {
response.write(JSON.stringify(getVendorItems(request)));
}
function getVendorItems(request) {
var vendorid = request.getParameter('vendorid');
var filters = new Array();
var columns = new Array();
filters[0] = new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0);
//filter should be on vendor
filters[1] = new nlobjSearchFilter('vendor', null, 'anyof', vendorid );
columns[0] = new nlobjSearchColumn('itemid');
columns[1] = new nlobjSearchColumn('entityid', 'vendor');
columns[2] = new nlobjSearchColumn('vendorcost');
columns[3] = new nlobjSearchColumn('vendorcode');
columns[4] = new nlobjSearchColumn('vendorpricecurrency');
var searchresults = nlapiSearchRecord('item', null, filters, columns );
//for test test each element of the array
(searchresults || []).forEach(function(res){
nlapiLogExecution('DEBUG', 'searchRes', res instanceof nlobjSearchResult);
})
return searchresults;
}
Also, make sure vendor id is specified in request parameter
var url = nlapiResolveURL('SUITELET', 'customscriptls_getitemvendors', 'customdeploy_getitemvendors', true);
var params = {}
params['itemid'] = itemID ; // itemID is passed to this function.
params['vendorid'] = vendorID ; // vendorID is passed to this function.
var response = nlapiRequestURL(url, params);
var itemVendorSublist = response.getBody();
If you're trying to query on item vendor then simply try
filters[1] = new nlobjSearchFilter('vendor', null,'anyof', vendorid );
This works in a console window. I suspect you are not supplying the numeric internal id in your parameter:
var vendorid = 43; // or 32 values from my test account. Want to confirm that the code works whether vendor is item's preferred vendor or not.
nlapiSearchRecord('item', null, [
new nlobjSearchFilter('vendorcost', null, 'greaterthan', 0),
new nlobjSearchFilter('internalid', 'vendor', 'anyof', [vendorid]), //numeric value
new nlobjSearchFilter('internalid', null, 'is', '42') // limit results for testing
], [
new nlobjSearchColumn('itemid'),
new nlobjSearchColumn('entityid', 'vendor'),
new nlobjSearchColumn('vendorcost'),
new nlobjSearchColumn('vendorcode'),
new nlobjSearchColumn('vendorpricecurrency')
]).forEach(function(it) {
console.log("%s from %s", it.getValue('itemid'), it.getValue('entityid', 'vendor'));
});
Although I've been working with Netsuite since 2002 I've never returned a set of search results directly from a Suitelet. I've seen that a couple of times recently in people's answers on this forum but I still find it a bit amusing.
If I were writing this I'd have tended to do the following:
var results = (nlapiSearchRecord(...) || []).map(function(res){
return { id:res.getId(), vendorName: res.getValue('entityid', 'vendor')...};
});
response.setContentType('JAVASCRIPT');
response.write(JSON.stringify(results));
Actually there's generally a little more. I have a sublime text snippet that I use for suitelet responses that handles a couple of common JSONP patterns (e.g. if you were calling this from a website):
function _sendJSResponse(request, response, respObject){
response.setContentType('JAVASCRIPT');
var callbackFcn = request.getParameter("jsoncallback") || request.getParameter('callback');
if(callbackFcn){
response.writeLine( callbackFcn + "(" + JSON.stringify(respObject) + ");");
}else response.writeLine( JSON.stringify(respObject) );
}

Search for records that have unassigned field in SuiteScript

What is the internal id for 'unassigned'? I've tried 0, -1, null, but have run out of ideas...
var unassigned = 'WHAT IS IT?';
var filters = new Array();
filters.push(new nlobjSearchFilter(nsIds.customerAreas_relatedCustomer, null, 'anyOf', unassigned));
var columns = new Array();
var nameColumn = new nlobjSearchColumn('name', null);
columns.push(nameColumn);
var searchResult = nlapiSearchRecord('customrecord_test', null , filters , columns);
Try this:
filters.push(new nlobjSearchFilter(nsIds.customerAreas_relatedCustomer, null, 'anyOf', '#NONE#'));

YUI DataTable with TEXT responseType

//Define the columns of the table
var myColumnDefs = new Array();
for ( i=0; i < $names.length ; i++)
{
var colObjConfig = new Object();
colObjConfig.label =$names[i];
colObjConfig.resizeable =true;
colObjConfig.width =150;
myColumnDefs.push ( new YAHOO.widget.Column(colObjConfig));
}
var beginningScoreRow = "1111,1111|1111,1111";
var myDataSource = new YAHOO.util.FunctionDataSource( function () {
return beginningScoreRow ;} );
myDataSource.responseType = YAHOO.util.DataSource.TYPE_TEXT;
myDataSource.responseSchema = {
fieldDelim : ",",
recordDelim : "|"
};
var myDataTable = new YAHOO.widget.DataTable("statusTable", myColumnDefs,
myDataSource);
I want to use TYPE_TEXT responseType for a YUI DataSource. But the above failed to render the data in the YUI table column cells. I do not see any error in JS console either.
What am i missing?
var column = new YAHOO.widget.Column(colObjConfig);
column.field = ""+i+"";
myColumnDefs.push (column );
Changed the column definition to add the field property and it did the trick...

Resources