Push Item Fulfilment data to numerik

Pushing SO when deleting the item fulfillment

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/******************************************************************************
 * Date : 19 July 2021
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : Pushing SO when deleting the itemfulfillment
 *
 ******************************************************************************
 * DEPLOYMENT : ITEMFULFILLMENT
 * This script summarises on pushing SO when deleting the itemfulfillment
 *
 * SCENARIO: On deleting IF from netsuite, the coreesponding SO is pushed to numerik if SO status is in pending fulfillment.
 * If SO customer is not is numerik, the customer is also pushed to numerik
 *
 * *****************************************************************************
 * REVISION HISTORY
 *
 * Revision 1.0 ${19 July 2021} JJ0131 : created
 * Revision 2.0 ${28 Sep 2021}  JJ0131 : Updated
 */
define(['N/https', 'N/record', 'N/search', 'N/email', 'N/runtime'],
    /**
     * @param{https} https
     * @param{record} record
     * @param{search} search
     */
    (https, record, search, email, runtime) => {


        const afterSubmit = (scriptContext) => {
            try {
                var CASUB = 11,
                    CASUB2 = 14;
                var header = [];
                var IFrec = scriptContext.newRecord;
                var IFtranid = IFrec.getValue({fieldId: 'tranid'});
                header['ApiKey'] = 'GcKVrpepGj1kwkIKNi5eNBNdnLuHgdEp3e972WrJ+qo=';
                header['Content-Type'] = 'application/json';
                var STATUS = 'pendingFulfillment';
                if (scriptContext.type === scriptContext.UserEventType.DELETE) {
                    //When IF is deleted from netsuite, the corresponding SO is pushed to numerik if status is in pending fulfillment
                    var IFrecord = scriptContext.oldRecord;
                    var recordid = IFrecord.id;
                    log.debug("record", recordid)
                    var createdSO = IFrecord.getValue({
                        fieldId: 'createdfrom'
                    });
                    log.debug("CreatedSO", createdSO);
                    var SOseacrh = search.create({
                        type: search.Type.SALES_ORDER,
                        columns: ['status', 'internalid'],
                        filters: ['internalid', 'is', createdSO]
                    });
                    var status, internalid;
                    SOseacrh.run().each(function (result) {
                        status = result.getValue({
                            name: 'status'
                        });
                        internalid = result.getValue({
                            name: 'internalid'
                        });
                        return true;
                    });
                    log.debug("STATUS", status);


                    /**If the Created SO status is in pending fulfillment, The sales data is pushed to numerik**/
                    if (status == STATUS) {
                        var SO_record = record.load({
                            type: record.Type.SALES_ORDER,
                            id: internalid,
                            isDynamic: true
                        });
                        var tranid = SO_record.getValue({fieldId: 'tranid'});
                        //Getting SO data from numerik
                        var SOresponse = https.get({
                            url: 'https://numerik-publicapi-prod.azurewebsites.net/api/SalesData/TransactionId/' + tranid,
                            headers: header
                        });
                        //Checking SO data is present in numerik
                        if (SOresponse.code == 200) {
                            var SOdelete = https.delete({
                                url: 'https://numerik-publicapi-prod.azurewebsites.net/api/SalesData/TransactionId/' + tranid,
                                headers: header
                            });
                            log.debug("CODE", SOdelete.code)
                        }


                        /*****Pushing sales data to numerik******/
                        function SalesData() {
                            try {
                                var subs = SO_record.getValue({fieldId: 'subsidiary'});
                                if (subs == CASUB || subs == CASUB2) {
                                    var custid = SO_record.getValue({fieldId: 'entity'});
                                    var cuname = SO_record.getText({fieldId: 'entity'});
                                    var tranid = SO_record.getValue({fieldId: 'tranid'});
                                    var tranDate = SO_record.getValue({fieldId: 'trandate'});
                                    var salesrep = SO_record.getValue({fieldId: 'salesrep'});
                                    var numLines = SO_record.getLineCount({
                                        sublistId: 'item'
                                    });
                                    log.debug("Lines", numLines);
                                    var productObj = [];
                                    var salesDataObj = [];
                                    for (var i = 0; i < numLines; i++) {
                                        var itemtype = SO_record.getSublistValue({
                                            sublistId: 'item',
                                            fieldId: 'itemtype',
                                            line: i
                                        });
                                        if (itemtype !== 'Group' && itemtype !== 'EndGroup') {
                                            var qtycommit = SO_record.getSublistValue({
                                                sublistId: 'item',
                                                fieldId: 'quantitycommitted',
                                                line: i
                                            });
                                            log.debug("Qty", qtycommit);
                                            if (qtycommit == null || qtycommit == '') {
                                                qtycommit = 0;
                                            }
                                            if (qtycommit > 0) {
                                                var item = SO_record.getSublistValue({
                                                    sublistId: 'item',
                                                    fieldId: 'item',
                                                    line: i
                                                });
                                                log.debug("ITEM", item);
                                                var quantity = SO_record.getSublistValue({
                                                    sublistId: 'item',
                                                    fieldId: 'quantity',
                                                    line: i
                                                });
                                                var rate = SO_record.getSublistValue({
                                                    sublistId: 'item',
                                                    fieldId: 'rate',
                                                    line: i
                                                });
                                                if (rate == null || rate == "") {
                                                    rate = 0;
                                                }
                                                var line = SO_record.getSublistValue({
                                                    sublistId: 'item',
                                                    fieldId: 'line',
                                                    line: i
                                                });
                                                var amount = SO_record.getSublistValue({
                                                    sublistId: 'item',
                                                    fieldId: 'amount',
                                                    line: i
                                                });
                                                if (amount == null || amount == "") {
                                                    amount = 0;
                                                }
                                                var costestimate = SO_record.getSublistValue({
                                                    sublistId: 'item',
                                                    fieldId: 'costestimate',
                                                    line: i
                                                });


                                                if (costestimate == null || costestimate == '')
                                                    costestimate = 0;
                                                var estgrossprofict = amount - costestimate;
                                                var gross = estgrossprofict.toFixed(2);
                                                var itemfields = search.lookupFields({
                                                    type: 'item',
                                                    id: item,
                                                    columns: ['itemid', 'displayname', 'custitem19', 'internalid']
                                                });
                                                var itemgroup = '';
                                                if (itemfields.custitem19[0]) {
                                                    itemgroup = itemfields.custitem19[0].text;
                                                }
                                                var intid = itemfields.internalid;
                                                log.debug('itemid', intid);
                                                var itemid = itemfields.itemid;
                                                log.debug('itemname', itemid);
                                                var itemdesc = itemfields.displayname;
                                                var Obj = {};
                                                Obj[i] = {};
                                                Obj[i].numerikId = 0;
                                                Obj[i].id = item.toString();
                                                Obj[i].name = itemid;
                                                Obj[i].description = itemdesc;
                                                Obj[i].productGroup = itemgroup;
                                                Obj[i].productManager = '';
                                                productObj.push(Obj[i]);


                                                var salesObj = {};
                                                salesObj[i] = {};
                                                salesObj[i].numerikId = 0;
                                                salesObj[i].transactionId = tranid;
                                                salesObj[i].dateOfTransaction = tranDate;
                                                salesObj[i].lineId = line.toString();
                                                salesObj[i].quantity = qtycommit;
                                                salesObj[i].unitSalesPrice = rate;
                                                salesObj[i].lineTotal = amount;
                                                salesObj[i].grossProfitDollars = gross;
                                                salesObj[i].customerId = custid;
                                                salesObj[i].customerName = cuname;
                                                salesObj[i].productId = itemid;
                                                salesObj[i].productDescription = itemdesc;
                                                salesObj[i].productGroup = itemgroup;
                                                salesObj[i].salesRep = salesrep;
                                                salesObj[i].consultant = ''; //transType
                                                salesObj[i].projectId = '';
                                                salesObj[i].projectName = '';
                                                salesDataObj.push(salesObj[i]);
                                            }
                                        }
                                    }
                                    var prodresponse = https.post({
                                        url: 'https://numerik-publicapi-prod.azurewebsites.net/api/Products',
                                        body: JSON.stringify(productObj),
                                        headers: header
                                    });
                                    log.debug("PRDCODE", prodresponse.code);
                                    var salesresponse = https.post({
                                        url: 'https://numerik-publicapi-prod.azurewebsites.net/api/SalesData',
                                        body: JSON.stringify(salesDataObj),
                                        headers: header
                                    });
                                    log.debug("SALECODE", salesresponse.code)
                                } else {
                                    log.debug("Invalid Subsidiary")
                                }
                            } catch (e) {
                                log.debug({title: e.title, details: e.message});
                                log.error({title: e.title, details: e.message});


                            }
                        }


                        SalesData();
                    } else {
                        log.debug("Sales order cannot be pushed");
                    }
                }
            } catch (e) {
                log.debug({title: e.title, details: e.message});
                log.error({title: e.title, details: e.message});
                var script = runtime.getCurrentScript();
                var rec = script.getParameter({
                    name: 'custscript_jj_if_err_email'
                });
                var user = runtime.getCurrentUser();
                var auth = user.id;
                var env = runtime.envType;
                var acnt = runtime.accountId;
                var scriptid = script.id;
                email.send({
                    author: auth,
                    recipients: rec,
                    subject: 'Numerik Sync Failure: IF Record #' + IFtranid,
                    body: 'Account ID: ' + acnt + 'n' +
                        'Environment: ' + env + 'n' +
                        'Script: ' + scriptid + 'n' +
                        'Error Details: ' + e.name + 'n' +
                        'Error Message: ' + e.message
                });
            }


        }


        return {afterSubmit}


    });

Leave a comment

Your email address will not be published. Required fields are marked *