Customer category sublist auto update

Jira Code: HPATH-14

Automate the sublist update(Create a custom history for a field change), when there is a change in the body fields ‘season, category or subcategory’. We will add a new line into the customer category history sublist using workflow. Created a client script deployed to custom record. On entering a value in ‘To date’ field, if ‘From date’ is less than ‘To date’, an alert shows on save action.

Workflow

  1. The workflow will trigger on create and view or update of record after submit
  2. Add a new action Create record.
  3. On new action add a condition.
  4. On parameter tab, enter the record type and add the fields.

Client script

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
 /**
 * Script Description
 *  Customer category History- Update customer category history sublist update.
 *  
 */
/*******************************************************************************
 * HR Path
 * **************************************************************************
 * 
 * Date: 13/06/2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY
 * 
 * Revision 1 $ 13/06/2019 Maria: Create
 * 
 ******************************************************************************/
define(['N/record'],

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

    }

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

            var currentRec = scriptContext.currentRecord;
            console.log("currentRec",currentRec);

            //To trigger the field chage of the field To date

                // Get the field value From date
                var fromDate = currentRec.getText({
                    fieldId : 'custrecord_hpath_8_from_date'
                });                
                var from_date = new  Date(fromDate).getTime()

                // Get the field value To date
                var toDate = currentRec.getText({
                    fieldId : 'custrecord_hpath_8_to_date'
                });
                var to_date = new  Date(toDate).getTime();               

                if(fromDate && toDate){
                    if(from_date > to_date){
                        alert("TO DATE should be greater than or equal to FROM DATE");
                        return false
                    }else{
                        return true
                    }
                }

        }catch (e) {
            console.log("error@saveRecord", getError(e));
        }

    }
    /*******************************************************************************
     * return error
     * Created on 13-June-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;
    }

    return {
        pageInit: pageInit,
        /*fieldChanged: fieldChanged,
        postSourcing: postSourcing,
        sublistChanged: sublistChanged,
        lineInit: lineInit,
        validateField: validateField,
        validateLine: validateLine,
        validateInsert: validateInsert,
        validateDelete: validateDelete,*/
        saveRecord: saveRecord
    };
    
});
/**/

Client Script

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
 /**
 * Script Description
 *  Customer category History- Update customer category history sublist update.
 *  
 */
/*******************************************************************************
 * HR Path
 * **************************************************************************
 * 
 * Date: 13/06/2019
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 * 
 * REVISION HISTORY
 * 
 * Revision 1 $ 13/06/2019 Maria: Create
 * 
 ******************************************************************************/
define(['N/record','N/search','N/runtime'],

function(record,search,runtime) {
    
    function pageInit(scriptContext) {
    }

    function fieldChanged(scriptContext) {

        try{

            var currentRec = scriptContext.currentRecord;
 
            //To trigger the field chage of the field To date
            if(scriptContext.fieldId=='custrecord_hpath_8_to_date'){

                //Getting the sublist
                var categorySublist = currentRec.getSublist({
                 sublistId:'recmachcustrecord_hpath_8_parent'
                });

                // Get the field value From date
                var fromDate = currentRec.getCurrentSublistText({
                    sublistId: 'recmachcustrecord_hpath_8_parent',
                    fieldId : 'custrecord_hpath_8_from_date'
                });
                var from_date = new  Date(fromDate).getTime();

                // Get the field value To date
                var toDate = currentRec.getCurrentSublistText({
                    sublistId: 'recmachcustrecord_hpath_8_parent',
                    fieldId : 'custrecord_hpath_8_to_date'
                });
                var to_date = new  Date(toDate).getTime();            

                if((fromDate) && (toDate)){
                    if(from_date > to_date){
                        alert("TO DATE should be greater than or equal to FROM DATE");
                    }
                }
                

            }

        }catch (e) {
            console.log("error@fieldChanged", getError(e));
        }
    }    

    /*******************************************************************************
     * return error
     * Created on 13-June-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;
    }

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

Leave a comment

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