If you are working on Sales Orders that contain the same item in different rows and would like to combine them into one, here is a sample code that may help:
/**
*@NApiVersion2.x
*@NScriptTypeUserEventScript
*@NModuleScopeSameAccount
*/
define(['N/record'],
function(record){
/**
*Functiondefinitiontobetriggeredbeforerecordisloaded.
*
*@param{Object}scriptContext
*@param{Record}scriptContext.newRecord-Newrecord
*@param{string}scriptContext.type-Triggertype
*@param{Form}scriptContext.form-Currentform
*@Since2015.2
*/
function beforeLoad(scriptContext){
vararrItems=[],itemSummary=[];
varsalesOrd=record.load({
type:record.Type.SALES_ORDER,
id :4746
});
varitemCount=salesOrd.getLineCount('item');
itemSummary[0]=salesOrd.getSublistValue({
sublistId:'item',
fieldId:'item',
line:0
});
for(vari=0;i<itemCount;i++){
varitem=salesOrd.getSublistValue({
sublistId:'item',
fieldId:'item',
line:i
});
varquantity=salesOrd.getSublistValue({
sublistId:'item',
fieldId:'quantity',
line:i
});
if(i==0){
arrItems[0]={'item':item,'quantity':quantity};
}
if(!isExisting(arrItems,item)){
arrItems[arrItems.length]={'item':item,'quantity':quantity};
}else{
for(varj=0;j<arrItems.length;j++){
if(arrItems[j].item==item){
arrItems[j].quantity+=quantity;
}
}
}
}
}
function isExisting(arr,item){
for(vari=0;i<arr.length;i++){
if(arr[i]['item']==item)returntrue;
}
returnfalse;
}
return{
beforeLoad:beforeLoad
};
});
The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee, or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect, or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
Got any feedback? Let us know what you think!