This article is relevant if you are a SuiteScript developer and you need a quick function to load record items.
Background
During the development of a RESTLet API using version SuiteScript 2.0 technologies, we encountered the common problem of loading item records. Items must be qualified by type before you can perform conventional record operations. To help with this, I crafted a quick function that illustrates the lookups needed to know the item type and the equivalent load record operation.
The following function may be valuable for a basic NetSuite software library. Note, this function is not designed to be specific to version 2.0 syntax.
NetSuite Load Item Record Utility Function
Use the following item lookup SuiteScript function in your library of helper utilities:
function loadItem(id){ var CONST_ITEMTYPE = { 'Assembly' : 'assemblyitem', 'Description' : 'descriptionitem', 'Discount' : 'discountitem', 'GiftCert' : 'giftcertificateitem', 'InvtPart' : 'inventoryitem', 'Group' : 'itemgroup', 'Kit' : 'kititem', 'Markup' : 'markupitem', 'NonInvtPart' : 'noninventoryitem', 'OthCharge' : 'otherchargeitem', 'Payment' : 'paymentitem', 'Service' : 'serviceitem', 'Subtotal' : 'subtotalitem' }; try { return nlapiLoadRecord(CONST_ITEMTYPE[nlapiLookupField('item', id, 'type')], id); } catch(e) { nlapiLogExecution('ERROR','loadItem failed with error: id:' + id, e.message) }; };
Let’s all Be Better SuiteCloud Developers
We got undocumented search column for item: recordtype. This considers lot numbered and serialised items as well.
var itemtype = nlapiLookupField(‘item’, id, ‘recordtype’);
I think a better, less “hardcoded” solution would be to do a search to item records and from there get the correct item type, something line
function get_item_record (itemid) {
if (!itemid) {
return null;
}
var filters = ['internalid', 'is', itemid];
return (nlapiSearchRecord('item', null, filters) || []).reduce(function () {
return last || nlapiLoadRecord(next.getRecordType(), next.getId());
}, null);
}
I like concise codings too.
1. We also try to use a small class/module to manage error messages in SuiteScript V1.0
Something like a function for, I.e.
var detailMsg = ex;
if (ex instanceof nlobjError)
detailMsg = detailMsg + '\nStackTrace: ' + ex.getStackTrace();
nlapiLogExecution('ERROR','loadItem failed with error: id:' + id, detailMsg);
2. I used this library function for all item types.
/**
* Convert item type to record type, so be able to load item record.
*
* @param itemType
* {String} itemtype column value in transaction
* @returns {String} item type codes in NS back-end
*/
function clmShpMark_ItemTypeToRecType(itemType) {
if (!itemType)
return '';
var recType = '';
switch (itemType) {
case 'Assembly' :
recType = 'assemblyitem';
break;
case 'Description' :
recType = 'descriptionitem';
break;
case 'Discount' :
recType = 'discountitem';
break;
case 'DwnLdItem' :
recType = 'downloaditem';
break;
case 'EndGroup' :
recType = ''; // Not SuiteScript Supported Records
break;
case 'GiftCert' :
recType = 'giftcertificateitem';
break;
case 'Group' :
recType = 'itemgroup';
break;
case 'InvtPart' :
recType = 'inventoryitem';
break;
case 'Kit' :
recType = 'kititem';
break;
case 'Markup' :
recType = 'markupitem';
break;
case 'NonInvtPart' :
recType = 'noninventoryitem';
break;
case 'OthCharge' :
recType = 'otherchargeitem';
break;
case 'Payment' :
recType = 'paymentitem';
break;
case 'Service' :
recType = 'serviceitem';
break;
case 'ShipItem' :
recType = ''; // Not SuiteScript Supported Records
break;
case 'Subtotal' :
recType = 'subtotalitem';
break;
case 'TaxGroup' :
recType = 'taxgroup';
break;
case 'TaxItem' :
recType = 'salestaxitem';
break;
}
return recType;
}
Carl
25 Feb(GMT+8)
https://www.linkedin.com/in/carlzeng
http://www.cnblogs.com/backuper
Hi Vanessa,
Thank you for that enhancement. This is easier and requires no maintenance!
Marty
Hi Carlos,
This looks like an enhancement on Vanessa’s which either returns the record object or a search result. Cool!
Marty
Thank you Carl for your contribution. It is more robust.
Marty
Indeed
ill go to vanessa
Hello Marty,
I’ve been following your blog for quite awhile and my team and I have found it very useful in the past, so thanks for that!
We’re starting to use SuitesScript 2.0 for all new development now that it’s out of beta. I see this is using 1.0 and a search for SuiteScript 2.0 on your site returns nothing so I’m wondering if you think that this is the right way to go – is 2.0 stable enough to use in a Production environment?
Yes, we have been using SuiteScript 2.0 now for now a while Saw a few issues early on but it is working out well. I plan to write a few articles with some examples.
It would be nice if there was a 2.0 version of the item search column ‘recordtype’. I just tested that and it didn’t work. Anyone else have any luck with a 2.0 script and Vanessa’s method?
Hi! thank you for your blog, it’s pretty usable!!
So, do you know what is the nlapiLookupField equivalence in the SuiteScript V2?
Regards!
Hi all,
I am having no luck getting it to work. Just starting to look at 2.0 and see that this is a 1.0 script. So 2.0 can call a 1.0 library function? If it can I cannot get it to work. Never seems to be able to find it
Hi Karen,
Are you able to get any data using 1.0 via this call? nlapiLookupField(‘item’, id, ‘type’) (be sure to replace id with the value you are seeking to lookup)
Marty
Hi Manuel,
Have you tried this?
var fieldLookUp = search.lookupFields({
type: search.Type.SALESORDER,
id: ’87’,
columns: [‘entity’, ‘subsidiary’, ‘name’, ‘currency’]});
Hi Marty,
Yes, I can use a nlapiLookupField function all day in 1.0. There just seems to be an issue with a 2.0 script loading this 1.0 library file. I tried to have it return a simple “hello” and got error that it cannot find the function.
Thanks,
Karen
Hi Marty,
I’m having a saved search, How can I get the dynamic column types, and values using suite script 2.0 can you please help me.
Hi Brus,
This question is off topic but the Help document has information on getting column information. If you need help, perhaps you can send a request via:
https://www.prolecto.com/services/innovations/
Marty
function getItemType (currentRec,itemID){
var itemSearchObj = search.create({
type: “item”,
filters: [[“internalid”, “anyof”, itemID]],
columns: [“type”]
});
var searchResult = itemSearchObj.run().getRange({ start: 0, end: 1 });
if (searchResult.length > 0) {
var itemTypeNs = searchResult[0].getText({ name: “type” });
}
log.debug(‘itemTypeNs’,itemTypeNs);
switch (itemTypeNs) {
case ‘Description’: itemType = ‘DESCRIPTION_ITEM’;
break;
case ‘Discount’: itemType = ‘DISCOUNT_ITEM’;
break;
case ‘Inventory Item’: itemType = ‘INVENTORY_ITEM’;
break;
case ‘Item Group’: itemType = ‘ITEM_GROUP’;
break;
case ‘Kit/Package’: itemType = ‘KIT_ITEM’;
break;
case ‘Non-inventory Item’: itemType = ‘NON_INVENTORY_ITEM’;
break;
case ‘Other Charge’: itemType = ‘OTHER_CHARGE_ITEM’;
break;
case ‘Payment’: itemType = ‘PAYMENT_ITEM’;
break;
case ‘Service’: itemType = ‘SERVICE_ITEM’;
break;
case ‘Subscription Plan’: itemType = ‘SUBSCRIPTION_PLAN’;
break;
default:
log.debug(‘type’,type);
break;
}
//}
log.debug(‘itemType’,itemType);
return itemType;
}