SuiteScript 2.0:根据发票应用部分客户付款时抛出错误

SuiteScript 2.0: Error thrown when applying partial customer payment against an invoice

如有任何帮助,我们将不胜感激。

我正在使用 map/reduce 脚本创建新的客户付款记录(来自发票记录),这是发票的部分付款。此操作需要在 MAP 阶段发生。

示例:发票上的应付金额是(比如说)26.66 英镑,我想为此申请 2.00 英镑的付款。

以下代码片段在应用发票的全额付款时似乎有效:

        var customerpayment = record.transform({
            fromType : rectype, 
            fromId: recid, 
            toType: 'customerpayment',
            isDynamic: true
        })

            if (amount > 0){

            var linecount = customerpayment.getLineCount({ sublistId: 'apply' }); 

            for (var i = 1; i < linecount; i++) {
                
            var refnum = customerpayment.getCurrentSublistValue({sublistId: 'apply', fieldId: 'refnum', line: i });
                
                if (refnum == tranid) { 
                    log.audit ('refnum == tranid. Line no: ' + i, refnum + ' | ' + tranid); 
                    
                    //isDynamic = TRUE
                    customerpayment.selectLine({sublistId: 'apply', line: i });

                    //Mark "apply' FALSE to clear the apply box (this would clear the "payment" header field as well)
                    customerpayment.setCurrentSublistValue({sublistId: 'apply', fieldId: 'apply', value: false });

                    //Now set the payment header field with the amount you want to apply
                    customerpayment.setValue({fieldId: 'payment', value: amount});      
                   
                    //Now select TRUE on apply. This (should) copy/paste the payment amount automatically as it does via the UI.
                    customerpayment.setCurrentSublistValue({sublistId: 'apply', fieldId: 'apply', value: true });
                     
                    //For sanity sake, just populate the apply 'total' line with the same amount as payment field. In case it did not do it automatically.
                    customerpayment.setCurrentSublistValue({sublistId: 'apply', fieldId: 'total', value: amount });

                    var getTotal = customerpayment.getCurrentSublistValue({sublistId: 'apply', fieldId: 'total', line: i }); 
                    var getAllTotal = customerpayment.getValue({fieldId: 'payment'});                       

                    //Quick check that 'payment' header field and apply total field are the same
                    log.audit ('payment | payment line', getAllTotal + ' | ' + getTotal);   

                    break;
                }
            }
            
            //Now save record
            var payment_id = customerpayment.save({
                enableSourcing : true,
                ignoreMandatoryFields: true
            })

不幸的是,当调整的金额小于发票上的到期金额时,上面的相同代码会引发以下错误。

此时我已陷入困境,因此将不胜感激任何帮助。 有趣的是,我设法创建了一个用户事件脚本,它成功地做到了这一点。 不确定它是否归因于可能阻止此操作的脚本类型? 提前致谢!

当您创建付款记录时,您必须在正文级别设置付款金额。由于您处于动态模式,因此您必须在开始应用之前设置它。

customerpayment.setValue({fieldId:'payment', value:amount});
customerpayment.setValue({fieldId:'autoapply', value:false}); //so only the invoice of interest will receive the payment

然后应用到你的台词。

if(refNum == tranid){
   customerpayment.setCurrentSublistValue({sublistId: 'apply', fieldId: 'apply', value: true }); // IIRC this is all you need to do. I think in dynamic mode NS fills in the amount or the amount owing whichever is less. But if not proceed:

   var canApply = Math.min(amount, customerpayment.getCurrentSublistValue({sublistid:'apply', fieldId:'amount'}));

   customerpayment.setCurrentSublistValue({sublistId: 'apply', fieldId: 'amount', value: canApply});
   break;
}