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
    };
});