Adjust item quantity to sell the item lengthwise

UN-309 Pipe item customization

The client has certain items which are sold to customers lengthwise, the length requirement of each customer is different. Instead of adding each item in different length as separate item master client script customization is used. The unit of measure of the item is set as millimetres, two custom columns – length in mm and pieces are added in sales transactions. The user can input the required length in the length in mm column and required quantity in the pieces column. Then the corresponding length is set to the quantity column.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

/*******************************************************************************
 * Unicorn
 * Script Description
 * This script will calculate item weight and quantity according to length
 * **************************************************************************
 * 
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * Date Created : 26-October-2018
 *
 ******************************************************************************/
define(['N/record', 'N/search'],

    function (record, search) {
 

        /**
         * Function to be executed when field is changed.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.sublistId - Sublist name
         * @param {string} scriptContext.fieldId - Field name
         * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
         * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
         *
         * @since 2015.2
         */
        function fieldChanged(scriptContext) {
            if ((scriptContext.sublistId == "item") && (scriptContext.fieldId == "custcol_jj_item_pieces")) {
    var recordtype=scriptContext.currentRecord.type;
    if(recordtype=='workorder')
    return;
                 var length = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_length_mm'
                    });
                    log.debug("length",length);
                 
          // continue only if length is not empty
                    if((length=='')||(length==null)||(length==undefined))
                        return;
                     length=parseFloat(length)/1000;
          var pcs = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_item_pieces'
                    });
                    log.debug("pcs",pcs);
          // continue only if pcs is not empty
                    if((pcs=='')||(pcs==null)||(pcs==undefined))
                        return;
         // find new quantity
                var newquantity=length*pcs;
                log.debug("newquantity",newquantity);
                 scriptContext.currentRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                    value: newquantity,
                    ignoreFieldChange: false
                    });

                 var item = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                    });
        // find weight
                var kgperm=search.lookupFields({
                type: search.Type.INVENTORY_ITEM,
                id: item,
                columns: ['custitem_jj_kg_m']
                });
                
                kgperm=kgperm.custitem_jj_kg_m;
                 if((kgperm=='')||(kgperm==null)||(kgperm==undefined))
                        return;
                var weight=(parseFloat(kgperm)*parseFloat(newquantity)).toFixed(2);
                log.debug("weight",weight);
                scriptContext.currentRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_weight',
                    value: weight,
                    ignoreFieldChange: false
                    });
        }
           
    if(scriptContext.fieldId=='billofmaterialsrevision'){
        console.log("Dd");
        setTimeout(function(){ var recordtype=scriptContext.currentRecord.type;
        log.debug("recordtype",recordtype);
            if(recordtype!='workorder')
                return;
        try{
        var currentRec=scriptContext.currentRecord;
        
        var bomrevision=currentRec.getValue({
                fieldId:'billofmaterialsrevision'
        });
        log.debug("bomrevision",bomrevision);
            if(bomrevision=='')
                return;
        var objRecord = record.load({
                    type: record.Type.BOM_REVISION, 
                    id: bomrevision,
                    isDynamic: true,
                });
             var numLines = objRecord.getLineCount({
                 sublistId: 'component'
                });
            var length_array=[];
            if(numLines<1)
                return;
            //var length_array=[];
            for(var i=0;i<numLines;i++){
               var length = objRecord.getSublistValue({
                    sublistId: 'component',
                    fieldId: 'custrecord_jj_bom_length',
                    line: i
                });
               length_array.push(length); 
            }
            log.debug("length_array",length_array);
            
            var linecount=currentRec.getLineCount({
                 sublistId: 'item'
                });
            log.debug("linecount",linecount);
            if(linecount<1)
                return;
            for(var j=0;j<linecount;j++){
                log.debug("length_array[i]",length_array[j]);
                if(length_array[j]!=''){
                   currentRec.selectLine({
                    sublistId: 'item',
                    line: j
                });
                currentRec.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_jj_length_mm',
                value: length_array[j],
                ignoreFieldChange: true
                });
                currentRec.commitLine({
                sublistId: 'item'
                }); 
                }
                
            }
        
        }catch(e){
            log.debug("err",e);
        } }, 3000);
        
        }
            
            if ((scriptContext.sublistId == "item")&&(scriptContext.fieldId == "quantity")) {
                try{
                 var records = scriptContext.currentRecord;
                 var recordtype = scriptContext.currentRecord.type;
                 if (recordtype == 'workorder')
                     return;
                    
                var quantity = records.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity'
                });
                    log.debug("quantity",quantity);
                var itemid = records.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                });
               var itemtype = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'itemtype'
                    });
                
            if(itemtype=='InvtPart'){
                
                var weight = search.lookupFields({
                type: search.Type.INVENTORY_ITEM,
                id: itemid,
                columns: ['custitem_jj_kg_pc']
                });
               
                weight=weight.custitem_jj_kg_pc;
                if((weight!='')||(weight!=null)||(weight!=undefined)){
                weight=weight*quantity;
                scriptContext.currentRecord.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_jj_weight',
                value:weight,
                ignoreFieldChange: true
                });
                }
                
            }
            else if(itemtype=='Assembly'){
                var weight = search.lookupFields({
                type: search.Type.ASSEMBLY_ITEM,
                id: itemid,
                columns: ['custitem_jj_kg_pc']
                });  
                log.debug("weight",weight);
                weight=weight.custitem_jj_kg_pc;
               if((weight!='')||(weight!=null)||(weight!=undefined)){
                   weight=weight*quantity;
                scriptContext.currentRecord.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_jj_weight',
                value:weight,
                ignoreFieldChange: true
                });
                }
            }
            else{
                
            }
             }catch(e){
                    log.debug("err",e)
             }
                
        }
    }

    
    /**
     * Function to be executed after line is selected.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     */
    function lineInit(scriptContext) {
        var recordtype=scriptContext.currentRecord.type;
        if(recordtype=='workorder')
                return;
         if (scriptContext.sublistId == 'item') {
             try{
            var itemtype = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'itemtype'
                    });
             if(itemtype!='InvtPart'){
                document.getElementById('custcol_jj_length_mm_formattedValue').disabled = true;
                document.getElementById('custcol_jj_item_pieces_formattedValue').disabled = true;
                document.getElementById('quantity_formattedValue').disabled = false;
                return;
            }
            var itemid = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                    });
            
            if((itemid=='')||(itemid==null)||(itemid==undefined))
               return;
            var itemgroup = search.lookupFields({
                type: search.Type.INVENTORY_ITEM,
                id: itemid,
                columns: ['custitem_jj_item_group']
                });
           
            if(itemgroup.custitem_jj_item_group!=''){
            itemgroup=itemgroup.custitem_jj_item_group[0].text;
            //log.debug(" post itemgroup",itemgroup);
            }else{
                itemgroup='';
            }
          
           if((itemgroup=='')||(itemgroup==null)||(itemgroup==undefined)||(itemgroup=='Others')){
                document.getElementById('custcol_jj_length_mm_formattedValue').disabled = true;
                document.getElementById('custcol_jj_item_pieces_formattedValue').disabled = true;
                document.getElementById('quantity_formattedValue').disabled = false;
            }
            else{
                document.getElementById('custcol_jj_length_mm_formattedValue').disabled = false;
                document.getElementById('custcol_jj_item_pieces_formattedValue').disabled = false;
                document.getElementById('quantity_formattedValue').disabled = true;
            }
        }catch(e){
            log.debug("err in lineinit",e);
        }
             
     }
    
    }
  /**
     * Function to be executed when field is slaved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     *
     * @since 2015.2
     */
    function postSourcing(scriptContext) {
        var recordtype=scriptContext.currentRecord.type;
        if(recordtype=='workorder')
                return;
        if ((scriptContext.sublistId == 'item')&&(scriptContext.fieldId=='item')) {
        try{
             var itemid = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                    });
            
             if((itemid!='')||(itemid!=null)||(itemid!=undefined))
             {
            var itemtype = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'itemtype'
                    });
            log.debug("itemtype",itemtype);
            if(itemtype=='InvtPart'){
                var weight = search.lookupFields({
                type: search.Type.INVENTORY_ITEM,
                id: itemid,
                columns: ['custitem_jj_kg_pc']
                });
                log.debug("weight",weight);
                weight=weight.custitem_jj_kg_pc;
                if((weight!='')||(weight!=null)||(weight!=undefined)){
                scriptContext.currentRecord.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_jj_weight',
                value:weight,
                ignoreFieldChange: true
                });
                }
                
            }
            else if(itemtype=='Assembly'){
                var weight = search.lookupFields({
                type: search.Type.ASSEMBLY_ITEM,
                id: itemid,
                columns: ['custitem_jj_kg_pc']
                });  
                log.debug("weight",weight);
                weight=weight.custitem_jj_kg_pc;
               if((weight!='')||(weight!=null)||(weight!=undefined)){
                scriptContext.currentRecord.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_jj_weight',
                value:weight,
                ignoreFieldChange: true
                });
                }
            }
            else{
                
            }
                
             
          }
            
            
        }catch(e){
                log.debug("err set weight",e);
        }
            
            
        try{
             var itemtype = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'itemtype'
                    });
            if(itemtype!='InvtPart'){
                document.getElementById('custcol_jj_length_mm_formattedValue').disabled = true;
                document.getElementById('custcol_jj_item_pieces_formattedValue').disabled = true;
                document.getElementById('quantity_formattedValue').disabled = false;
                return;
            }
            var itemid = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item'
                    });
            
            if((itemid=='')||(itemid==null)||(itemid==undefined))
               return;
            var itemgroup = search.lookupFields({
                type: search.Type.INVENTORY_ITEM,
                id: itemid,
                columns: ['custitem_jj_item_group']
                });
           
            if(itemgroup.custitem_jj_item_group!=''){
            itemgroup=itemgroup.custitem_jj_item_group[0].text;
            //log.debug(" post itemgroup",itemgroup);
            }else{
                itemgroup='';
            }
          
            
            if((itemgroup=='')||(itemgroup==null)||(itemgroup==undefined)||(itemgroup=='Others')){
                document.getElementById('custcol_jj_length_mm_formattedValue').disabled = true;
                document.getElementById('custcol_jj_item_pieces_formattedValue').disabled = true;
                document.getElementById('quantity_formattedValue').disabled = false;
            }
            else{
                document.getElementById('custcol_jj_length_mm_formattedValue').disabled = false;
                document.getElementById('custcol_jj_item_pieces_formattedValue').disabled = false;
                document.getElementById('quantity_formattedValue').disabled = true;
            }
            
        }catch(e){
            log.debug("err in postsourcing",e);
        }
             
     }

    }
     /**
     * Validation function to be executed when record is saved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @returns {boolean} Return true if record is valid
     *
     * @since 2015.2
     */
    function saveRecord(scriptContext) {
         var recordtype=scriptContext.currentRecord.type;
        if(recordtype=='workorder')
                return true;
        try{
        var numLines = scriptContext.currentRecord.getLineCount({
        sublistId: 'item'
        });
        var totalweight=0.0;
        for(var i=0;i<numLines;i++){
            var weight = scriptContext.currentRecord.getSublistValue({
                                     sublistId: 'item',
                                     fieldId: 'custcol_jj_weight',
                                     line: i
                                    });
            log.debug("weight",weight);
            if((weight=='')||(weight==null)||(weight==undefined))
                weight=0;
            totalweight=parseFloat(totalweight)+parseFloat(weight);
        }
        log.debug("totalweight",totalweight);
        scriptContext.currentRecord.setValue({
            fieldId: 'custbody_jj_total_weight',
            value: totalweight,
            ignoreFieldChange: true
            });
        }catch(e){
            log.debug("error",e);
        }
        return true;
    }
    
     
     
        return {
            fieldChanged: fieldChanged,
            lineInit: lineInit,
            postSourcing: postSourcing,
            saveRecord:saveRecord
        };

    });

Leave a comment

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