I need to automatically invoice a Sales Order when...
# ask-stanley-ai
s
I need to automatically invoice a Sales Order whenever an Item Fulfillment attached to a sales order changes to Pending Fulfillment - how can I do this natively or with SuiteScript?
c
AI generated answer (using internal Slack threads):
To automatically invoice a Sales Order whenever an Item Fulfillment attached to a sales order changes to Pending Fulfillment, you can use a combination of SuiteScript and workflows. Here's a general approach: 1. Create a Workflow: This workflow will be triggered when the status of an Item Fulfillment changes to 'Pending Fulfillment'. The workflow should be set to run on the Item Fulfillment record type and should be triggered on 'After Record Submit'. The triggering condition should be when the status field changes to 'Pending Fulfillment'. 2. Transform the Sales Order to an Invoice: In the workflow, add an action to execute a SuiteScript. This script will transform the Sales Order to an Invoice. You can use the
N/record
module's
transform
method to achieve this. Here's a sample code snippet:
Copy code
javascript
require(['N/record'], function (record) {
  record.transform({
    fromType: record.Type.SALES_ORDER,
    fromId: salesOrderId,
    toType: record.Type.INVOICE
  }).save();
});
In this code,
salesOrderId
is the internal ID of the Sales Order that you want to transform into an Invoice. You can get this ID from the Item Fulfillment record. 3. Handle Partial Fulfillments: If you have partial fulfillments, you need to handle them carefully. When you transform a Sales Order to an Invoice, NetSuite will invoice for all fulfilled items. If an item is on an order twice, the invoice will be created with the item appearing twice, but it will pull in the fulfilled quantity of the first line and set that as the invoice quantity for both lines on the invoice. To handle this, you can match the
orderline
on the fulfillment to the
line
on the sales order. The
orderline
field of the item fulfillment refers to the line id of the sales order, and the
line
field of the sales order refers to the line id of the sales order. Please note that this is a general approach and you might need to adjust it according to your specific business requirements and NetSuite setup. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.855008364
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.849342108
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.848630071
Please provide your feedback on this answer.