Marty Zigman

Conversations with Marty Zigman

Certified Administrator • ERP • SuiteCloud

Quick NetSuite Utility Function: Load Item Record

NetSuite Technical

Tags: ,

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

If you are a NetSuite SuiteCloud software developer, hold high standards for care, and you want to work together, let’s have a conversation.

 

Marty Zigman LinkedIn

Marty Zigman

Holding three official certifications, Marty is widely recognized as a top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. A former Deloitte & Touche CPA and technology executive with CTO roles, he brings over 35 years of leadership in ERP, CRM, and eCommerce business systems. Contact Marty to engage directly.

BiographyYouTubeLinkedInX (Twitter)

18 thoughts on “Quick NetSuite Utility Function: Load Item Record

  1. We got undocumented search column for item: recordtype. This considers lot numbered and serialised items as well.

    var itemtype = nlapiLookupField(‘item’, id, ‘recordtype’);

    Reply
  2. 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);
    }

    Reply
  3. 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

    Reply
  4. 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?

    Reply
  5. 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?

    Reply
  6. Hi! thank you for your blog, it’s pretty usable!!
    So, do you know what is the nlapiLookupField equivalence in the SuiteScript V2?
    Regards!

    Reply
  7. 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

    Reply
  8. 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

    Reply
  9. 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.

    Reply
  10. 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;
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *