Create invoice from sales order using the Suite script

We can create the invoice from the details in the sales order created from the webstore using the Suite script code. Obtain the sales order id created from the webstore by extending the ‘OrderWizard.Module.Confirmation’ view. We can obtain the internal id of the sales order from the mentioned view then pass this id to the backend. On the back end using the suite script code we can create the invoice related to the sales order.

The code is given below:

JavaScript entry point

	_.extend(OrderWizardModuleConfirmation.prototype, {
						template: jj_integration_intergration_tpl,
						getContext: _.wrap(OrderWizardModuleConfirmation.prototype.getContext, function (fn) {
							try {
								var originalRet = fn.apply(this, _.toArray(arguments).slice(1));
								console.log("originalRet", originalRet);
								console.log("this", this);
								var salesorderid=originalRet.orderId;
								console.log("salesorderid", salesorderid);
								var url = Utils.getAbsoluteUrl(getExtensionAssetsPath("services/intergration.Service.ss"))
								$.get(url, {
									salesorderid: salesorderid
									})
									.done(function (data) {});
								// var payMethod = (this.model.get('paymentmethods').models.length != 0) ? this.model.get('paymentmethods').models : "none";
								// console.log("tpayMethodhis", payMethod);
							} catch (e) {
								console.log('Error in OrderWizardModuleConfirmation getContext', e);
							}
							return originalRet;
						})
					});

Service controller:

get: function get() {
      try {
        var data=this.request.getParameter('salesorderid');
        var result = integration.createinvoice(data);
        return result;
      } catch (e) {
        console.error(e);
      }
    },

Suite script entry point:

return SCModel.extend({
		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);
			}
		},
	})

Leave a comment

Your email address will not be published. Required fields are marked *