Marty Zigman

Conversations with Marty Zigman

Certified Administrator • ERP • SuiteCloud

Learn SuiteScript 2.0 Pattern to Update NetSuite Item Vendor Purchase Price List

NetSuite Technical

Tags: , ,

This article is relevant if you seek to understand how to program NetSuite’s vendor purchase price list on the item record.

Background

In the article Maintain NetSuite Foreign Currency Item Purchase Price Lists I illustrate how to maintain a NetSuite purchase price list using native and custom structures. One of our clients sought a mechanism to update NetSuite’s native vendor purchase price list using some techniques that required script. The price list can be maintained in different currencies. Since it was not documented well, one our senior consultants brought forth the code pattern. I bring it here to you.

NetSuite SuiteScript 2.0 to Update Vendor Item Price List

Below is the basic and simple SuiteScript 2.0 script to help illustrate the pattern to read and update the vendor item purchase price list. See related image for an example of the vendor item list we are updating.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/search'],
/**
 * @param {record} record
 * @param {search} search
 */
function(record, search) {
   
    /**
     * Function definition to illustrate how to update the currency price list offered by a vendor.
     */
	function beforeLoad(scriptContext) {
		
		//for learning, hard code loading the item we are working with
		var itemRecord = record.load({type: record.Type.INVENTORY_ITEM, id: 501});

		//get the item's vendor price sublist
		var sublistRecord = itemRecord.getSublistSubrecord({
			sublistId: 'itemvendor',
			fieldId: 'itemvendorprice',
			line: 0
		});
		
		//spin through the sublist of prices; arbitrarily update the price to illustrate how
		var count = sublistRecord.getLineCount({sublistId: 'itemvendorpricelines'});
		for ( var i=0; i < count ; i++)
		{
			var id = sublistRecord.getSublistValue({sublistId: 'itemvendorpricelines', fieldId: 'id', line: i});
			var vendorcurrency = sublistRecord.getSublistValue({sublistId: 'itemvendorpricelines', fieldId: 'vendorcurrency', line: i});
			var vendorcurrencytext = sublistRecord.getSublistText({sublistId: 'itemvendorpricelines', fieldId: 'vendorcurrency', line: i});
			var vendorprice = sublistRecord.getSublistValue({sublistId: 'itemvendorpricelines', fieldId: 'vendorprice', line: i});

			log.debug(id + vendorcurrency + vendorcurrencytext, vendorprice);

			sublistRecord.setSublistValue({sublistId: 'itemvendorpricelines', fieldId: 'vendorprice', line: i, value: (i+1)*1000});

		}
		
		//commit the item record.  The sublist will update
		itemRecord.save();
	}

    return {
        beforeLoad: beforeLoad,
    };
    
});

NetSuite Enhancement Support

NetSuite’s platform is designed to be enhanced to meet operational business requirements. If you are looking to be part of a team of professionals that appreciate innovation, technology, and leadership, 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)

3 thoughts on “Learn SuiteScript 2.0 Pattern to Update NetSuite Item Vendor Purchase Price List

  1. Hi Marty,

    One issue with your code is that SS2 does not provide a way to commit subrecord. As per NS help article /app/help/helpcenter.nl?fid=section_4267286323.html for Record.save():

    This method is not available to subrecords.

    The SS1 -> SS2 map entry for nlobjSubrecord.commit() says:

    This API does not have a SuiteScript 2.x equivalent. SuiteScript 2.x subrecords are returned as record.Record objects.

    The solution to this seems to be omitting saving subrecord at all.

    Thanks,
    Lukasz

    Reply

Leave a Reply

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