Attach Cash Refund in Related Records Subtab

Almost all the records which are created from the sales order get attached to the related subtab of the sales order record. The client needs to attach the cash refunds created from the cash sale in the sales order record. It can be resolved either through a saved search and sourcing the value into the newly created custom field, or set the value using a script.

For that first created a custom transaction body field with the following field Mapping:-

Field Type : List/Record

List/Record : Transaction

store value : checked

Applies To : Sales

Record is parent: Checked

Subtab: Related Records

Parent Subtab: Related Records

Created the UserEvent script in cash refund record

define(['N/search','N/record'],
    /**
     * @param{search} search
     * @param{record} record
     */
    (search,record) => {
        function cashRefundSearch(recordId){
            try{
                var cashrefundSearchObj = search.create({
                    type: "cashrefund",
                    filters:
                        [
                            ["type","anyof","CashRfnd"],
                            "AND",
                            ["appliedtotransaction.type","anyof","CashSale","RtnAuth","CustCred"],
                            "AND",
                            ["internalid","anyof",recordId],
                            "AND",
                            ["mainline","is","T"]
                        ],
                    columns:
                        [
                            search.createColumn({
                                name: "createdfrom",
                                join: "appliedToTransaction",
                                label: "Created From"
                            })
                        ]
                });
                var searchResultCount = cashrefundSearchObj.runPaged().count;
                log.debug("cashrefundSearchObj result count",searchResultCount);
                var salesOrder;
                cashrefundSearchObj.run().each(function(result){
                    salesOrder = result.getValue({name: "createdfrom", join: "appliedToTransaction", label: "Created From"})
                    log.debug("salesOrder",salesOrder)
                    return true;
                });
                return salesOrder;
            }catch(e){
                log.debug("Error@cashRefundSearch",e)
            }
        }
        /**
         * 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 === scriptContext.UserEventType.CREATE || scriptContext.UserEventType.EDIT){
                                let recordId = scriptContext.newRecord.id;
                                log.debug("Record ID",recordId)
                                var salesOrderId = cashRefundSearch(recordId)
                                log.debug("salesOrderId",salesOrderId)
                                var cashRefundRecord = record.load({
                                    type: record.Type.CASH_REFUND,
                                    id: recordId
                                })
                                log.debug("cashRefund",cashRefundRecord)
                                var setValue = cashRefundRecord.setValue({
                                    fieldId: "custbody_jj_sales_order",
                                    value: salesOrderId,
                                    ignoreFieldChange: true
                                })
                            log.debug("setValue",setValue)
                            cashRefundRecord.save();
                                log.debug("record saved")
                        }
                }catch(er){
                    log.debug("Error@afterSubmit")
                }

        }

        return { afterSubmit}
    });

Leave a comment

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