Skip to content

Instantly share code, notes, and snippets.

@cafeasp
Created July 15, 2019 17:08
Show Gist options
  • Save cafeasp/30318813c65199e167a94431ab8ea555 to your computer and use it in GitHub Desktop.
Save cafeasp/30318813c65199e167a94431ab8ea555 to your computer and use it in GitHub Desktop.
NetSuite - Suite Script Create Item Fulfillment
/**
* @NApiVersion 2.x
* @NScriptType Restlet
* @NModuleScope SameAccount
*/
define(['N/search', 'N/record', 'N/util', './momentjs.js', './cafeaspMagento.js'],
/**
* @param {search} search
* @param {record} record
* @param {util} util
* @param {moment} moment
* @param {cafeaspmagento} cafeaspmagento
*/
function (search, record, util, moment, cafeaspmagento) {
/**
* Function called upon sending a GET request to the RESTlet.
*
* @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.1
*/
function doGet() {
}
/**
* Function called upon sending a PUT request to the RESTlet.
* FOR CREATING ITEM FULFILLMENT
* @param {string | Object} requestBody - The HTTP request body; request body will be passed into function as a string when request Content-Type is 'text/plain'
* or parsed into an Object when request Content-Type is 'application/json' (in which case the body must be a valid JSON)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.2
*/
function doPut(requestBody) {
if (requestBody) {
var trackingNumber = '123';
var shipDate = '12/12/2019';
var status = salesOrderStatus(salesOrderid);
if (status == 'Pending Fulfillment') {
//create IF
var fulfillment = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: salesOrderid,
toType: record.Type.ITEM_FULFILLMENT
});
fulfillment.setValue({ fieldId: 'shipstatus', value: 'C' });
fulfillment.setValue({ fieldId: 'custbody14333', value: trackingNumber });
//line below can be improved
fulfillment.setValue({ fieldId: 'trandate', value: new Date(moment(shipDate).format('M/D/YYYY')) });
var fulfillmentLineCount = fulfillment.getLineCount({
sublistId: 'item'
});
for (var i = 0; i < fulfillmentLineCount; i++) {
fulfillment.setSublistValue({
sublistId: 'item',
line: i,
fieldId: 'itemReceive',
value: true
});
fulfillment.setSublistValue({
sublistId: 'item',
line: i,
fieldId: 'itemIsFulfilled',
value: true
});
}
var itemFulfillmentId = fulfillment.save({
enableSourcing: true,
ignoreMandatoryFields: false
});
}
}
return "ok";
}
/**
* Function called upon sending a POST request to the RESTlet.
*
* @param {string | Object} requestBody - The HTTP request body; request body will be passed into function as a string when request Content-Type is 'text/plain'
* or parsed into an Object when request Content-Type is 'application/json' (in which case the body must be a valid JSON)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.2
*/
function doPost(requestBody) {
return "ok";
}
/**
* Function called upon sending a DELETE request to the RESTlet.
*
* @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.2
*/
function doDelete(requestParams) {
}
return {
'get': doGet,
put: doPut,
post: doPost,
'delete': doDelete
};
});
@jbyte2009
Copy link

Question about the following line of logic: var status = salesOrderStatus(salesOrderid);

Is there a missing function called, salesOrderStatus?

Please provide if this is the case.

Thanks.

@cafeasp
Copy link
Author

cafeasp commented Feb 15, 2023

Hello, not the same function that I had but it will give you an idea.

function findSalesOrderInternalId(salesOrderNumber) {
var salesOrderInternalId = '';
var salesorderSearchObj = search.create({
type: "salesorder",
filters:
[
["type", "anyof", "SalesOrd"],
"AND",
["transactionnumber", "is", salesOrderNumber],
"AND",
["mainline", "is", "T"]
],
columns:
[
search.createColumn({ name: "internalid", label: "Internal ID" })
]
});
var searchResultCount = salesorderSearchObj.runPaged().count;
log.debug("salesorderSearchObj result count", searchResultCount);
var resultSet = salesorderSearchObj.run();
var result = resultSet.getRange({ start: 0, end: 1 });

    log.debug('search result', result);
    if (searchResultCount > 0) {
        salesOrderInternalId = result[0].getValue({ name: 'internalid' });
    }

    log.debug('internal id OC', salesOrderInternalId);
    return salesOrderInternalId;
}

another idea is to use the search.lookupFields call

columns, you can add the fields you need.

var fieldLookUp = search.lookupFields({
type: search.Type.SALES_ORDER,
id: '87',
columns: ['entity', 'subsidiary', 'name', 'currency','status']
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment