Dynamic Update of Payment Method Field in Customer

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*************************************************************************************
* Payment Method field dynamic update in customer
* Description : UserEvent Script to dynamic update of Payment method of customer.
**************************************************************************************/
define(['N/record', 'N/search'],
    /**
 * @param{format} format
 * @param{record} record
 * @param{search} search
 * @param{commonLibrary} commonLibrary
 */
    (record, search) => {

        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
            try {
                if (scriptContext.type === 'create' || scriptContext.type === 'edit' || scriptContext.type === 'delete') {
                    let newRecord = scriptContext.newRecord;
                    log.debug("newRecord", newRecord);
                    let currentRecId = newRecord.id;
                    log.debug("currentRecId", currentRecId);
                    let customerInternalId = newRecord.getValue({
                        fieldId: 'entity'
                    })
                    log.debug("customerInternalId", customerInternalId);

                    let paymentInstrumentsObj = getPaymentInstrumentDetails(customerInternalId);
                    log.debug("paymentInstrumentsObj", paymentInstrumentsObj)
                    let id = record.submitFields({
                        type: record.Type.CUSTOMER,
                        id: customerInternalId,
                        values: {
                            'custentity_ota_paymentmethod': paymentInstrumentsObj.paymentMethod
                        },
                        options: {
                            enableSourcing: false,
                            ignoreMandatoryFields: true
                        }
                    });
                    log.debug("Successfully Updated");

                }
            } catch (e) {
                log.error("error@afterSubmit", e);
            }
        }

        /**
         * @description Fetch default payment method from customer.
         * @param {*} customerInternalId 
         * @returns 
         */
        function getPaymentInstrumentDetails(customerInternalId){
            try{
                let customerSearchObj = search.create({
                    type: "customer",
                    filters:
                    [
                       ["stage","anyof","CUSTOMER"], 
                       "AND", 
                       ["formulatext: {paymentinstrument.default}","is","T"], 
                       "AND", 
                       ["paymentinstrument.isinactive","is","F"], 
                       "AND", 
                       ["internalid","anyof",customerInternalId]
                    ],
                    columns:
                    [
                       search.createColumn({name: "internalid", label: "Internal ID"}),
                       search.createColumn({name: "altname", label: "Name"}),
                       search.createColumn({
                          name: "paymentmethod",
                          join: "paymentInstrument",
                          label: "Payment Method"
                       }),
                       search.createColumn({
                          name: "default",
                          join: "paymentInstrument",
                          label: "Default"
                       })

                    ]
                 });
                 let searchResultCount = customerSearchObj.runPaged().count;
                 log.debug("customerSearchObj result count",searchResultCount);

                 let customerPaymentMethodObj ={}
                 if(searchResultCount >0){
                    customerSearchObj.run().each(function(result){
                        let customerId = result.getValue(customerSearchObj.columns[0]);
                        let paymentMethod = result.getValue(customerSearchObj.columns[2]);
                        let isDefault = result.getValue(customerSearchObj.columns[3]);
                        log.debug("isDefault",isDefault);
                        customerPaymentMethodObj.customerId = customerId;
                        customerPaymentMethodObj.paymentMethod = paymentMethod;

                        return true;
                     });
                 }

                 return customerPaymentMethodObj;

            }
            catch (e) {
                log.error("error@getPaymentInstrumentDetails", e);
            }
        }
        return { afterSubmit }
    });

Leave a comment

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