Oneworld (multiple subsidiaries, multiple customer...
# suitescript
m
Oneworld (multiple subsidiaries, multiple customers), Sales Orders: We are attempting to, after the customer has been selected by the user, to set the Subsidiary to replace the primary subsidiary on the customer record with the subsidiary on the most recent sales order for the customer (same with currency). I've attempted several approaches: fieldedit, postsourcing. In all cases NetSuite, in the UI, will flash the subsidiary from the subscript, then will immediately over-ride it with the primary subsidiary. ClientScript. Script is here. Any ideas are super welcome here. Thank you! /** * @NApiVersion 2.1 * @NScriptType ClientScript * @NModuleScope SameAccount */ define(['N/search', 'N/record', 'N/log'], (search, record, log) => { function fieldChanged(scriptContext) { const sublistId = scriptContext.sublistId; const fieldId = scriptContext.fieldId; const currentRecord = scriptContext.currentRecord; // Check if the field change occurs at the record level (headers) if (!sublistId && fieldId === 'entity') { const entityId = currentRecord.getValue({ fieldId: 'entity' }); // Check if the "Entity" field has a value (not empty) if (entityId) { // Proceed with the Sales Order search const salesOrderSearch = search.create({ type: search.Type.SALES_ORDER, filters: [ search.createFilter({ name: 'entity', operator: search.Operator.ANYOF, values: [entityId] }) ], columns: [ { name: 'subsidiary' }, { name: 'currency' }, { name: 'datecreated', sort: search.Sort.DESC } // Sort by Date Created in descending order ], title: 'Recent Sales Order Search' }); // Run the search and retrieve the results const searchResults = salesOrderSearch.run().getRange({ start: 0, end: 1 }); if (searchResults.length > 0) { // Get the subsidiary and currency from the most recent Sales Order const mostRecentSubsidiaryId = searchResults[0].getValue({ name: 'subsidiary' }); const mostRecentCurrencyId = searchResults[0].getValue({ name: 'currency' }); // Log the "Currency" and "Subsidiary" values from the search results log.debug('Most Recent Subsidiary ID', mostRecentSubsidiaryId); log.debug('Most Recent Currency ID', mostRecentCurrencyId); // Set the Subsidiary and Currency fields on the current transaction try { currentRecord.setValue({ fieldId: 'subsidiary', value: mostRecentSubsidiaryId }); currentRecord.setValue({ fieldId: 'currency', value: mostRecentCurrencyId }); } catch (e) { log.error({ title: 'Error updating subsidiary and currency fields', details: e.toString() }); } } } } } return { fieldChanged: fieldChanged }; });
m
@Michael_R2D2 Try doing a console.log the fieldID inside the fieldChanged and postSourcing functions. Watch the flow of what fields are affected in what order when the customer is selected. Your script might be changing the subsidiary field to early/quickly before all of the native NetSuite field changes are completed. You might need to run the fieldChanged/postSourcing based off another field. Another thought, are you using different forms that are driven by certain options on the sales order by chance? If the form value changes it causes a page refresh which will reset the subsidiary on you.
👀 1
n
If changing it in a client script is getting overwritten you might need to do it in a User Event before submit. If the user seeing the value you're changing it to is important, you may need to hide the native field, show and set a custom field and then use that value in a UE to set the native field
👀 1
m
@Matt Carter @NElliott Thank you for both of your insights. Matt, good callout on the form change; there is a workflow that switches the form based on the role of the user. I'll look ingo that and also do a console.log. If behavior persists, NEliott, your recommendation will work also excellent suggestion thank you. I do dislike introducing new fields just to make it "look" like it's working as expected but totally understand in some situations completely necessary. Thank you both again for these suggestions. Super helpful feedback.
s
I didn't think you could change the Primary Subsidiary after there has been a Transaction against the Entity?
👀 1
Sorry, you are changing the transaction.
👀 1
m
@Stuart Anderton Yep attempting to change it on the S.O. transaction. Before saving, a user can change the subsidiary. Once transaction is saved of course the subsidiary of the transaction cannot be edited.