Set item id with ‘DEV’ and Check digit calculation.

Jira Code: AD 4, 5

This task is to put “DEV” in item id field when product life cycle is “Development”.The format of item id should be “DEV”+ InternalID of item record.
Calculate UPC check digit: This task is to calculate UPC code of Sellable UPC,Inner carton UPC, Import Carton UPC, Master carton UPC only when the no.of digits are 8,12,13,14

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME: ADVANTUS
 * AD-52
 * 
 * **************************************************************************
 * Date : 19-03-2019
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : 
 * Date created : 19-03-2019
 *
 * REVISION HISTORY 1.0 aj created for check digit
 * 					1.1 joe added DEV task
 * 					1.2 aj modified DEV task
 *
 ******************************************************************************/
define(['N/record', 'N/search', 'N/ui/dialog', 'N/ui/message'],
/**
 * @param {record} record
 * @param {search} search
 * @param {dialog} dialog
 * @param {message} message
 */
function(record, search, dialog, message) {
    
    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     */
    function pageInit(scriptContext) {
    	
             try {
                
                 if(scriptContext.mode=='create'||scriptContext.mode=='copy')
                	 {
                 var currentRec = scriptContext.currentRecord;
                 var date = new Date();
                 var timeStamp = date.getTime();

                 var itemId = "DEV" + timeStamp;
                 currentRec.setValue({
                     fieldId: 'itemid',
                     value: itemId
                 });
                	 }
               
             } catch (er) {
                 console.log('er', er);
                 log.debug("err @PageInit",er)
                  log.error("err @PageInit",er)
             }

    }

    /**
     * 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) {

    }

    /**
     * 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) {

    }

    /**
     * Function to be executed after sublist is inserted, removed, or edited.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     */
    function sublistChanged(scriptContext) {

    }

    /**
     * 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) {

    }

    /**
     * Validation 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
     *
     * @returns {boolean} Return true if field is valid
     *
     * @since 2015.2
     */
    function validateField(scriptContext) {

    }

    /**
     * Validation function to be executed when sublist line is committed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     */
    function validateLine(scriptContext) {

    }

    /**
     * Validation function to be executed when sublist line is inserted.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     */
    function validateInsert(scriptContext) {

    }

    /**
     * Validation function to be executed when record is deleted.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     */
    function validateDelete(scriptContext) {

    }

    /**
     * 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) {
    	try{
    		 try {
    			// to get the current record
    	    		var curRecord = scriptContext.currentRecord;
                
                  var lifecycleStatus = curRecord.getText({
                     fieldId: 'custitem_adv_product_lifecycle_status'
                 });
               
                 var itemName = curRecord.getValue({
                     fieldId: 'itemid'
                 });
                 var displayName = curRecord.getValue({
                     fieldId: 'displayname'
                 });
                 var isDev=0;
                 if (lifecycleStatus != 'Development') {
                     if (itemName.includes('DEV')) {
                    	 isDev=1;
                         var options = {
                             title: "Error",
                             message: "Item Number is a DEV number. In order to move to a Product Lifecycle Status other than Development, a real Item Number must be assigned (without DEV as a prefix)."
                         };

                         function success(result) {
                             console.log("result: " + result);
                             console.log("err msg");
                         }

                         function failure(reason) {
                             console.log("Failure: " + reason);
                         }

                         dialog.confirm(options).then(success).catch(failure);
                     }
                 } else
                	 
                     console.log('notify else');
             } catch (er) {
                 console.log('error in DEV ', er);
                 log.debug("error in DEV",er);
                 log.error("error in DEV",er);
             }
         
    		// to get the data
    		var checkPoint;
    		//var noOfDigits = 0;
    		
    		
    		// to get the UPC CODE
    		
    		var upcCode=curRecord.getText({
    			fieldId:'upccode'
    		});
    		
    		// to find the total digit
    		
    		var noOfDigits =upcCode.length;
    		
    		
    		var upcDigit=parseInt(upcCode%10);
    		
    		
    		// to get the ignore mandatory field
    		var ignore=curRecord.getValue({
    			fieldId:'custitem_adv_ignore_check_digit'
    		});
    		
    		if(ignore!=false){
    			
    			return "TTTT";
    		}
    		
    		else
    			{
    		upcCode=upcCode.substring(0,(noOfDigits-1));
    		
    		
    		
    			var SumOfDigits = findSumOfDigits(upcCode,noOfDigits);
    			
    			// get the even num digits sum
    			var SumOfEven = SumOfDigits.EvenSum;
    			
    			//to get the Odd Sum
    			var SumOfOdd = SumOfDigits.OddSum;
    			
    			
    			var Result =parseInt(SumOfEven)+parseInt(SumOfOdd*3);
               
    			var checkPointLevel0 = Result%10;
                  
    			if(checkPointLevel0!=0)
    				{
    				checkPoint = 10-checkPointLevel0;
                      
    				}
    			else
    				checkPoint =0;
    			
    			//}
    		// to set value in check digit
    			if(noOfDigits==8||noOfDigits==12||noOfDigits==13||noOfDigits==14)
    				{
    				curRecord.setValue({
    	    			fieldId:'custitem_upc_check_digit',
    	    			value:checkPoint
    	    		});
    				}
    		// New updates...
    			
    			// to get the 1 inner carton upc
    			
    			var innerUpc = curRecord.getText({
        			fieldId:'custitem_adv_innerctnupc_primary'
        		});
    		
    			
    			// to get no.of digits..
    			var innerUpcLength =innerUpc.length;
    		
    		
    			var innerUpcCheckDigit=parseInt(innerUpc%10);
    
    			
    			innerUpc=innerUpc.substring(0,(innerUpcLength-1));
    			
    			// to get the sum of even & odd digits..
    			var innerUPCObjects = findSumOfDigits(innerUpc,innerUpcLength);
    			
    	
    			
    			var innerCheckPoint = checkSum(innerUPCObjects);
    			
    
    			// to get the master carton upc
    			
    			var masterUpc = curRecord.getText({
        			fieldId:'custitem_adv_masterctnupc_primary'
        		});
    			
    
    			
    			// to get no.of digits..
    			var masterUpcLength =masterUpc.length;
    		
    		
    			var masterUpcCheckDigit=parseInt(masterUpc%10);
    			
    			masterUpc=masterUpc.substring(0,(masterUpcLength-1));
    			
    			// to get the sum of even & odd digits..
    			var masterUPCObjects = findSumOfDigits(masterUpc,masterUpcLength);
    		
    
    			
    			var masterCheckPoint = checkSum(masterUPCObjects);
    			
    	
    			// to get the import carton upc
    			
    			var importUpc = curRecord.getText({
        			fieldId:'custitem_adv_importctnupc_primary'
        		});
    			
    
    			
    			
    			var importUpcLength =importUpc.length;
    		
    		
    			var importUpcCheckDigit=parseInt(importUpc%10);
    			
    			importUpc=importUpc.substring(0,(importUpcLength-1));
    			
    			// to get the sum of even & odd digits..
    			var importUPCObjects = findSumOfDigits(importUpc,importUpcLength);
    	
    			var importCheckPoint = checkSum(importUPCObjects);
    
    			
    			if(noOfDigits==8||noOfDigits==12||noOfDigits==13||noOfDigits==14)
    				{
    				// to return true / false in Check point...
        			var value1 = calculateReturnValues(checkPoint,upcDigit);
        			curRecord.setValue({
    	    			fieldId:'custitem_upc_check_digit',
    	    			value:checkPoint
    	    		});
    				}
    			
    			else
    				{
    				var value1 ="T";
    				curRecord.setValue({
    	    			fieldId:'custitem_upc_check_digit',
    	    			value:''
    	    		});
    				}
    				
    			//inner carton...
    			if(innerUpcLength==8||innerUpcLength==12||innerUpcLength==13||innerUpcLength==14)
    				{
    				var value2 =calculateReturnValues(innerUpcCheckDigit,innerCheckPoint);
    				
    				curRecord.setValue({
    	    			fieldId:'custitem_innrer_upc_check_digit',
    	    			value:innerCheckPoint
    	    		});
    				}
    			
    			else
    				{
    				var value2 ="T";
    				curRecord.setValue({
    	    			fieldId:'custitem_innrer_upc_check_digit',
    	    			value:''
    	    		});
    				}
    				
    			// master carton...
    			if(masterUpcLength==8||masterUpcLength==12||masterUpcLength==13||masterUpcLength==14)
    				{
    				var value3 =calculateReturnValues(masterUpcCheckDigit,masterCheckPoint);
    				curRecord.setValue({
    	    			fieldId:'custitem_master_upc_check_digit',
    	    			value:masterCheckPoint
    	    		});
    				}
    			
    			else
    				{
    				var value3 ="T";
    				curRecord.setValue({
    	    			fieldId:'custitem_master_upc_check_digit',
    	    			value:''
    	    		});
    				}
    				
    			// import carton...
    			if(importUpcLength==8||importUpcLength==12||importUpcLength==13||importUpcLength==14)
    				{
    				var value4 =calculateReturnValues(importUpcCheckDigit,importCheckPoint);
    				curRecord.setValue({
    	    			fieldId:'custitem_import_upc_check_digit',
    	    			value:importCheckPoint
    	    		});
    				}
    			
    			else
    				{
    				var value4 ="T";
    				curRecord.setValue({
    	    			fieldId:'custitem_import_upc_check_digit',
    	    			value:''
    	    		});
    				}
    				
    			
    			var totalValue =value1+value2+value3+value4;
    		
    			if(totalValue=="FFFF")
    				{
    				var options = {
    					    title: "Sellable unit UPC Code, Master Carton ,Import Carton , Primary Carton are incorrect!",
    					    message: "Do you want to save the record?"
    					};

    					function success(result) { 
    						console.log("result: " + result); 
    					    console.log("err msg"); 
    					}

    					function failure(reason) { 
    					    console.log("Failure: " + reason); 
    					}
    					 
    					dialog.confirm(options).then(success).catch(failure);  
    				}
    			else if(totalValue=="FFFT")
				{
				var options = {
					    title: "Sellable unit UPC Code , Inner carton UPC , Master carton are incorrect!",
					    //UPC Code , Inner Carton UPC ,Import Carton UPC are incorrect
					    message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="FFTF")
				{
				var options = {
					    title: "Sellable unit UPC Code , Inner Carton UPC ,Import Carton UPC are incorrect!",
					    //UPC Code , Inner Carton UPC ,Import Carton UPC are incorrect
					    message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="FFTT")
				{
				var options = {
					    title: "Sellable unit UPC Code , Inner Carton UPC are incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="FTFF")
				{
				var options = {
					    title: "Sellable unit UPC Code , Master Carton UPC ,Import Carton UPC are incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="FTFT")
				{
				var options = {
					    title: "Sellable unit UPC Code, Master Carton UPC are incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="FTTF")
				{
				var options = {
					    title: "Sellable unit UPC Code , Import Carton UPC are incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="FTTT")
				{
				var options = {
					    title: "Sellable unit UPC Code is incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="TFFF")
				{
				var options = {
					    title: "Inner Carton UPC , Master Carton UPC , Import Carton UPC are incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="TFFT")
				{
				var options = {
					    title: "Inner Carton UPC , Master Carton UPC are incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="TFTT")
				{
				var options = {
					    title: "Inner Carton UPC is incorrect!",//Inner Carton UPC ,Import Carton UPC are incorrect!
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="TFTF")
				{
				var options = {
					    title: "Inner Carton UPC ,Import Carton UPC are incorrect!",//
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="TTFF")
				{
				var options = {
					    title: "Master Carton UPC , Import Carton UPC are incorrect!",//
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="TTFT")
				{
				var options = {
					    title: "Master Carton UPC is incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(totalValue=="TTTF")
				{
				var options = {
					    title: "Import Carton UPC is incorrect!",
					   message: "Do you want to save the record?"
					};

					function success(result) { 
						console.log("result: " + result); 
					    console.log("err msg"); 
					}

					function failure(reason) { 
					    console.log("Failure: " + reason); 
					}
					 
					dialog.confirm(options).then(success).catch(failure);  
				}
    			else if(isDev!=1)
    				return true;
    			}
    		
    	}catch(e)
    	{
    		console.log("Err@ FN saveRecord=",e);
    		log.debug("err @save record",e)
    	}

    } /***
     * To get return value
     */
    function calculateReturnValues(checkPoint,upcDigit)
    {
    	try{
    		if(checkPoint!=upcDigit)
    			{
        			return "F";
    			}
        		else
        		
        			return "T";
    	}catch(e)
    	{
    		console.log("Err@ FN calculateReturnValues",e.message);
    		//log.error("Err@ FN calculateReturnValues",e.message);
    		
    	} 
    	
    }
    /**
     * Find Check sum
     */
    function checkSum(SumOfDigits)
    {
    	try{
    		// get the even num digits sum
			var SumOfEven = SumOfDigits.EvenSum;
			
			//to get the Odd Sum
			var SumOfOdd = SumOfDigits.OddSum;
			
    		var result =parseInt(SumOfEven)+parseInt(SumOfOdd*3);
            
			var checkPointLevel0 = result%10;
              
			if(checkPointLevel0!=0)
				{
				checkPoint = 10-checkPointLevel0;
                  
				}
			else
				checkPoint =0;
			
			return checkPoint;
    		
    	}catch(e)
    	{
    		console.log("Err@ FN checkSum",e.message);
    		//log.error("Err@ FN checkSum",e.message);
    		
    	} 
    }
    /**
     * Find Sum of digits
     */
    function findSumOfDigits(upcCode,noOfDigits)
    {
    	var SumObj={},Number=upcCode;
    	SumObj.OddSum=0;
    	SumObj.EvenSum=0;
    	try{
    		
    		for(var i=1;i<noOfDigits;i++)
    			{
    			if(i%2==0)
    				{
    				SumObj.EvenSum=parseInt(SumObj.EvenSum)+parseInt(Number%10);
    				Number = parseInt(Number/10);
    				
    				}
    			else{
    				SumObj.OddSum=parseInt(SumObj.OddSum)+parseInt(Number%10);
    				Number = parseInt(Number/10);
    				
    			}
    			}
    		
    	}catch(e)
    	{
    		console.log("Err@ FN findSumOfDigits",e.message);
          //log.error("Err@ FN findSumOfDigits",e.message);
    		
    	}
    	return SumObj;
    }
    
    /**
     * Function to find count of digit
     */
    
    function getNoOfDigits(Number)
    {
    	try{
    		var count = 0;
    		 
    		while(Number>0)
    			{
    			Number=parseInt(parseInt(Number)/10);
                  
    			count=parseInt(count)+1;
                  
    			}
    		
    		
    		
    	}catch(e)
    	{
    		console.log("Err@ FN getNoOfDigits",e.message);
    		//log.error("Err@ FN getNoOfDigits",e.message);
    		
    	}
    	
    	return count;
    }

   
    return {
    	pageInit:pageInit,
        saveRecord: saveRecord
    };
    
});

Leave a comment

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