Hi All, I am working in Automating Intercompany PO...
# suitescript
p
Hi All, I am working in Automating Intercompany PO to SO by using userevent script. I am not creating any transfer order just creating the intercompany SO from Intercompany PO. In that i am getting a error "IC_TRANSFER_ORDER_NOT_SOURCED_FROM_PO". Just sharing the code. can anybody help me to overcome this? or can i use any other script like map/reduce or schedule to achieve this? /** * @NApiVersion 2.1 * @NScriptType UserEventScript */ define(['N/record', 'N/log'], function (record, log) { function afterSubmit(context) { if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT) return; try { var poId = context.newRecord.id; log.debug('Script Triggered', 'PO ID: ' + poId); // Load PO var poRecord = record.load({ type: record.Type.PURCHASE_ORDER, id: poId, isDynamic: true }); log.debug('PO Record Loaded', 'Internal ID: ' + poId); //Extracting item lines var itemLines = []; var lineCount = poRecord.getLineCount({ sublistId: 'item' }); log.debug('Line Count in PO', lineCount); for (var i = 0; i < lineCount; i++) { var itemId = poRecord.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i }); var quantity = poRecord.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: i }); var rate = poRecord.getSublistValue({ sublistId: 'item', fieldId: 'rate', line: i }); log.debug('Extracted Line ' + i, 'Item: ' + itemId + ', Quantity: ' + quantity + ', Rate: ' + rate); itemLines.push({ item: itemId, quantity: quantity, rate: rate }); } //Save PO var savedPoId = poRecord.save({ enableSourcing: false, ignoreMandatoryFields: true }); log.debug('PO Saved', 'Saved PO ID: ' + savedPoId); //Load Vendor and Subsidiary var vendorId = poRecord.getValue('entity'); log.debug('Vendor ID from PO', vendorId); var vendorRec = record.load({ type: record.Type.VENDOR, id: vendorId }); var representingSubsidiary = vendorRec.getValue('representingsubsidiary'); var representingSubsidiaryText = vendorRec.getText('representingsubsidiary'); log.debug('Vendor Representing Subsidiary', representingSubsidiary + ' (' + representingSubsidiaryText + ')'); if (!representingSubsidiary) { log.debug('Not Intercompany', 'Vendor has no representing subsidiary'); return; } var poSubsidiary = poRecord.getValue('subsidiary'); log.debug('PO Subsidiary ID', poSubsidiary); var subsidiaryRec = record.load({ type: 'subsidiary', id: poSubsidiary }); var representingCustomer = subsidiaryRec.getValue('representingcustomer'); var representingCustomerText = subsidiaryRec.getText('representingcustomer'); log.debug('PO Subsidiary Representing Customer', representingCustomer + ' (' + representingCustomerText + ')'); if (!representingCustomer) { log.debug('Not Intercompany', 'Subsidiary has no representing customer'); return; } //Create Sales Order var so = record.create({ type: record.Type.SALES_ORDER, isDynamic: true }); so.setValue('entity', representingCustomer); so.setValue('subsidiary', representingSubsidiary); so.setValue('intercoStatus', 2); log.debug('Creating SO', 'Subsidiary ID: ' + representingSubsidiary + ', Customer ID: ' + representingCustomer); //Add Items itemLines.forEach(function (line, index) { log.debug('Adding Line ' + index, JSON.stringify(line)); so.selectNewLine({ sublistId: 'item' }); so.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: line.item }); so.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: line.quantity }); so.setCurrentSublistValue({ sublistId: 'item', fieldId: 'rate', value: line.rate }); so.commitLine({ sublistId: 'item' }); }); //Save SO var soId = so.save({ enableSourcing: true, ignoreMandatoryFields: true }); log.audit('ICSO Created Successfully', 'Sales Order ID: ' + soId); //Update Intercompany link var result = record.submitFields({ type: record.Type.SALES_ORDER, id: soId, values: { intercotransaction: poId }, options: { enableSourcing: false, ignoreMandatoryFields: true } }); log.debug('Interco Transaction Field Set', 'SO ID: ' + soId + ', PO ID: ' + poId); } catch (e) { log.error('Error Creating ICSO', e.toString()); } } return { afterSubmit: afterSubmit }; });
e
Took a look at your script, got a few questions: • What record type is this UserEvent attached to? • Why are you saving the PO on line 30 when you did not update or set any PO fields? • Why do you save the new Sales order on line 75 only to update a single field again on line 81? • How are you going to prevent duplicate sales orders from being created every time the PO is edited? Also in regards to "IC_TRANSFER_ORDER_NOT_SOURCED_FROM_PO" I think that happens cause NetSuite is expecting a Transfer order instead of a Purchase Order for moving intercompany inventory.
a
UE is presumably attached to PO record
Copy code
var poId = context.newRecord.id;
your other points are all certainly valid šŸ™‚
e
There could be a custom record he is working off as well, otherwise why load the PO record when you could access it with context.newRecord ? He's access the PO internal ID from context.newRecord only to reload the record again. But good catch as well @Anthony OConnor
a
generally you load the the full record, to account for situations where its an XEDIT and you don't have the full record in the context
šŸ¤” 1
oh nevermind they're only processing on CREATE and EDIT
šŸ‘ 1
n
Some fields are only available after you load the record.