Corey Schwoebel
11/14/2024, 6:53 PMCorey Schwoebel
11/14/2024, 7:09 PMasync function postSourcing(context) {
if (context.sublistId == 'item' && context.fieldId == 'item') {
try {
const currentRecord = context.currentRecord;
const itemId = await currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
const currentPriceLevel = await currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'price'
});
let description = await currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'description'
})
const currentQuantity = await currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity'
})
const currentQuantityConversionRate = await currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'unitconversionrate'
})
if (!itemId) return;
if (!priceLevelsToChange.includes(currentPriceLevel)) return;
const saleOrCloseoutData = await getSaleOrCloseoutdata(itemId);
const priceLevel = saleOrCloseoutData?.priceLevel;
const rate = saleOrCloseoutData?.price;
let saleEndDate;
if (saleOrCloseoutData?.saleEndDate) saleEndDate = moment(saleOrCloseoutData.saleEndDate).format('MM/DD/YYYY')
if (priceLevel) {
if (priceLevel == salesPriceLevel) {
description += `\n This item is on sale until ${saleEndDate}.`;
} else {
description += `\n This item is on sale as a Closeout item.`;
}
await currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'price',
value: priceLevel,
forceSyncSourcing: true,
ignoreFieldChange: true
});
await currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'description',
value: description,
forceSyncSourcing: true
});
await currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: rate,
forceSyncSourcing: true
})
await currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount',
value: Number(currentQuantity) * Number(currentQuantityConversionRate) * rate,
forceSyncSourcing: true
})
}
} catch (error) {
handleAndEmailError(error);
}
};
};