How to create a payment record in NetSuite

/**
         * @description To create paymentRecord
         * @param {string| number} collector 
         * @param {string| number} salesRep 
         * @param {string} receiptNumber 
         * @param {object} paymentDetails 
         * @param {object} invoiceData 
         * @returns string paymentId
         */
        const createPaymentRecord = (paymentDetails, invoiceData, paymentObj, id) => {
            try {
                let fieldValues = mapParentObj(paymentObj, paymentDetails)
                let paymentRecord = record.create({
                    type: record.Type.CUSTOMER_PAYMENT,
                    isDynamic: true
                });
                log.debug("paymentRecord", paymentRecord)
                Object.entries(fieldValues).forEach(([fieldId, value]) => {
                    paymentRecord.setValue({
                        fieldId: fieldId,
                        value: value
                    });
                });
                let lineCount = paymentRecord.getLineCount({
                    sublistId: 'apply'
                })
                for (let i = 0; i < invoiceData.length; i++) {
                    let invoiceId = invoiceData[i].invoice;
                    let invRef = search.lookupFields({
                        type: 'invoice',
                        id: invoiceId,
                        columns: ['tranid']
                    });
                    invRef = invRef.tranid
                    for (let j = 0; j < lineCount; j++) {
                        let apply = paymentRecord.getSublistValue({
                            sublistId: 'apply',
                            fieldId: 'refnum',
                            line: j
                        });
                        if (invRef == apply) {
                            paymentRecord.selectLine({
                                sublistId: 'apply',
                                line: j
                            });
                            paymentRecord.setCurrentSublistValue({
                                sublistId: 'apply',
                                fieldId: 'apply',
                                value: true,
                                line: j
                            });
                            paymentRecord.setCurrentSublistValue({
                                sublistId: 'apply',
                                fieldId: 'amount',
                                line: j,
                                value: invoiceData[i].paymentAmount
                            })
                            paymentRecord.commitLine({
                                sublistId: 'apply'
                            })
                        }
                    }
                }


                let paymentRecordId = paymentRecord.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                });
                if (paymentRecordId && paymentObj.fileid) {
                    record.attach({
                        record: {
                            type: 'file',
                            id: paymentObj.fileid
                        },
                        to: {
                            type: 'customerpayment',
                            id: paymentRecordId
                        }
                    });
                }
                log.debug('Payment Record Created Successfully', `Payment Record ID: ${paymentRecordId}`);
                // Update payment record ID in custom record
                paymentRecordId && paymentDetails?.internalId && record.submitFields({
                    id: paymentDetails.internalId,
                    type: 'customrecord_jj_tms_payments_micl_555',
                    values: {
                        custrecord_jj_payment_record: paymentRecordId,
                        custrecord_jj_payment_creation_error: ''
                    }
                });
                return paymentRecordId;
            } catch (error) {
                log.error("Error @createPaymentRecord", error);


                // Update error message in custom record
                paymentDetails?.internalId && record.submitFields({
                    id: paymentDetails.internalId,
                    type: 'customrecord_jj_tms_payments_micl_555',
                    values: {
                        custrecord_jj_payment_creation_error: error.message
                    }
                });
            }
        }

Leave a comment

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