ps_salesorder_sync_to_ns.js

/**
 * @NApiVersion 2.x
 * @NScriptType Restlet
 * @NModuleScope Public
 * @description assert project sync other system SO to  netsuite  
 * @author WZ
 * @copyright netsuite Oracle
 * @version 1.0
 */
define(['N/record', 'N/search', 'N/error', './configInfo.js'],

    function (record, search, error, configInfo) {
   

        var configObj = configInfo.configInfo();
        /**
         * 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(requestParams) {
   

        }

        /**
         * Function called upon sending a PUT 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 doPut(requestBody) {
   


        }


        /**
         * 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) {
   



            var realResults = forSingleSalesOrder(requestBody);
            return realResults
        }



        function forSingleSalesOrder(params) {
   
            var resultObkArr = {
    'create': [], 'update': [], 'error': [] };
            
            log.debug('modual config', configObj)
            try {
   

                var isDuplicate = searchDupSo(params);

                // var singelRes = {}
                if (isDuplicate) {
   
                    var update_so_id = updateSoBySoid(params, isDuplicate);

                    if (update_so_id) {
   
                        resultObkArr.update.push(params.body[configObj.uniqueFieldId.extraInfo])
                    }

                    //  singelRes[success] = params.tranId;

                } else {
   
                    var new_id = CreateSalesOrder(params)
                    log.debug('new_id', new_id)
                    if (-1 == new_id) {
   
                    } else {
   
                        resultObkArr.create.push(params.body[configObj.uniqueFieldId.extraInfo])
                    }


                }
                return resultObkArr;
            } catch (error) {
   
                log.debug('error', error)
                var error_obj = {
   };
                error_obj.name = error.name;
                error_obj.message = error.message;
                error_obj.tranId = params.tranId;

                resultObkArr.error.push(error_obj)
                return resultObkArr;
            }


        }

        /**
               * 
               * @param {*} requestBody 
               * @description search so by unique identifier
               * @returns internalId_update :false or internal id
               */
        function searchDupSo(requestBody) {
   
            
            var salesorderSearchObj = search.create({
   
                type: "salesorder",
                filters:
                    [
                        [configObj.uniqueFieldId.extraInfo, "is", requestBody.body[configObj.uniqueFieldId.extraInfo]],
                        "AND",
                        ["type", "anyof", "SalesOrd"],
                        "AND",
                        ["mainline", "is", "T"]
                    ],
                columns:
                    [
                        search.createColumn({
    name: "internalid", label: "Internal ID" })
                    ]
            });
            var searchResultCount = salesorderSearchObj.runPaged().count;

            log.debug("salesorderSearchObj result count", searchResultCount);



            if (searchResultCount) {
   
                var internalId_update;
                salesorderSearchObj.run().each(function (result) {
   
                    // .run().each has a limit of 4,000 results
                    internalId_update = result.id;
                    log.debug('internalId_update', internalId_update)
                    return true;
                });
                return internalId_update;
            } else {
   
                return false

            }

        }
        function CreateSalesOrder(params) {
   
            
            /**
             * create a dynamic salesorder record
             */
            var soRecord = record.create({
   
                type: 'salesorder',
                isDynamic: true
            })


            for (var key in params.body) {
   
                // No. 1 custom form
                if ('customform' == key) {
   
                    /**
                     * if other sys wants set form should set this key
                     * internal id not provide
                     */
                    if (configObj[key].name) {
   
                    } else {
   
                        soRecord.setValue({
   
                            fieldId: key,
                            value: params.body[key]
                        });

                    }

                } else

                    // No.2 customer field
                    if ('entity' == key) {
   
                        /**
                * if ns customer internal id  not  provide  
                */
                        if (configObj[key].name) {
   
                            // search ns customer id  by other system customer id
                            var customerSearchObj = search.create({
   
                                type: "customer",
                                filters:
                                    [
                                        [configObj[key].extraInfo, "is", params.body[key]],
                            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值