Invoice migration between subsidiaries in NetSuite can be streamlined using a Map/Reduce script. This process involves identifying invoices in the source subsidiary and replicating them in the target subsidiary while ensuring all necessary fields and data relationships are accurately transferred.
Steps in the Map/Reduce Script
- Get Input Data:
- The script starts by fetching the list of invoices that need to be migrated using specific filters. For example, invoices from a specific subsidiary or within a date range are identified.
- Map Stage:
- In this stage, each invoice is processed to determine its mapping to the target subsidiary. It involves verifying fields like customer, items, and subsidiary relationships to ensure compatibility.
- Reduce Stage:
- This is the core of the migration process. The script creates a new invoice in the target subsidiary, replicating essential fields such as
entity,trandate,location,items, and more. Any invalid data (e.g., inactive items) is handled, such as replacing or skipping invalid items. - Summarize Stage:
- Finally, the script logs the success or failure of each migration and generates a summary for validation.
const reduce = (reduceContext) => {
let invoiceId = reduceContext.key;
let originalInvoice = record.load({
type: record.Type.INVOICE,
id: invoiceId
});
let newInvoice = record.create({
type: record.Type.INVOICE,
isDynamic: true
});
// Set key fields
newInvoice.setValue({ fieldId: 'entity', value: newCustomer });
newInvoice.setValue({ fieldId: 'subsidiary', value: 24 });
newInvoice.setValue({ fieldId: 'trandate', value: originalInvoice.getValue({ fieldId: 'trandate' }) });
// Copy item lines
let lineCount = originalInvoice.getLineCount({ sublistId: 'item' });
for (let i = 0; i < lineCount; i++) {
newInvoice.selectNewLine({ sublistId: 'item' });
let item = originalInvoice.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i });
// Replace invalid item
if (item === 4753) item = 7117;
newInvoice.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: item });
newInvoice.commitLine({ sublistId: 'item' });
}
newInvoice.save();
};
Benefits of Map/Reduce for Invoice Migration
- Efficiency: Processes large datasets without timeout issues.
- Flexibility: Handles custom logic for field validation and data transformation.
- Scalability: Adapts to different migration needs, like custom fields or multiple subsidiaries.
This approach ensures accurate, efficient invoice migration while maintaining data integrity.