Any help on this would be greatly appreciated.
I am using a map/reduce script to create a new customer payment record (from an invoice record) which is a partial payment of the invoice. This action needs to occur in the MAP stage.
Example: Amount due on the invoice is (say) £26.66, and I want to apply a £2.00 payment against this.
The following snippet appears to work when applying the FULL payment of the invoice:
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
})
The same code above sadly fires the following error when adjusting amount that is SMALLER than the amount due on the invoice.
I have reached a holt at this point so any assistance would be greatly appreciated. Interestingly, I have managed to create a user event script which successfully does just this. Not sure if its down to the script type that could be preventing this action? Thanks in advance!
