Display Item Stock in all location when an Item is added in Sales Order

Jira Code: PROF-11 Item stock in location

When an Item is added in the sales order if that item is back ordered in the sales order location an alert will be shown indicating the item is not in stock in that particular location. However the item may have stock in a different location.If the user has to know which location have the item stock, they need to navigate to item record. In order to avoid this a client script is used which shows the stock in all the locations when an item is added in the sales order.

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

/**
 * Script Description
 * This  script will show item stock at all locations
 */
/*******************************************************************************
 * Proficium
 * **************************************************************************
 * 
 * Date: 08/01/2018
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 ******************************************************************************/
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) {
    	try{
    		
    	
    	    	if((scriptContext.sublistId=='item')&&(scriptContext.fieldId=='item')){
    	    		var record_so=scriptContext.currentRecord;

    	        	var primary_location='Proficium';
    	        	console.log("primary_location",primary_location);

    		var item_id = scriptContext.currentRecord.getCurrentSublistValue({
    		    sublistId: 'item',
    		    fieldId: 'item'
    		});
    		console.log("item_id",item_id);

    		
    		var item_record=record.load({
    		    type: record.Type.INVENTORY_ITEM, 
    		    id: item_id,
    		    isDynamic: true,
    		});
    		var numLines = item_record.getLineCount({
    		    sublistId: 'locations'
    		});
    		var quantityarray=new Array;
    		var locationarray=new Array;
    		
    		var flag=0;
    		for(var i=0;i<numLines;i++){
    			
    			var quantity=item_record.getSublistText({
    			    sublistId: 'locations',
    			    fieldId: 'quantityavailable',
   			    line: i
    			});
    			
    			log.debug({
    			    title: 'quantity', 
    			    details: quantity
   			    });
    			
    			var location=item_record.getSublistText({
  			    sublistId: 'locations',
    			    fieldId: 'location_display',
   			        line: i
   			    });
    			
    			log.debug({
    			    title: 'location', 
    			    details: location
   			    });
    			if((location!='Singapore')&&(location!='MC')&&(location!='Defective')){
    				quantityarray.push(quantity);
       			    locationarray.push(location);
    			}
    			
   			
   			if((location==primary_location)&&(quantity==0)){
   				flag=1;
   			}
   		}
    		
    		
    		
    		var locations='<html> <body> <table style="width:60%"> <tr> <th><b>Location</b></th> <th><b>Quantity Available</b></th></tr>';
    		
    		for(var j=0;j<locationarray.length;j++){
    			locations+='<tr> <td>'+locationarray[j]+'</td> <td>'+quantityarray[j]+'</td></tr>';
    		}
    		locations+='</table> </body> </html>';
    		
    		if(flag==1){
    			try {
        			
        			var item_location=scriptContext.currentRecord.setValue({
            		    fieldId: 'custbody_jj_item_location',
            		    value: locations,
            		    ignoreFieldChange: true
            		});
        			
        			var currentLocation =document.getElementById("custbody_jj_item_location_val").innerHTML;
        			
        			document.getElementById("custbody_jj_item_location_val").innerHTML=locations;
        		
    			} catch (e) {
    				console.log('e@setting field value'+ e.message);
    			}
    		}
    		else{
    			document.getElementById("custbody_jj_item_location_val").innerHTML="";
    		}
    		
    	}
    }
    	catch(err){
     	   console.log(err.message);
    	}
    }

    return {
    	fieldChanged: fieldChanged
       
    };
    
});

Leave a comment

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