RESTlet – Add Item to Commerce Category

Here is the RESTlet code to add a product to an existing Commerce Category.

AddItemToCommerceCategory

This is called via a post request.

POST: function(datain) { addProductToCategory(datain); }

Here is the code in a form more easily copied.

addProductToCategory: function(datain) {
try
{
if (!datain.sku) return “Missing sku”;
if (!datain.vendor_id) return “Missing vendor_id”;
if (!datain.categories) return “Missing categories”;

var item_ids = logic_restlet_sca.GetItemIds(datain.sku, datain.vendor_id);

for (var i = 0; i < datain.categories.length; i++) {
// load a verify that the category exists
var categoryRec = nlapiLoadRecord(‘commercecategory’, datain.categories[i]);
if (!categoryRec) continue;

for (var j=0; j < item_ids.length; j++) {
categoryRec.selectNewLineItem(‘items’);
categoryRec.setCurrentLineItemValue(‘items’, ‘item’, item_ids[0].internalid)
categoryRec.commitLineItem(‘items’);
}

var id = nlapiSubmitRecord(categoryRec);
}
return “Success”;
}
catch (err) {
return err.message
}
}
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s