we are tried to create an invoice from the SO using the default values to the mandatory fields such as location. The invoice are successfully created for the normal inventory item from the sales order.
The code used:
createinvoice: function (data) {
try {
var salesOrderId = data;
var salesOrder = nlapiLoadRecord('salesorder', salesOrderId);
console.log("salesOrder", salesOrder);
// Create a new Invoice record
var invoice = nlapiCreateRecord('invoice');
// Set fields on the Invoice record
invoice.setFieldValue('entity', salesOrder.getFieldValue('entity'));
var locationId = salesOrder.getFieldValue('location');
// Set location field on invoice record
if (locationId) {
invoiceRecord.setFieldValue('location', locationId);
} else {
// If location is empty, set a default location based on criteria
var defaultLocationId = null;
// Run the saved search to retrieve locations
var locationSearch = nlapiSearchRecord("location", null, [
["isinactive", "is", "F"],
"AND",
["subsidiary", "anyof", salesOrder.getFieldValue('subsidiary')]
], [
new nlobjSearchColumn("name").setSort(false),
new nlobjSearchColumn("phone"),
new nlobjSearchColumn("city"),
new nlobjSearchColumn("state"),
new nlobjSearchColumn("country")
]);
// Check if the search returned any results
if (locationSearch && locationSearch.length > 0) {
// Retrieve the first location from the search results
var locationResult = locationSearch[0];
defaultLocationId = locationResult.getId();
} // Replace with your logic to determine the default location
invoiceRecord.setFieldValue('location', defaultLocationId);
}
// Get line item count from Sales Order
var lineItemCount = salesOrder.getLineItemCount('item');
// Add line items to the Invoice record
for (var i = 1; i <= lineItemCount; i++) {
var item = salesOrder.getLineItemValue('item', 'item', i);
var quantity = salesOrder.getLineItemValue('item', 'quantity', i);
// Add logic to retrieve item details like price, description, etc.
// Set line item fields on the Invoice record
invoice.selectNewLineItem('item');
invoice.setCurrentLineItemValue('item', 'item', item);
invoice.setCurrentLineItemValue('item', 'quantity', quantity);
// Set more line item fields as needed
invoice.commitLineItem('item');
}
// Save the Invoice record
var invoiceId = nlapiSubmitRecord(invoice);
console.log("hello", invoiceId);
} catch (e) {
console.error('error at model', e);
}
},
We cannot create the invoice for serialized inventory items from the sales order. In case of serialized item the inventory details are mandatory for invoice but not for sales order. We cant create a inventory details by using code the serial number that we added may cause further development issue so it is not possible.
