Create customer payment and use the credit memo to pay the invoice

function createCustomerPaymentUsingCmId(currentRecord, salesLocation, location, cmId) { //Function to pay the invoice using credit memo.
            try {
                let paymentRecord = record.transform({
                    fromType: record.Type.INVOICE,
                    fromId: currentRecord.id,
                    toType: record.Type.CUSTOMER_PAYMENT,
                    isDynamic: false
                });
                paymentRecord.setValue({
                    fieldId: 'paymentmethod',
                    value: 24
                });
                paymentRecord.setValue({
                    fieldId: 'authcode',
                    value: '1234'
                });
                paymentRecord.setValue({
                    fieldId: 'custbody_ccard_present',
                    value: 1
                });
                paymentRecord.setValue({
                    fieldId: 'custbody_aha_sales_location',
                    value: salesLocation
                });
                paymentRecord.setValue({
                    fieldId: 'location',
                    value: location
                });
                let cmLineCount = paymentRecord.getLineCount({
                    sublistId: 'credit'
                });
                for (let i = 0; i < cmLineCount; i++) {
                    let cmRecord = paymentRecord.getSublistValue({
                        sublistId: 'credit',
                        fieldId: 'refnum',
                        line: i
                    });
                    if (cmId.includes(cmRecord)) {
                        paymentRecord.setSublistValue({
                            sublistId: 'credit',
                            fieldId: 'apply',
                            value: true,
                            line: i
                        });
                    }
                }
                paymentRecord.save({
                    ignoreMandatoryFields: true,
                    enableSourcing: true
                });
            }
            catch (err) {
                log.error("Error in createCustomerPaymentUsingCmId", err);
                sendEmailOnError(currentRecord.id);
            }
        }

Leave a comment

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