Brian Brown
05/04/2022, 3:49 PMbattk
05/04/2022, 3:56 PMBrian Brown
05/04/2022, 3:57 PMfunction reduce(context) {
try {
/**
log.debug('context.values is:', JSON.stringify(context.values))
var soId = context.key;
var uniqueKey = context.values //this is not stringified, but is an array of objects
//var tagItem = context.values.tagitem;
log.debug('soId / uniqueKey:', soId +' / '+ uniqueKey)
**/
var soIdKey = context.key
log.debug('soIdKey is:', soIdKey)
if ( context.values.length > 0 ) {
var dataInput = [];
for ( var i = 0 ; i < context.values.length ; i++ ) {
dataInput.push(JSON.parse(context.values[i]))
}
} else {
var dataInput = JSON.parse(context.values[0])
}
var objInput = {};
objInput[soIdKey] = dataInput;
//The key here is to loop through the array elements and parse them individually.
log.debug('objInput is:', JSON.stringify(objInput)); //the entire array of all objects
////load SO/////
var soRec = record.load({
type: record.Type.SALES_ORDER,
id: soIdKey,
isDynamic: true
});
log.debug('Loaded SO / objInput[soIdKey].length:', soIdKey +' / '+ objInput[soIdKey].length)
var headerLocationId = soRec.getValue({fieldId: 'location'})
var headerDepartmentId = soRec.getValue({fieldId: 'department'})
var headerProductLineId = soRec.getValue({fieldId: 'class'})
// Get SO record line item count
var lineCount = soRec.getLineCount({
sublistId: 'item'
});
////for loop////
for(var i = 0; i < objInput[soIdKey].length; i++) {
log.debug('Searching for objInput[soIdKey].uniquekey:', objInput[soIdKey][i].uniquekey)
var indexFound = soRec.findSublistLineWithValue({
sublistId: 'item',
fieldId: 'lineuniquekey',
value: objInput[soIdKey][i].uniquekey
});
log.debug('indexFound is:', indexFound)
if(indexFound == -1) continue
soRec.insertLine({sublistId: 'item', line: indexFound + 1});
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'item', value: objInput[soIdKey][i].tagitem});
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'description', value: 'Scripted Tag Item '});
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'quantity', value: objInput[soIdKey][i].tagquantity});
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'rate', value: 0});
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'location', value: 1});
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'createpo', value: 'DropShip'});
///SET MANDATORY LINE values
if(headerDepartmentId){
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'department', value: headerDepartmentId});
}
if(headerLocationId){
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'location', value: headerLocationId});
}
if(headerProductLineId){
soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'class', value: headerProductLineId});
}
soRec.commitLine({
sublistId: 'item'
});
log.debug('Added line:', Number(indexFound + 1))
/////set as processed//////////
soRec.selectLine({
sublistId: 'item',
line: indexFound,
});
soRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_tags_dropship_processed',
value: true
});
soRec.commitLine({
sublistId: 'item'
});
log.debug('Set line:', indexFound + ' to processed')
}//end sublist for loop
// Save SO
var soSaved = soRec.save({
enableSourcing: false,
ignoreMandatoryFields: true
});
context.write({
key: soIdKey
});
log.debug('Saved SO:', soSaved)
} catch (error) {
log.error('reduce error', error);
}
}
battk
05/04/2022, 3:58 PMBrian Brown
05/04/2022, 4:03 PMbattk
05/04/2022, 4:04 PMBrian Brown
05/04/2022, 4:24 PMbattk
05/04/2022, 4:25 PMBrian Brown
05/04/2022, 4:26 PMbattk
05/04/2022, 4:26 PMBrian Brown
05/04/2022, 4:26 PMbattk
05/04/2022, 4:28 PMBrian Brown
05/04/2022, 4:29 PMbattk
05/04/2022, 4:29 PMBrian Brown
05/04/2022, 4:29 PMbattk
05/04/2022, 4:33 PMBrian Brown
05/04/2022, 4:34 PMbattk
05/04/2022, 4:37 PMBrian Brown
05/04/2022, 4:41 PMbattk
05/04/2022, 4:55 PMBrian Brown
05/04/2022, 4:58 PM