Task-5: Update PO line item field on a field change.

Jira Code : CD-5

Once the “Receive by” date is changed, the “expected receive date” of every item in the PO will automatically be updated with that ‘Receive By’ date.

Client Script

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/**
 * Script Description
 *  In PO on Receive by date field update, the ETA date in PO’s line items get updated.
 *  
 */
/*******************************************************************************
 * CORP DESIGN
 * **************************************************************************
 * 
 * Date: 23/04/2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY
 * 
 * Revision  $ 23/04/2019 Maria: Create
 * 
 ******************************************************************************/

define(['N/record','N/search','N/runtime'],

function(record,search,runtime) {
    
    /**
     * 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) {

    }

    function fieldChanged(scriptContext) {
    	
    	try{
    	var currentRec = scriptContext.currentRecord;
        
        // Triggering the field change of field receive by from PO
        if(scriptContext.fieldId=='duedate'){
        	
        	//Get Receive by date
            var receivedate = currentRec.getValue({
                fieldId: 'duedate',
            });
            
            //function to update PO line item field
            var update_ETA_date = updatedate(currentRec, receivedate);
        	
        }
    } catch (e) {
        logme("@fieldchange", getError(e));
    }

    }
    
     // function to update ETA date in PO line items 
     function updatedate(currentRec, receivedate){
    	
    	try{  		            
            var linecount = currentRec.getLineCount({
                sublistId: 'item'
            });
            
            for (var i = 0; i<linecount; i++){
            	
            	// to select the line
            	currentRec.selectLine({
                    sublistId: 'item',
                    line: i
                });
            	
            	// Set ETA date 
            	currentRec.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'expectedreceiptdate',
                    line: i,
                    value: receivedate
                });
            	
            	currentRec.commitLine({
                    sublistId: 'item'
                });
            }
    		return true;
    		
    	} catch (e) {
            logme("@itemupdate", getError(e));
        }
    }


    return {
        pageInit: pageInit,
        fieldChanged: fieldChanged
    };
    
    /*******************************************************************************
     * return error
     * 
     * @param e
     * @returns
     * 
     * Created on 22-Feb-2019 by Maria
     */
    function getError(e) {
        var stErrMsg = '';
        if (e.getDetails != undefined) {
            stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
                    + e.getStackTrace();
        } else {
            stErrMsg = '_' + e.toString();
        }
        return stErrMsg;
    }

    /*******************************************************************************
     * Log these data
     * 
     * @param title
     * @param details
     * @returns
     * 
     * Created on 22-Feb-2019 by Maria
     */
    function logme(title, details) {
        log.debug({
            title : title,
            details : details
        });
    }
    
});

Leave a comment

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