Credit memo due date

Jira Code: SMT-14

Description: Due date is calculated for credit memo using the formula: credit memo date + term(in the customer record)

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/search','N/record','N/format'],

function(search,record,format) {
   
    /**
     * 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) {
    	try{
    		var customerid=scriptContext.newRecord.getValue({
            fieldId: 'entity'
           });
            log.debug({    
				title: 'customer id',
                details: customerid
            });
 
    		var customerTerm= search.lookupFields({
                type: record.Type.CUSTOMER,
                id: customerid,
                columns: ['terms']
            });

    		log.debug({    
				title: 'CustomerTerm',
                details: customerTerm
            });
            
            var dateAdded;
            var dateid=scriptContext.newRecord.getValue({
	            fieldId: 'trandate'
	        });
	          if(customerTerm.terms[0] != null && customerTerm.terms[0] != undefined && customerTerm.terms[0] != ' '){
	          	  log.debug({    
				title: 'customerTerm.terms[0].value;',
                details: customerTerm.terms[0].value
            });
        
	        var term= search.lookupFields({
                type: record.Type.TERM,
                id:customerTerm.terms[0].value,
                columns: ['daysuntilnetdue']
	            });
          
            	
	           log.debug({    
					title: 'Term',
	                details: term
	            });
	           log.debug({    
					title: 'Days until net due',
	                details: term.daysuntilnetdue
	            });


	           log.debug({    
					title: 'dateid',
	                details: dateid
	            });
	           var dueno=term.daysuntilnetdue;
	           if(dueno != null && dueno != undefined && dueno!=' '){
			 		dateAdded	= new Date(new Date(dateid).getTime()+(86400000*parseInt(term.daysuntilnetdue)));
	           }

	            else{
	            	dateAdded=new Date(new Date(dateid).getTime());
	            }
	        }
	        else{
	        	dateAdded=new Date(new Date(dateid).getTime());
	        }
           


			 log.debug({    
				title: 'dateAdded',
                details: dateAdded
            });
    
             scriptContext.newRecord.setValue({
             fieldId: 'custbody7',
             value: dateAdded
    
});
    	}
    	
    	catch(err){
            log.debug({    
				title: 'err',
                details: err
            });

    	}


    }

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

    //}

    return {
       
        beforeSubmit: beforeSubmit
     
    };
    
});

Leave a comment

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