Source Transaction Line Fields from Line on Sales Order onto Body Fields for Linked Work Order using SuiteScript

Written by
Manuelito Macalinao
Published on
September 28, 2023 at 7:38:34 AM PDT September 28, 2023 at 7:38:34 AM PDTth, September 28, 2023 at 7:38:34 AM PDT

Scenario

The User would like to source values of custom Transaction Line Fields from respective lines on a Sales Order onto Body Fields for the Linked Work Order possibly created in the following manner:

  1. Navigate toTransactions>Sales>Enter Sales Orders
  2. ClickItemssubtab
  3. ClickItemssublist
  4. Item:EnterAssembly Item
  5. ClickAdd
  6. ClickSave
    Note:Add any other required fields on the Sales Order and keep any defaults.
  7. View savedSales Order
  8. ClickItemssubtab
  9. ClickItemssublist
  10. Notice value ofCreate WOfield
    Note:TheCreate WOcolumn will either of the following values:
    a)Work Order ID(if the Work Order has been created automatically) -or-

    b)Work Ord.(which may be clicked to manually create the associated Work Order)
The values ofTransaction Line Fieldson theSales Order Linethat theWork Orderhas beencreated frommay be set as values forBody Fieldson theWork Orderusing aScript.

Solution


The sample Script below searches for theSales Orderthat theWork OrderwasCreated Fromonce theWork Orderis created and obtains the value of theCustom Transaction Line Fieldof theLine Itemit is associated with. It then sets this value onto aCustom Transaction Body FieldtheWork Order.
Note:The Work Order will not get updated automatically if the corresponding Sales Order Transaction Line Field is updated after the Work Order is Saved. In order to update the Work Order, it will need to be Edited and Saved again.
  • Script Type:User Event
  • Function:After Submit Function
  • Deployed On:Work Order
SuiteScript 1.0
functionsetSOColFieldsonWO_afterSubmit_UE(type){//internal ID of the Work Ordervar wo_internalID =nlapiGetRecordId();//load the Work Order recordvar wo =nlapiLoadRecord('workorder', wo_internalID);//internal ID of the Sales Order that the Work Order was created fromvar createdFromSO = wo.getFieldValue('createdfrom');//if the Created From field on the Work Order has a value, do the following:if((createdFromSO !="")&&(createdFromSO !=null)){//conduct search to obtain value of custom transaction line fields on the Sales Order that the Work Order was created from//search filters://- Work Order with the current Work Order's Internal ID//- Mainline is True//- Applied to Transaction is Sales Order//- Applied to Transaction (Sales Order) of the Work Order is the same as the Sales Order that the Work Order was created fromvar filters =newArray(); filters[0]=newnlobjSearchFilter('internalid',null,'is', wo_internalID); filters[1]=newnlobjSearchFilter('mainline',null,'is','T'); filters[2]=newnlobjSearchFilter('type','appliedtotransaction','is','SalesOrd'); filters[3]=newnlobjSearchFilter('appliedtotransaction',null,'is', createdFromSO);//search columns:// - Applied To Transaction (Sales Order) Internal ID// - Custom Transaction Line Fields from the Applied to Transaction (Sales Order)var columns =newArray(); columns[0]=newnlobjSearchColumn('appliedtotransaction'); columns[1]=newnlobjSearchColumn('custcol_fieldonSO','appliedtotransaction');//(can add more Custom Transaction Line Fields from the Sales Order here)//perform searchvar searchResults =nlapiSearchRecord('workorder',null, filters, columns);//if there are results from the search, do the following:if(searchResults){//get value of the first search result's (there should be only one search result) to Custom Transaction Line Fields from the Sales Ordervar myCustTransColFieldfromSO = searchResults[0].getValue('custcol_fieldonSO','appliedtotransaction');//(can add more Custom Transaction Line Fields from the Sales Order here)//set value of Custom Transaction Line Fields from the Sales Order onto the Custom Transaction Body Fields on the Work Order wo.setFieldValue('custbody_fieldonWO', myCustTransColFieldfromSO);//(can add more Custom Transaction Body Fields on the Work Order here)//save the Work OrdernlapiSubmitRecord(wo);}}}
SuiteScript 2.0

/** *@NApiVersion 2.x *@NScriptType UserEventScript */define(['N/record','N/log','N/search'],function(record, log, search){functionsetSOColFieldsonWO_afterSubmit_UE(context){// internal ID of the Work Ordervar wo_internalID = context.newRecord.id;//load the Work Order recordvar wo = record.load({ type: record.Type.WORK_ORDER, id: wo_internalID, isDynamic:true});//internal ID of the Sales Order that the Work Order was created fromvar createdFromSO = wo.getValue('createdfrom');//if the Created From field on the Work Order has a value, do the following:if((createdFromSO !="")&&(createdFromSO !=null)){//conduct search to obtain value of custom transaction line fields on the Sales Order that the Work Order was created from//search filters://- Work Order with the current Work Order's Internal ID//- Mainline is True//- Applied to Transaction is Sales Order//- Applied to Transaction (Sales Order) of the Work Order is the same as the Sales Order that the Work Order was created fromvar filters =newArray(); filters[0]= search.createFilter({ name:'internalid', operator: search.Operator.IS, values: wo_internalID }); filters[1]= search.createFilter({ name:'type', join:'appliedtotransaction', operator: search.Operator.IS, values:'SalesOrd'}); filters[2]= search.createFilter({ name:'mainline', operator: search.Operator.IS, values:'T'}); filters[3]= search.createFilter({ name:'appliedtotransaction', operator: search.Operator.IS, values: createdFromSO });//search columns:// - Applied To Transaction (Sales Order) Internal ID// - Custom Transaction Line Fields from the Applied to Transaction (Sales Order)var columns =newArray(); columns[0]= search.createColumn({ name:"appliedtotransaction"}); columns[1]= search.createColumn({ name:"custcol_fieldonSO", join:"appliedtotransaction"});var mySearch = search.create({ type: search.Type.WORK_ORDER, columns: columns, filters: filters });var searchResults = mySearch.run().getRange({ start:0, end:1});//if there are results from the search, do the following:if(searchResults.length >0){//get value of the first search result's (there should be only one search result) to Custom Transaction Line Fields from the Sales Ordervar myCustTransColFieldfromSO = searchResults[0].getValue({name:'custcol_fieldonSO', join:'appliedtotransaction'});//(can add more Custom Transaction Line Fields from the Sales Order here)//set value of Custom Transaction Line Fields from the Sales Order onto the Custom Transaction Body Fields on the Work Order wo.setValue('custbody_fieldonWO', myCustTransColFieldfromSO);//(can add more Custom Transaction Body Fields on the Work Order here)//save the Work Ordervar woIdSaved = wo.save({ enableSourcing:true, ignoreMandatoryFields:true});}}}return{ afterSubmit: setSOColFieldsonWO_afterSubmit_UE };});