Reputation: 812
I am trying to pay an open Invoice in NetSuite. My aim is to pay the Invoice and for it to return the Status 'Paid In Full'.
I am able to create a Customer Payment record quite easily using the code below but I don't seem to be able to assign the payment againt an actual Invoice. When I perform a manual payment (see image), a list of open Invoices is shown and the user makes a selection.
In SuiteScript, I have mimicked this and created a recordset of Open Invoices for that customer. The recordset is traversed and checked for a matching Invoice ID. If at matching Invoice id is found I'm updating these fields, both found in the apply sublist
record.setLineItemValue('apply', 'amount', r, '1.03');
record.setLineItemValue('apply', 'apply', r, 'T');
However that gives me the following error: "You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist".
function postRESTlet(dataIn) {
var record = null;
var err = new Object();
var rectype = "customerpayment";
var id = 5915556;
try{
record = nlapiCreateRecord(rectype);
record.setFieldValue('currency', 1); // GBP
record.setFieldValue('customer', 40562);
record.setFieldValue('customform', 118);
record.setFieldValue('exchangerate', '1.00');
record.setFieldValue('payment', '1.03');
record.setFieldValue('paymentmethod', 8);
record.setFieldValue('account', 154);
var records = nlapiSearchRecord('invoice', null, [
new nlobjSearchFilter('status', null, 'is', 'CustInvc:A'), // Open Invoices
new nlobjSearchFilter('entity', null, 'is', 40562), // Entity
new nlobjSearchFilter('mainline', null, 'is', 'T') // Mainline True
], [
new nlobjSearchColumn('internalid').setSort(false)
]);
if (!records){
nlapiLogExecution('DEBUG', '0 Records Found');
return 'no records found';
};
// Loop all Open Invoices for this entity
for (var r=0; r<records.length; r++) {
// Check for match
if(id == records[r].getId()) {
// Update sublist
//record.setLineItemValue('apply', 'amount', r, '1.03');
//record.setLineItemValue('apply', 'apply', r, 'T');
}
}
var recordId = nlapiSubmitRecord(record,false,true);
var nlobj = nlapiLoadRecord(rectype,recordId);
return nlobj;
} catch(err){
var message = (!!err.message)?err.message:"An unexpected error ocurred";
message += (!!dataIn.id)?("Record ID is: " + dataIn.id):"No ID supplied" + err.message;
return message;
}
}
Does anyone know what steps I need to perform in order to assign payment to a single Invoice.
Thanks in advance :)
UPDATED WORKING RESTLET (in case it helps someone else):
function postRESTlet(dataIn) {
var id = dataIn[0]['sid'];
var entity = dataIn[0]['entity'];
var amount = dataIn[0]['amount'];
var paymethod = dataIn[0]['paymethod'];
var payaccount = dataIn[0]['payaccount'];
var record = null;
var err = new Object();
var rectype = "customerpayment";
var paymentapplied = 0;
try {
var records = nlapiSearchRecord('invoice', null, [
new nlobjSearchFilter('status', null, 'is', 'CustInvc:A'), // Open Invoices
new nlobjSearchFilter('entity', null, 'is', entity), // Entity
new nlobjSearchFilter('mainline', null, 'is', 'T') // Mainline True
], [
new nlobjSearchColumn('internalid').setSort(false)
]);
if (!records){
nlapiLogExecution('DEBUG', 'No Records Found');
return 'no records found';
};
// Loop all Open Invoices for this entity
for(var r=0; r<records.length; r++ ) {
if(records[r].getId() == id) { // The Invoice id matches
// Transform below does not appear documented; but works as expected
var deposit = nlapiTransformRecord('invoice', records[r].getId(), 'customerpayment');
deposit.setFieldValue('trandate', nlapiDateToString(new Date()));
deposit.setFieldValue('currency', 1); // GBP
deposit.setFieldValue('customer', entity);
deposit.setFieldValue('customform', 118);
deposit.setFieldValue('exchangerate', '1.00');
deposit.setFieldValue('payment', amount);
deposit.setFieldValue('paymentmethod', paymethod);
deposit.setFieldValue('account', payaccount);
// Walk the invoice list that we want to apply; find the invoice we are working on
var a = deposit.getLineItemCount('apply');
for(var i = 1; i <= a; i++) {
if(deposit.getLineItemValue('apply', 'internalid', i) == id ) {
nlapiLogExecution('DEBUG', 'working on invoice line:' + i, deposit.getLineItemValue('apply', 'refnum', i));
deposit.setLineItemValue('apply', 'amount', i, deposit.getLineItemValue('apply', 'total', i));
deposit.setLineItemValue('apply', 'apply', i, 'T');
};
};
paymentapplied = 1;
nlapiSubmitRecord(deposit);
return 'Payment Completed';
}
};
if(paymentapplied == 0)
return 'No Payment made. Is the Invoice already Paid?';
} catch(err){
var message = (!!err.message)?err.message:"An unexpected error ocurred";
return message;
}
}
Upvotes: 2
Views: 1717
Reputation: 8847
Use a record transform instead of creation to transform the Invoice into a Customer Payment. See the Help documentation for nlapiTransformRecord()
.
Upvotes: 2