I have a script thats purpose is to adjust the pri...
# suitescript
c
I have a script thats purpose is to adjust the price level of an item on an invoice if that items is on sale. I'm running into an issue if the customer/item has group pricing. This function is able to adjust the price level regardless of the customer/items group pricing:
Copy code
function postSourcing(context) {
     // console.log('PostSourcing. FieldId - ', context.fieldId, ', Sublist is ', context.sublistId);
     if (context.sublistId == 'item' && context.fieldId == 'price') {
      console.log('postSourching - Price level has been changed')
      console.log(context.currentRecord.getCurrentSublistValue({sublistId: 'item', fieldId: 'item'}))
      context.currentRecord.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'price',
        value: 1,
        ignoreFieldChange: true
      })
     }

    };
However, this function does not update the price level:
Copy code
function postSourcing(context) {
            if (context.sublistId == 'item' && context.fieldId == 'price') {
                try {
                    const currentRecord = context.currentRecord;
                    const itemId = currentRecord.getCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'item'
                    });
                    const currentPriceLevel = currentRecord.getCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'price'
                    });
                    let description = currentRecord.getCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'description'
                    })
                    if (!itemId) return;
                    if (!priceLevelsToChange.includes(currentPriceLevel)) return;

                    const saleOrCloseoutData = getSaleOrCloseoutdata(itemId);
                    log.debug('Sale or closeout data', saleOrCloseoutData)

                    const priceLevel = saleOrCloseoutData?.priceLevel;
                    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.`
                        }
                        console.log('Changing price level - ', priceLevel)
                        currentRecord.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'description',
                            value: description,
                            ignoreFieldChange: true
                        });
                        currentRecord.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'price',
                            value: priceLevel,
                            ignoreFieldChange: true
                        });
                    }
                } catch (error) {
                    handleAndEmailError(error);
                }

            };
        };
The "priceLevel" variable does have a value, so the logic is all executing, but it will not update the price level field. Any ideas why the test script works, but the latter does not?
The description isn't changing either
i
Is the console.log('Changing price level - ', priceLevel) outputting?
c
it is, i added a typeof check for pricelevel as well to that log, and the type is correct
If i move the setCurrentSublistValue call above the getSaleOrCloseoutdata call, it works, but if i put it after, it doesn't. Here's the getSaleOrCloseoutdata function just in case:
Copy code
function getSaleOrCloseoutdata(itemId) {
            try {
                const itemLookup = search.lookupFields({
                    type: search.Type.INVENTORY_ITEM,
                    id: itemId,
                    columns: ['custitem_item_on_sale', 'custitem_item_sale_end_date', 'custitem_closeout_item']
                });
                const onSale = itemLookup.custitem_item_on_sale;
                const onCloseout = itemLookup.custitem_closeout_item;
                const saleEndDate = itemLookup.custitem_item_sale_end_date;

                if (onCloseout) {
                    return {
                        priceLevel: closeoutPriceLevel
                    };
                };

                if (onSale) {
                    let endDate = new Date(saleEndDate).getTime();
                    let todaysDate = new Date().getTime();
                    if (todaysDate < endDate) {
                        return {
                            priceLevel: salesPriceLevel,
                            saleEndDate: saleEndDate
                        };
                    };
                };
            } catch (error) {
                handleAndEmailError(error);
            };

        };
i
Can you paste the version of code that does set the values?
c
Looks like it was an async issue. By making the postSourcing function async, and then awaiting each api call, everything is working as it should.
🙌 2
i
Nice catch man
c
Thanks, I appreciate the help!