RyanM-02
12/18/2023, 3:07 AMRyanM-02
12/18/2023, 3:09 AMbattk
12/18/2023, 4:31 AMRyanM-02
12/18/2023, 12:19 PMfunction createEditDemandPlan(itemObj){
try{
if(itemObj.demandPlan.length > 0){
record.delete({
type: record.Type.ITEM_DEMAND_PLAN,
id: itemObj.demandPlan
})
}
let demandPlan = record.create({
type: record.Type.ITEM_DEMAND_PLAN,
isDynamic: true,
});
demandPlan.setValue({fieldId: 'demandplancalendartype', value: 'WEEKLY' })
demandPlan.setValue({fieldId: 'subsidiary', value: 2})
demandPlan.setValue({fieldId: 'location', value: 16 })
demandPlan.setValue({fieldId: 'item', value: itemObj.item})
// demandPlan.setValue({fieldId: 'year', value: null});
// demandPlan.setValue({fieldId: 'month', value: null});
demandPlan.setValue({fieldId: 'startdate', value: formatDate('12/1/2023') })
demandPlan.setValue({fieldId: 'enddate', value: formatDate('12/31/2024') })
const id = demandPlan.save()
let loadedDemandPlan = record.load({
type: record.Type.ITEM_DEMAND_PLAN,
id: id,
isDynamic: false,
});
//demandPlan.setValue({fieldId: 'year', value: '2024'});
loadedDemandPlan.setValue({fieldId: 'startdate', value: '12/1/2023', forceSyncSourcing: true})
loadedDemandPlan.setValue({fieldId: 'enddate', value: '12/31/2024', forceSyncSourcing: true})
loadedDemandPlan.setValue({fieldId: 'demandplancalendartype', value: 'WEEKLY' })
// demandPlan.setValue({fieldId: 'startdate', value: null})
// demandPlan.setValue({fieldId: 'enddate', value: null})
// demandPlan.setValue({fieldId: 'year', value: null});
let currStart = loadedDemandPlan.getValue({fieldId: 'startdate'})
let currEnd = loadedDemandPlan.getValue({fieldId: 'enddate'})
log.debug('start and end before loop', currStart + ' ' + currEnd);
let detailLines = loadedDemandPlan.getLineCount({sublistId: 'demandplandetail'});
//let detailLines = 53;
log.debug('detailLInes', detailLines);
let startToPass = new Date('12/3/2023');
let endToPass = new Date('12/31/2024');
let sundaysCount = getFirstSundayDatesInRange(startToPass, endToPass)
log.debug('sunday count', sundaysCount.length);
let firstSunday = getFirstSundayOfMonth(startToPass)
let foundLine = loadedDemandPlan.findSublistLineWithValue({sublistId: 'demandplandetail', fieldId: 'startdate', value: formatDate(firstSunday)})
log.debug('foundline - 12/3/2023', foundLine);
for(let i = foundLine; i < foundLine + sundaysCount.length; i++){
log.debug('line index', i);
loadedDemandPlan.setSublistValue({sublistId: 'demandplandetail', fieldId: 'startdate', line: i, value: firstSunday})
loadedDemandPlan.setSublistValue({sublistId: 'demandplandetail', fieldId: 'quantity_1_', line: i, value: 99})
firstSunday = addDays(firstSunday, 7)
}
// loadedDemandPlan.commitLine({sublistId: 'demandplandetail'})
const id2 = loadedDemandPlan.save({enableSourcing: true})
}catch(e){
log.error('Caught Error', e.message);
log.error('Caught Error', e.stack);
}
}
RyanM-02
12/18/2023, 12:20 PMbattk
12/18/2023, 1:18 PMbattk
12/18/2023, 1:22 PM