Post total quantity of items on a sales order body field.

Jira Code: PS 10

This task is to post the total quantity of items ordered in a sales order in the body field.The group components should be excluded from the total but need to include the group items.

Userevent script

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:PARKSLOPE
 * PS -10 Set Qty on sales order
 *************************************************************************
 * Date : 12/02/2019
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description :To set the total no.of Qty on an body field
 * 
 * 
 *  Date created : 12/02/2019
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${12/02/2019} aj : created
 * 			1.1 ${14/02/2019} aj: modified to consider item grp
 * 
 *
 ******************************************************************************/
define(['N/record', 'N/search'],
/**
 * @param {record} record
 * @param {search} search
 */
function(record, search) {
   
    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {string} scriptContext.type - Trigger type
     * @param {Form} scriptContext.form - Current form
     * @Since 2015.2
     */
    function beforeLoad(scriptContext) {

    }

    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {Record} scriptContext.oldRecord - Old record
     * @param {string} scriptContext.type - Trigger type
     * @Since 2015.2
     */
    function beforeSubmit(scriptContext) {

    }

    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {Record} scriptContext.oldRecord - Old record
     * @param {string} scriptContext.type - Trigger type
     * @Since 2015.2
     */
    function afterSubmit(scriptContext) {
    	try{
    		var currentRecord =  scriptContext.newRecord;
    		  var numLines = currentRecord.getLineCount({
    			    sublistId: 'item'
    			});
    		  var totalQty = 0,excludeQty=0;
    			  
    			 
    			   for(var i=0;i<numLines;i++)
    			   {
    			    
    			  var qty = currentRecord.getSublistValue({
        			    sublistId: 'item',
        			    fieldId: 'quantity',
        			    line: i
        			});
    			  if(qty==null || qty==undefined || qty=='null'||qty==''||qty==' ')
    				  qty=0;
			    	
    			    // total qty
    			    totalQty=parseInt(totalQty)+parseInt(qty);
    			    
    			    var type = currentRecord.getSublistValue({
        			    sublistId: 'item',
        			    fieldId: 'itemtype',
        			    line: i
        			});
    			   
    			    
    			    if(type=='Group')
    			    	{

    			    	
    			    	var quantity = currentRecord.getSublistValue({
            			    sublistId: 'item',
            			    fieldId: 'quantity',
            			    line: i
            			});
    			    	if(quantity==null || quantity==undefined || quantity=='null'||quantity==''||quantity==' ')
    			    		quantity=0;
    			    	
    			    	excludeQty=parseInt(excludeQty)+parseInt(quantity);
    			    	

    			    	}
    			
    			 
    			   }
    			  
    			  
    			   var totalQuantity = parseInt(totalQty)-parseInt(excludeQty);
    			 var soRec =  record.load({
    				 type:'salesorder',
    				 id:currentRecord.id
    			 });
    			
    			   soRec.setText({
    				    fieldId: 'custbodyitemqty',
    				    text:totalQuantity.toString(),
    				    ignoreFieldChange: true
    				});
    			   soRec.save({ignoreMandatoryFields: true});
    	}catch(e)
    	{
    		
    		log.error("Err@ onAction FN ",e.message);
    		
    	}

    }

    return {
        beforeLoad: beforeLoad,
        beforeSubmit: beforeSubmit,
        afterSubmit: afterSubmit
    };
    
});

Leave a comment

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