RE: posting json to netsuite for sales order.
Seeing some older examples on here for posting json to restlet but cant find anything for the current version of netsuite’s api system. I am wanting to avoid the netsuite-php integration and would rather just post directly to netsuite. My needs are not huge. just being able to post via json a sales order with userid it is going to, items, price per item, and quantity.
Anyone have anything handy I could build off of? Thanks!!!
Ended up doing this.. saving for anyone else interested.
/**
*@NApiVersion 2.x
*@NScriptType Restlet
*/
define([‘N/record’], function (record) {
// Create a NetSuite record from request params
function post(context) {
var objResp = {};
log.audit(‘post’, ‘Context : ‘ + JSON.stringify(context));
var arrJSONItems = context.item;
try {
var recSO = record.create({
type: context.recordtype,
isDynamic: true
});
recSO.setValue(‘entity’, context.entity);
recSO.setValue(‘quantity’, context.quantity);
recSO.setValue(‘otherrefnum’, context.PurchaseOrder);
recSO.setText(‘custbody_reqd_delivery_date’, context.shipdate);
recSO.setText(‘custbodydelivery_date’, context.shipdate);
recSO.setText(‘enddate’, context.shipdate);
recSO.setValue(‘location’, 71);
recSO.setValue(‘custbody_rsm_created_by’, 26211);
var shippingAddr = recSO.getSubrecord({
fieldId: ‘shippingaddress’
});
shippingAddr.setText({
fieldId: ‘city’,
text: context.shipcity || ”
});
shippingAddr.setText({
fieldId: ‘state’,
text: context.state || ”
});
shippingAddr.setText({
fieldId: ‘zip’,
text: context.zip || ”
});
shippingAddr.setText({
fieldId: ‘addr1’,
text: context.addr1 || ”
});
shippingAddr.setText({
fieldId: ‘addr2’,
text: context.addr2 || ”
});
for (var ji in arrJSONItems) {
recSO.selectNewLine(‘item’);
recSO.setCurrentSublistValue(‘item’, ‘item’, arrJSONItems[ji].item);
var lineQty = arrJSONItems[ji].quantity;
if (arrJSONItems[ji].item == ‘561’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 54;
}
if (arrJSONItems[ji].item == ‘667’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 75;
}
if (arrJSONItems[ji].item == ‘559’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 54;
}
if (arrJSONItems[ji].item == ‘558’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 55;
}
if (arrJSONItems[ji].item == ‘666’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 75;
}
if (arrJSONItems[ji].item == ‘560’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 91;
}
if (arrJSONItems[ji].item == ‘687’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 48;
}
if (arrJSONItems[ji].item == ‘684’)
{
lineQty = Number(arrJSONItems[ji].quantity) * 60;
}
recSO.setCurrentSublistValue(‘item’, ‘quantity’, lineQty);
recSO.setCurrentSublistValue(‘item’, ‘price’, arrJSONItems[ji].plvl);
//recSO.setCurrentSublistValue(‘item’, ‘amount’, arrJSONItems[ji].amount);
recSO.commitLine(‘item’);
}
var recordId = recSO.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.audit(‘post’, ‘Created Sales Order | id : ‘ + recordId);
objResp = {
status: ‘Success’,
description: ‘Created Sales Order record | internal id : ‘ + recordId
};
} catch (error) {
log.error(‘post’, ‘ERROR : ‘ + error);
objResp = {
status: ‘Failed’,
description: error.message
};
}
return objResp;
}
return {
post: post
};
});