Jira Code: MISC-147
Woocommerce API to fetch the orders from Woocommerce. From the fetched products we perform the updation for the test order given. The customer email available in the Woocommerce is Map to NetSuite customer email.
We are using the NetSuite customer email and Woocommerce customer email to match the customer in Woocommerce to customer in Netsuite. If the customer is present in NetSuite for the corresponding customer email in Woocommerce, it loads the customer and updates customer details if any and then sales order is generated. In the else part, if the customer is not present, creates customer and generates the sales order.
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/file', 'N/format', 'N/http', 'N/https', 'N/record', 'N/runtime', 'N/search'],
/**
* @param {file} file
* @param {format} format
* @param {http} http
* @param {https} https
* @param {record} record
* @param {runtime} runtime
* @param {search} search
*/
function(file, format, http, https, record, runtime, search) {
/**
* Definition of the Scheduled script trigger point.
*
* @param {Object} scriptContext
* @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
* @Since 2015.2
*/
var item = {};
var customer = {};
function execute(scriptContext) {
try{
var response=https.get({
url:'https://www.protecstyle.com/wp-json/wc/v2/orders?per_page=100',
headers:{
'authorization':'Basic Y2tfMDhlYzRiZDI3NjcxY2JlNDc2OGUzZjFjMWI0MTBhNGJkNTJmZTY1Yjpjc18wMTI0Zjg4YTQ0NjI1YzU1YmY4YmU1YjI2ZmQ1MWNkNmRiMzljNTgz',
}
});
/*var count=0;
var sum=0;*/
var recId;
var itemSearch;
var content=JSON.parse(response.body);
/*context.response.write(JSON.stringify(content));*/
log.debug({
title:'response.body',
details:content
});
for(var i=0;i<content.length;i++)
{
if(content[i].id=="5943")
{
if(customer[content[i].billing.email])
{
/*sum++;*/
var CustobjRecord = record.load({
type: record.Type.CUSTOMER,
id:customer[content[i].billing.email],
isDynamic: true,
});
CustobjRecord.setValue({
fieldId: 'isperson',
value: 'T'
});
CustobjRecord.setValue({
fieldId: 'firstname',
value:content[i].billing.first_name
});
CustobjRecord.setValue({
fieldId: 'lastname',
value:content[i].billing.last_name
});
CustobjRecord.setValue({
fieldId: 'email',
value: content[i].billing.email
});
CustobjRecord.selectNewLine({
sublistId: 'addressbook'
});
var myaddressSubrecord = newCustobjRecord.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
myaddressSubrecord.setValue({
fieldId: 'addr1',
value: content[i].billing.address_1
});
myaddressSubrecord.setValue({
fieldId: 'country',
value: content[i].billing.country
});
myaddressSubrecord.setValue({
fieldId: 'addrphone',
value: content[i].billing.phone
});
myaddressSubrecord.setValue({
fieldId: 'city',
value: content[i].billing.city
});
myaddressSubrecord.setValue({
fieldId: 'state',
value: content[i].billing.state
});
myaddressSubrecord.setValue({
fieldId: ' zip',
value: content[i].billing.postcode
});
CustobjRecord.commitLine({
sublistId: 'addressbook'
});
var recId1 = CustobjRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
customer[content[i].billing.email]= recId1;
log.debug({
title:'recId1',
details: recId1
});
}
else{
var custSearch = search.create({
type:search.Type.CUSTOMER,
columns:[{
name: 'email',
},
{
name: 'internalid',
}],
filters: [{
name: 'email',
operator: 'is',
values:[content[i].billing.email]
}]
});
custSearch.run().each(function(result){
customer[content[i].billing.email]=result.getValue({
name:'internalid',
});
return false;
});
if(!customer[content[i].billing.email])
{
/*count++;*/
var newCustomer = record.create({
type: record.Type.CUSTOMER,
isDynamic: true,
});
newCustomer.setValue({
fieldId: 'isperson',
value: 'T'
});
newCustomer.setValue({
fieldId: 'firstname',
value:content[i].billing.first_name
});
newCustomer.setValue({
fieldId: 'lastname',
value:content[i].billing.last_name
});
newCustomer.setValue({
fieldId: 'email',
value: content[i].billing.email
});
newCustomer.selectNewLine({
sublistId: 'addressbook'
});
var myaddressSubrecord = newCustomer.getCurrentSublistSubrecord({
sublistId: 'addressbook',
fieldId: 'addressbookaddress'
});
myaddressSubrecord.setValue({
fieldId: 'addr1',
value: content[i].billing.address_1
});
myaddressSubrecord.setValue({
fieldId: 'country',
value: content[i].billing.country
});
/*myaddressSubrecord.setValue({
fieldId: 'attention',
value: (content[i].shipping.first_name)+" "+(content[i].shipping.last_name)
});*/
myaddressSubrecord.setValue({
fieldId: 'addrphone',
value: content[i].billing.phone
});
myaddressSubrecord.setValue({
fieldId: 'city',
value: content[i].billing.city
});
myaddressSubrecord.setValue({
fieldId: 'state',
value: content[i].billing.state
});
myaddressSubrecord.setValue({
fieldId: ' zip',
value: content[i].billing.postcode
});
newCustomer.commitLine({
sublistId: 'addressbook'
});
recId = newCustomer.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
customer[content[i].billing.email]= recId;
/*createSalesorder(recId);*/
}
log.debug({
title:'recId',
details: recId
});
var len = content[i].line_items.length;
for(var j=0; j<len; j++)
{
if(!item[content[i].line_items[j].sku])
{
itemSearch = search.create({
type:search.Type.INVENTORY_ITEM,
columns:[{
name: 'itemid',
},
{
name:'internalid'
}],
filters: [{
name: 'itemid',
operator: 'is',
values:[content[i].line_items[j].sku]
}]
});
itemSearch.run().each(function(result){
log.debug({
title:' result',
details: result
});
item[content[i].line_items[j].sku] =result.getValue({
name:'internalid',
});
return false;
});
}
}
log.debug({
title:'item',
details:item
});
}
createSalesorder(content[i]);
}
}
log.debug({
title:'itemSearch',
details:itemSearch
});
log.debug({
title:'custSearch',
details:custSearch
});
}
catch(e){
log.debug({
title:'err@Get',
details:e
});
}
}
return {
execute: execute
};
function createSalesorder(orderObj)
{
log.debug({
title:'orderObj,',
details:orderObj
});
log.debug({
title:'customer[orderObj.billing.email]',
details:customer[orderObj.billing.email]
});
var salesObjRecord = record.transform({
fromType: record.Type.CUSTOMER,
fromId:customer[orderObj.billing.email],
toType: record.Type.SALES_ORDER,
isDynamic: true
});
var lineItems = orderObj.line_items;
for(var i=0;i<lineItems.length;i++)
{
salesObjRecord.selectNewLine({
sublistId: 'item'
});
salesObjRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value:parseInt(item[lineItems[i].sku]),
ignoreFieldChange: false,
});
log.debug({
title:'item[lineItems[i].sku]',
details:item[lineItems[i].sku]
});
salesObjRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value:parseInt(orderObj.line_items[i].quantity),
ignoreFieldChange: false
});
salesObjRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount',
value:parseInt(orderObj.line_items[i].price),
ignoreFieldChange: false
});
log.debug({
title:'item[lineItems[i].quantity]',
details:orderObj.line_items[i].quantity
});
salesObjRecord.commitLine({
sublistId: 'item'
});
}
var SalesrecordId=salesObjRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug({
title:'SalesrecordId',
details:SalesrecordId
});
}
});