Hi All! I created an user event script to automate...
# suitescript
t
Hi All! I created an user event script to automate invoice/cash sale creation that triggers upon saving the item fulfillment record. The script compares the invoiced qty against the fulfilled qty on the sales order and generates the invoice for the difference. The use case that I didn't consider when creating the script is the scenario where an item is on a sales order twice (as two line items). The invoice that is generated contains the item as two line items but the invoiced qty pulls from the fulfilled qty of the first line and sets that as the invoice qty for both line items on the invoice. For example, item 123 appears on the sales orders twice, the first line has an order and fulfilled qty of 10, the second line has an order and fulfilled qty of 5. When the invoice is created, item 123 is displayed as two line items but the invoice qty for both lines is 10 rather than 10 for the first and 5 for the second. Does anyone have any suggestions as to how I can correct this? The script is written in suitescript 1.0
b
usually the invoice you get from transforming the sales order into an invoice is for all fulfilled items
that should do the quantities for you
t
The invoice does pull in the fulfilled qty. However, if an item is on an order twice then the invoice is created with the item appearing twice but it pulls in the fulfilled qty of the first line on the sales order and sets that as the invoice qty for both lines on the invoice
message has been deleted
The above is a screenshot of the sales order that has the same item twice. As you can see, the second line is pulling in the fulfilled qty of the first line rather than the actual fulfilled qty
The second line should have an invoice qty of 5
b
as an example, a sales order whose items looks like this
t
Yeah
b
looks like this after saving its invoice transform
Copy code
require(["N/record"], function (record) {
  record
    .transform({ fromType: "salesorder", fromId: "4212", toType: "invoice" })
    .save();
});
t
how are you getting it to pull in 5 for the second line?
b
nothing
code i shared is what was used to create the invoice
t
hm - did you fulfill the order using two fulfillments or was it fulfilled with a single fulfillment?
b
single one
it wouldnt really change anything unless you generated an invoice twice, once after each fulfillment
you could get the wrong invoiced numbers if you increased the quantity on the invoice
are you setting quantities on the invoice?
t
I am. Does the transform method as you have it in your code snippet work when there are multiple fulfillments?
btw - I wrote this script using 1.0 rather than 2.0
b
are you using nlapiTransformRecord?
t
var Array_data = []; var Rec_id = nlapiGetRecordId(); var Rec_Type = nlapiGetRecordType(); var isNotMatched; var itemFulfillment = nlapiLoadRecord(Rec_Type, Rec_id); var count = itemFulfillment.getLineItemCount('item'); nlapiLogExecution('debug', 'count ', count); var salesOrder = itemFulfillment.getFieldValue('createdfrom') // getting sales order from item fulfillment salesOrder nlapiLogExecution('debug', 'salesOrder ', salesOrder); var so_Load = nlapiLoadRecord('salesorder', salesOrder); var paymentMethod = so_Load.getFieldValue('terms') // var billed_Quantity = so_Load.getLineItemValue('item', 'quantitybilled'); if(salesOrder && paymentMethod != null) { var so_Record = nlapiLoadRecord('salesorder', salesOrder); var invoice = nlapiTransformRecord('salesorder', salesOrder, 'invoice'); var invoice_Count = invoice.getLineItemCount('item'); nlapiLogExecution('debug', 'invoice_Count ', invoice_Count); for(var j=1;j<=invoice_Count;j++) { nlapiLogExecution('debug', 'j : ', j); var line_Unique = invoice.getLineItemValue('item', 'item', j); nlapiLogExecution('debug', 'line_Unique ', line_Unique); for (var i = 1;line_Unique && i <= count; i++) { nlapiLogExecution('debug', 'i : ', i); var is_Matched_Line = itemFulfillment.getLineItemValue('item', 'item', i); nlapiLogExecution('debug', 'is_Matched_Line ', is_Matched_Line); if(Number(is_Matched_Line)==Number(line_Unique)) { isNotMatched = false; var so_Item = itemFulfillment.getLineItemValue('item', 'item', i); var quantityFulfilled = itemFulfillment.getLineItemValue('item', 'quantity', i); nlapiLogExecution('debug', 'quantityFulfilled ', quantityFulfilled); var quantityFulfilled_SO = so_Load.getLineItemValue('item', 'quantityfulfilled', i); var quantityBilled = so_Load.getLineItemValue('item', 'quantitybilled', i); var finalQty = quantityBilled - quantityFulfilled_SO; // Removed Math.Abs() var abs_finalQty = Math.abs(finalQty); // nlapiLogExecution('debug', 'finalQty id', finalQty); if (abs_finalQty > 0) { invoice.selectLineItem('item', j); invoice.setCurrentLineItemValue('item', 'quantity', abs_finalQty); invoice.commitLineItem('item'); } break; } else isNotMatched =true; } nlapiLogExecution('debug', 'isNotMatched ', isNotMatched); if(isNotMatched) { Array_data.push({keyline Unique,value j}); } } nlapiLogExecution('debug', 'Array_data ', Array_data); for(var a=0;a<Array_data.length;a++) { var line = Array_data[a].value; nlapiLogExecution('debug', 'line ', line); nlapiLogExecution('debug', 'key ', Array_data[a].key); if(line) invoice.removeLineItem('item',Number(line)); } var invoice_Id = nlapiSubmitRecord(invoice, true, true); nlapiLogExecution('debug', 'invoice id', invoice_Id);
I am using nlapiTransformRecord
s
You dont need to set any qty on the transformed record, the the transform does that for you.
t
Gotcha. I feel like I added the logic to set the qty because I was encountering an issue with just using transform. I'll remove that bit of code and see what happens.
b
if your
Invoice in Advance of Fulfillment
or
Show Unfulfilled Items on Invoices
preference is enabled, then your invoice would be for all quantity, regardless of fulfillment
though thats kinda the point of the preference
t
we do have that preference enabled. Do you have any recommendations as to how I can keep that preferences enabled but have the script invoice for the fulfilled qty?
b
dont match by the item field
t
Use the line number instead?
b
match the
orderline
on the fulfillment to the
line
on the sales order
first line of a fulfillment may not be the first of the sales order in partial fulfillment scenarios
t
yeah
so I'm not sure how to handle that use case
b
the
orderline
field of the item fulfillment refers to the line id of the sales order
the
line
field of the sales order refers to the line id of the sales order
do not get the line id and line sequence number mixed up
t
ooo
definitely was
I'll give it shot
I assume the
orderline
field on the invoice refers to the line id of the sales order. Is that correct?
nvm
answered my own question
also - thank you so much for the help!