The client wanted their Drop Ship Purchase Orders to pick up the selling rate of the unit item in the related Sales Order. For this, we created a custom field (Customization > Lists, Records, & Fields > Transaction Line Fields > New) that would store the value of the selling rate. We name this field id: _sell_rate (field id is automatically prepended with custcol), applied to Purchase Item. To accomplish this task, we created a client script as well as a user event script. The pageInit entry point was used for the client script while afterSubmit was utilized for the user event script, as shown in the code below:
Client Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/** *@NApiVersion 2.1 *@NScriptType ClientScript */ define(['N/record'], function(record) { function pageInit(context) { //Upon creation of PO from SO, get PO and SO records var poRec = context.currentRecord; if (context.mode != 'create') return; var soId = poRec.getValue({ fieldId: 'createdfrom' }); if (soId == '' || soId == undefined) return; var soRec = record.load({ type: record.Type.SALES_ORDER, id: soId }); //Go through all lines and set the enco so rate custom field with value from the corresponding line in the SO for (var line = 0; line & lt; poRec.getLineCount({ sublistId: 'item' }); line++) { var item = poRec.getSublistValue({ sublistId: 'item', fieldId: 'item', line: line }); var soLine = soRec.findSublistLineWithValue({ sublistId: 'item', fieldId: 'item', value: item }); var soRate = soRec.getSublistValue({ sublistId: 'item', fieldId: 'rate', line: soLine }); poRec.selectLine({ sublistId: 'item', line: line }); poRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'custcol_sell_rate', value: soRate }); } } return { pageInit: pageInit } }); |
User Event Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/** *@NApiVersion 2.1 *@NScriptType UserEventScript */ define(['N/record'], function(record) { function afterSubmit(context) { try { //Get record and ensure that it is created from an SO var poRec = record.load({ type: record.Type.PURCHASE_ORDER, id: context.newRecord.id }); if (context.type != context.UserEventType.DROPSHIP) return; var soId = poRec.getValue({ fieldId: 'createdfrom' }); if (soId == '' || soId == undefined) return; var soRec = record.load({ type: record.Type.SALES_ORDER, id: soId }); //Go through all lines and set the corresponding values from SO to custom field for (var line = 0; line & lt; poRec.getLineCount({ sublistId: 'item' }); line++) { var item = poRec.getSublistValue({ sublistId: 'item', fieldId: 'item', line: line }); var soLine = soRec.findSublistLineWithValue({ sublistId: 'item', fieldId: 'item', value: item }); var soRate = soRec.getSublistValue({ sublistId: 'item', fieldId: 'rate', line: soLine }); poRec.setSublistValue({ sublistId: 'item', fieldId: 'custcol_sell_rate', line: line, value: soRate }); } //Save record poRec.save({ enableSourcing: false, ignoreMandatoryFields: true }); } catch (error) { log.error({ title: 'Error', details: error }); } } return { afterSubmit: afterSubmit } }); |
Expected Behavior
- Create a Drop Shipment purchase order from the Sales Order form.
- Upon saving, a Sales Order and a Purchase Order are generated from the form.
- The Purchase Order should be accessible in the Sales Order via a link.
- The selling rate on the Purchase Order should reflect the unit item’s selling rate from the Sales Order.
This behavior is also mimicked in the PO generated after approval of an existing SO with newly-populated PO Vendor and Create PO fields.