Post json response to suitelet to create sales order in Netsuite. We are using version 2 *@NApiVersion 2.x

I need to write a suitelet to get data submitted from html page as json response and use this data to create a sales order page

This is sample response from html submit button

GET – https://www.xyz.com/example.html/?callback=jQuery545221&{%22orderdetail%22:[{%22id%22:%2289%22,%22quantity%22:2,%22price%22:%2213.96%22,%22amount%22:%2227.92%22},
{%22id%22:%2280%22,%22quantity%22:1,%22price%22:%2212.95%22,%22amount%22:%2212.95%22},
{%22id%22:%2281%22,%22quantity%22:2,%22price%22:%2210.95%22,%22amount%22:%2221.90%22},
{%22id%22:%2283%22,%22quantity%22:3,%22price%22:%2212.56%22,%22amount%22:%2237.68%22},
{%22id%22:%2275%22,%22quantity%22:4,%22price%22:%2212.95%22,%22amount%22:%2251.80%22}],
%22customer_id%22:%22180%20xyz%20abc%22,%22total_amount%22:%22152.25%22}&_=1580411441286

where id = 89 , quantity = 2, price = 13.96, amount = 27.92 (2*13.96) in first line
id = 80 , quantity = 1, price = 12.95, amount = 12.95 (1*12.95) in second line and so on’
here are total 5 different items in same order (in array orders)
and in last line
there is customer_id =180 xyz abc , total amount = 152.25

I need help in processing this response to suitelet to create a sales order.

Rookie Asked on February 4, 2020 in SuiteScript.
Add Comment
10 Answer(s)

HTML should make a POST call to the Suitelet.

On your function that handles POST on the Suitelet you just need to grab the data…

 var data = context.request.body;
                //data comes in as a string so we need to convert it back to an object using JSON.parse
  processPOSTData(JSON.parse(data));
Beginner Answered on February 4, 2020.
Add Comment

Your HTML page that makes a POST call to your Suitelet should determine the data structure.  Whatever data structure you are POSTing to the Suitelet, is what you will need to code for on the SL side of things.

 

Beginner Answered on February 4, 2020.
Add Comment

Thank u for reply. I have tried this code, can u please help me where to put your code, this is my very first time writing suitelet

 

/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define([‘N/search’, ‘N/record’],
function (search, record) {
function onRequest(context) {
if (context.request.method === ‘GET’) {

// Get parameters
var customer_id = context.request.parameters.customer_id;
var total_amount = context.request.parameters.total_amount;
var id = context.request.parameters.id;
var quantity = context.request.parameters.quantity;
var price = context.request.parameters.price;
var amount = context.request.parameters.amount;

var salesOrder = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true,
});

// memo
salesOrder.setValue({
fieldId: ‘memo’,
value: Testing
});

// Set customform
salesOrder.setValue({
fieldId: ‘customform’,
value: ‘123’ //internal id
});

// Set shipmethod
salesOrder.setValue({
fieldId: ‘shipmethod’,
value: ‘123’ //internal id
});

// Shipping Carrier
salesOrder.setValue({
fieldId: ‘shipcarrier’,
value: FedEx
});

//Sales representative
salesOrder.setValue({
fieldId: ‘salesrep’,
value: ‘123’ //internal id
});

salesOrder.selectNewLine({
sublistId: ‘item’
});

salesOrder.setCurrentSublistValue({
sublistId: ‘item’,
fieldId: ‘item’,
value: item.id
});

salesOrder.setCurrentSublistValue({
sublistId: ‘item’,
fieldId: ‘tax’,
value: 0
});

salesOrder.setCurrentSublistValue({
sublistId: ‘item’,
fieldId: ‘quantity’,
value: item.quantity
});

salesOrder.setCurrentSublistValue({
sublistId: ‘item’,
fieldId: ‘rate’,
value: item.price
});

salesOrder.setCurrentSublistValue({
sublistId: ‘item’,
fieldId: ‘amount’,
value: item.amount
});

salesOrder.commitLine({
sublistId: ‘item’
});

try {

var sales_order_id = salesOrder.save({
ignoreMandatoryFields: true
});

log.debug(‘record save with id’, sales_order_id);//sales order internal id
return id;

} catch (e) {
return 0;

}

return {
onRequest: onRequest
};
});

Rookie Answered on February 4, 2020.
Add Comment

Replace this…

if (context.request.method === ‘GET’) {

// Get parameters
var customer_id = context.request.parameters.customer_id;
var total_amount = context.request.parameters.total_amount;
var id = context.request.parameters.id;
var quantity = context.request.parameters.quantity;
var price = context.request.parameters.price;
var amount = context.request.parameters.amount;

with…

if (context.request.method === ‘POST’) {
var data =  context.request.body;
var soData = JSON.parse(data);
...and then your salesorder creation code would look like this
salesOrder.selectNewLine({
sublistId: ‘item’
});
salesOrder.setCurrentSublistValue({
sublistId: ‘item’,
fieldId: ‘item’,
value: soData.id
});
salesOrder.setCurrentSublistValue({
sublistId: ‘item’,
fieldId: ‘quantity’,
value: soData.quantity
});
}
Beginner Answered on February 4, 2020.
Add Comment

Thank u so much. I will try and let u know.

Rookie Answered on February 4, 2020.
Add Comment

You’ll need to structure your HTML data for the salesorder lines as an array of objects.

var salesorder = {

lines : [

{id: xxx, qty: 1,price : $xxx.xx},{id:xxx. qty: 1, price: $xxx.xx}

]

 

Beginner Answered on February 4, 2020.
Add Comment

I can create sales order now. I need to make a comparison of quantity user has put with quantity available in inventory and if the user input quantity is more than in stock, I have to send notification in html page to lower the quantity or remove it. Please help me on this.

Rookie Answered on February 12, 2020.
Add Comment

I suggest you create an array of items and search on those items.
Then you can process the search results matching against your request items by item internalid and compare the quantity.

If the amount requested breaks the rules you’re trying to enforce send back the response and don’t save the SO.

Rookie Answered on February 13, 2020.
Add Comment

How are you validating the items being added to the HTML form? If you already have a mechanism that brings in those items to the HTML page, you can just include the quantity available field to validate against. In fact you can just filter that dataset by only bringing in the items that have available quantity to sell.

Beginner Answered on February 13, 2020.

Yes, while bringing , I have vaildate the quantity available, but if someone create a new order and submit it after some time, the quantity available will be changed , so i need to validate it before creating order in netsuite as well.

on February 13, 2020.
Add Comment

I have a question: how can i Access Customer Fields  from Sales Order Suitelet. I need to get a value from customer record and update it after sales order is successful.

Rookie Answered on February 13, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor