Suitelet to get price from item record and customer record entity field value

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 * @Description
 *  get price from item record
 */
/*******************************************************************************
 * 
 ********PCIC-834**********
 * 
 * 
 * AUTHOR:JOBIN AND JISMI IT SERVICES LLP
 * 
 * CREATED ON :13/07/2020
 * 
 * 
 * REVISION HISTORY
 *  
 * Revision 1.0 $ 13/07/2020 
 * 
 ******************************************************************************/
 var quan=[];
                    var spe_price=[];
                    var itemobj=[];
                    var itemid='';
                    var table1=[];
                    var result=[];
                    var online_price=[];
                    var tabl_onl=[];
                   
define(['N/error', 'N/record', 'N/search'],
    /**
     * @param {error} error
     * @param {record} record
     * @param {search} search
     */
    function(error, record, search) {

        /**
         * Definition of the Suitelet script trigger point.
         *
         * @param {Object} context
         * @param {ServerRequest} context.request - Encapsulation of the incoming request
         * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
          @Since 2015.2     **/
        var main = {
            onRequest: function(context) {
                try {
                    var itemID = context.request.parameters.itemid;
                    var customerid = context.request.parameters.customerid;
                    var callBackFuncStr = context.request.parameters.callback;
                    log.debug("callBackFuncStr", callBackFuncStr);
                    var priceID = 'price1';
                    logme("itemID:", itemID);

                    var customRecord = record.load ({
                                  type:  record.Type.CUSTOMER,
                                    id: customerid,
                                    isDynamic: true
                                   });
                          

                    var freeShipping = customRecord.getValue({
                        fieldId: 'custentity_jj_free_shipping'
                          });
                    log.debug("freeShipping", freeShipping)
                          

                    if(customerid){
                        var TaxItem= main.isTaxItemName(customerid);
                          log.debug("id2222222", TaxItem);
                          itemobj.push({
                             Taxitem:TaxItem,
                             freeShipping:freeShipping
                          })
                          }

                    if(main.isSpecialItem(itemID)) {
                        log.debug("error", itemID)
                       var itemRecord = record.load ({
                                  type:  'inventoryitem',
                                    id: itemID,
                                    isDynamic: true
                                   });
                       log.debug("itemRecord", itemRecord)
                       

                       var quantityLevels = itemRecord.getMatrixHeaderCount({
                             sublistId: priceID,
                               fieldId: 'price',
                               line:1
                               });
                       log.debug("quantityLevels", quantityLevels)
                                         
                          for(var j=0;j<quantityLevels;j++){
                                log.debug("123")
                                quantityLevelsvalue=itemRecord.getMatrixHeaderValue({
                                sublistId: priceID,
                                 fieldId: 'price',
                                  column: j
                               });
                                log.debug("quantityLevelsvalue2", quantityLevelsvalue)
                                if(quantityLevelsvalue=='0'){
                                   log.debug("yes")
                                   quantityLevelsvalue==1;
                                   quan.push(quantityLevelsvalue)
                                  
                               }
                               else{
                                quan.push(quantityLevelsvalue)
                               }
                                if(quantityLevelsvalue!=null){
                                    log.debug("2345")
                                    var i=j+1;
                                     itemBasePrice1 = itemRecord.getSublistValue({
                                    fieldId: "price_"+i+"_",
                                    sublistId: 'price1',
                                    line: 5
                                });
                                      onlineprice = itemRecord.getSublistValue({
                                    fieldId: "price_"+i+"_",
                                    sublistId: 'price1',
                                    line: 6
                                });
                                    log.debug("itemBasePrice1", itemBasePrice1)
                                    log.debug("onlineprice", onlineprice)
                                    spe_price.push(itemBasePrice1)
                                    online_price.push(onlineprice)
                                    
                                }

                            }
                        
                         
                         log.debug("quan012",quan[0])
                         if(quan[0]=='0'){
                          quan[0]=1;  
                         }
                        
                        spe_price = spe_price.filter(function(e){return e});
                         quan = quan.filter(function(e){return e});
                         online_price=online_price.filter(function(e){return e});
                        log.debug("spe_price1", spe_price)
                        log.debug("quan", quan) 
                        log.debug("online_price", online_price)
                        
                        
                       
                      if(quan.length==1){
                            log.debug("length is one")
                            log.debug("spe_price[0]",spe_price[0])
                            
                            itemobj.push({
                                itemid:itemID,
                                specialprice:spe_price[0],
                                onlineprice:online_price[0],
                                freeShipping:freeShipping
                            });
                          log.debug("itemobj",itemobj) 
                        
                          

                        }
                        else{
                             table1=['quantity','price',quan[0],spe_price[0],quan[1],spe_price[1],quan[2],spe_price[2],quan[3],spe_price[3],quan[4],spe_price[4]]
                        table1 = table1.filter(function(e){return e});
                        tabl_onl=[online_price[0],online_price[1],online_price[2],online_price[3],online_price[4]]
                        tabl_onl = tabl_onl.filter(function(e){return e});
                        log.debug("table1", table1)
                            itemobj.push({
                                itemid:itemID,
                                table1:table1,
                                tabl_onl:tabl_onl,
                                freeShipping:freeShipping
                                
                            });
                          log.debug("itemobj",itemobj) 
                        
                          
                   }
                    var strJson = callBackFuncStr +  '(\'' + JSON.stringify(itemobj) + '\')';
                            context.response.write(strJson); 
                            log.debug("strJson",strJson)  

                    }else{
                        var strJson = callBackFuncStr +  '(\'' + JSON.stringify(itemobj) + '\')';
                        context.response.write(strJson); 
                        log.debug("strJson",strJson)  
                    }
                 
              
                 } 
                 catch (e) {
                    // logme('err@callback', e);
                    result = false;
                    var strJson = callBackFuncStr + '(\'' + JSON.stringify({ result: result }) + '\')';
                    context.response.write(strJson);
                }
            },
            isTaxItemName: function(customerid) {
                var taxitemId = [];
                var customerSearchObj = search.create({
                    type: "customer",
                    filters:
                    [
                        ["internalid","anyof",customerid]
                    ],
                    columns:
                    [
                       search.createColumn({name: "taxitem", label: "Tax Item"}),
                       search.createColumn({name: "billstate", label: "Billing State/Province"}),
                       search.createColumn({name: "internalid", label: "Internal ID"})
                    ]
                 });
                 var searchResultCount = customerSearchObj.runPaged().count;
                 log.debug("customerSearchObj result count",searchResultCount);
                 customerSearchObj.run().each(function(result){
                    log.debug("result22222",result); 
                    
                    var taxitem = result.getText({name: "taxitem"});
                    var state=result.getText({name: "billstate"});
                    if(taxitem){
                        taxitem=taxitem;
                    }
                    else{
                       
                        if(state == "AB"){taxitem="AB - GST Only";}
                        if(state == "NB"){taxitem="NB - HST";}
                        if(state == "NL"){taxitem="NL - HST";}
                        if(state == "NS"){taxitem="NS - HST";}
                        if(state == "NT"){taxitem="NT - GST Only";}
                        if(state == "NU"){taxitem="NU - GST Only";}
                        if(state == "ON"){taxitem="ON - HST";}
                        if(state == "PE"){taxitem="PE - HST";}
                        if(state == "BC"){taxitem="BC - PST & GST";}
                        if(state == "MB"){taxitem="MB - RST (PST) & GST";}
                        if(state == "SK"){taxitem="SK - PST & GST";}
                        if(state == "QC"){taxitem="QC - QST (PST) & GST";}
                        if(state == "YT"){taxitem="YT - GST Only";}
                    }
                    taxitemId.push({
                                taxitem:taxitem,
                                
                                
                            });
                   
                    return true;
                 });
                  log.debug("result2222222",taxitemId);
                return taxitemId[0];
    
                },
            isSpecialItem: function(itemID) {

                var itemSearchObj = search.create({
                    type: "item",
                    filters: [
                        ["category", "anyof", "69145"], //SPECIAL CATEGORY
                        "AND",
                        ["internalidnumber", "equalto", itemID]
                    ],
                    columns: [
                        search.createColumn({
                            name: "itemid",
                            sort: search.Sort.ASC,
                            label: "Name"
                        }),
                        search.createColumn({ name: "internalid", label: "Internal ID" })
                        
                    ]
                }).run().getRange({
                    start: 0,
                    end: 2
                });

                if (itemSearchObj.length > 0) {
                    log.debug("is special")
                    return true;
                }
            
                return false;

            }

        };
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                    NOERROR = false;
                    
                }
            }
        };
        return main;

    });

/*******************************************************************************
 * Log these data
 * 
 * @param title
 * @param details
 * @returns 
 */
function logme(title, details) {
    log.debug({
        title: title,
        details: details
    });
}

Leave a comment

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