Hi there, I’d like to update the rate coming from...
# suitescript
n
Hi there, I’d like to update the rate coming from the price level to the actual when transforming an opportunity into a quote. I can make it work in the console with selecting the line (selectLine) and setCurrentSublistValue. But when I try in a script it does not work. I’ve tried beforeLoad but I think we can’t do this in this type of script, so then I tried with pageInit but I’m failing again 😔. I’ve checked with lineValidation and sublistUpdate but they don’t trigger. Would you have any tips, please?
a
right, you can't edit data in the beforeLoad UE but you should be able to do this in a pageInit of a client script you haven't shared your code, and you haven't said how it doesn't work, do you get an error? is ignored? can you at least get a log from the pageInit?
n
I will as soon as I’m in front of my computer 😇 thanks Anthony.
I've tried multiple things... With the currentRecord module, getCurrentSublistValue or getSublistValue don't work. With the record module, it works with getSublistValue (but I believe - I did not re-check since yesterday night - getCurrent does not work). I can see it with the log. Then all set option don't work in any case, and also I tried adding cancelLine because the UI is showing a popup "Please choose an item..." And I need to press ok twice for every line. But it's still not working perfectly 😕 (I believe it's also linked to ignoreFieldChange.) I've tested so many things that I'm now lost in my tests 🤦🏻‍♂️ I've found out that we may need to use fireSlavingSync but it still does not work.
Copy code
function pageInit(scriptContext) {
            log.debug('scriptContext', scriptContext.mode);

            if (['copy'].indexOf(scriptContext.mode) < 0) {
                return null;
            }

            // let rec = scriptContext.currentRecord;
            let rec = cr.get();

            let transformFrom = rec.getValue({
                fieldId: 'transform'
            })
            log.debug({
                title: 'transformFrom',
                details: transformFrom
            })

            if (['opprtnty'].indexOf(transformFrom) < 0) {
                return null;
            }

            let itemSublist = rec.getLineCount({
                sublistId: 'item'
            })

            log.debug({
                title: 'itemSublist',
                details: itemSublist
            })

            rec.cancelLine({
                sublistId: 'item'
            })

            for (let i = 0; i < itemSublist; i++) {

                rec.selectLine({
                    sublistId: 'item',
                    line: i
                })



                rec.cancelLine({
                    sublistId: 'item'
                })

                const currentPriceLevel = rec.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'price',
                    // line: i
                });

                const currentRate = parseFloat(rec.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'rate',
                    // line: i
                }));

                log.debug({
                    title: 'currentPriceLevel & rate',
                    details: currentPriceLevel + ' | ' + currentRate
                })

                // rec.selectLine({
                //     sublistId: 'item',
                //     line: i
                // })

                console.log(i);

                // rec.selectLine({
                //     sublistId: 'item',
                //     line: i
                // })

                rec.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_acs_opp_rate',
                    value: currentRate,
                    ignoreFieldChange: false,
                    // forceSyncSourcing: false
                })

                if (currentPriceLevel < 0) {
                    console.log('custom');

                    rec.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'rate',
                        value: '10.00', //parseFloat(currentRate),
                        ignoreFieldChange: false,
                        // forceSyncSourcing: true
                    })
                } else {
                    console.log('not custom');
                    // rec.setCurrentSublistValue({
                    //     sublistId: 'item',
                    //     fieldId: 'price',
                    //     value: '-1',
                    //     // line: i,
                    //     // ignoreFieldChange: true,
                    //     // forceSyncSourcing: true
                    // })
                    // rec.setCurrentSublistValue({
                    //     sublistId: 'item',
                    //     fieldId: 'rate',
                    //     value: parseFloat(currentRate),
                    //     // line: i,
                    //     // ignoreFieldChange: true,
                    //     // forceSyncSourcing: true
                    // })
                    // rec.commitLine({
                    //     sublistId: 'item',
                    //     ignoreRecalc: true
                    // })
                    // rec.selectLine({
                    //     sublistId: 'item',
                    //     line: i
                    // })
                    rec.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'price',
                        value: currentPriceLevel,
                        ignoreFieldChange: false,
                        fireSlavingSync: true
                    })
                }

                rec.commitLine({
                    sublistId: 'item',
                    ignoreRecalc: false
                })
            }
        }
b
in general you must use forceSyncSourcing whenever you commit the line in the currentRecord
and you want to avoid the use of ignoreFieldChange, which is also used internally by netsuite
n
I’ve tried both forceSyncSourcing and fireSlavingSync in vain 😔
b
what did your attempt using them look like
n
I’ll retry later and let you know. But I believe the value just not update.
What I really don’t understand is why getCurrentSublistValue does not return anything 😔
a
I have a number of comments / questions... 1. why
cr.get()
instead of the commented out out
scriptContext.currentRecord
? I think both should be fine, but I'd always use the scriptContext one. 2. why do you mix log.debug and console.log? I always just use console.log in client scripts, again this isn't an issue and its supposed to work to show logs on script records I think? I just never switched. 3. what IS and ISNT logging, the code is only half the story we need to see what's happening at run time. 4. Why do you select and then cancel lines? I don't think I've seen that pattern before, I don't know that its doing anything bad but it just looks strange.
n
1. Just to try out since it was not working with
script Context.currentRecord
2. Because I was tired to always go back to the NetSuite log 😉 and it’s eventually easier in the console. 3. the list on item price level is not updated and the custom column is not as well. I.e. all set…Value don’t work 4. I explained in my post earlier. I tried to do that since I kept having a dialog box saying « please choose an item… » NB: this code is ugly because I’ve tried a zillion things to get this thing working in vain…
b
the important question to ask is which line is selected when you do
Copy code
rec.selectLine({
  sublistId: "item",
  line: i,
});

rec.cancelLine({
  sublistId: "item",
});

const currentPriceLevel = rec.getCurrentSublistValue({
  sublistId: "item",
  fieldId: "price",
});

const currentRate = parseFloat(
  rec.getCurrentSublistValue({
    sublistId: "item",
    fieldId: "rate",
  }),
);
if you dont know, you can check in the ui yourself, cancelLine is equivalent to clicking the cancel button same as selectLine is equivalent to clicking a sublist line
n
Sorry again for the messy script, played so much with it yesterday that it's a remnant. This is not what's causing the issue though 😉 • getCurrent... does not work. I can only get the data with using getSublistValue. • however, setSublistValue gives the error "TypeError: rec.setSublistValue is not a function" when setCurrentSublistValue seems to pass but not updating the value.
b
the code you shared is a mess and contains multiple problems
toss it
start with a simple script that only uses selectLine and getCurrentSublistValue
n
I started with is basic one. getCurrentSublistValue never worked. Only worked with getSublistValue. I wonder if it's a timing issue... Maybe I should add a setTimeout 😕 when I write the same thing in the console step by step it works.
b
what did you basic script look like
n
Copy code
function pageInit(scriptContext) {
    log.debug('scriptContext', scriptContext.mode);

    if (['copy'].indexOf(scriptContext.mode) < 0) {
        return null;
    }

    let rec = scriptContext.currentRecord;

    let transformFrom = rec.getValue({
        fieldId: 'transform'
    })
    log.debug({
        title: 'transformFrom',
        details: transformFrom
    })

    if (['opprtnty'].indexOf(transformFrom) < 0) {
        return null;
    }

    let itemSublist = rec.getLineCount({
        sublistId: 'item'
    })

    log.debug({
        title: 'itemSublist',
        details: itemSublist
    })

    for (let i = 0; i < itemSublist; i++) {

        const currentPriceLevel = rec.getSublistValue({
            sublistId: 'item',
            fieldId: 'price',
            line: i
        });

        const currentRate = parseFloat(rec.getSublistValue({
            sublistId: 'item',
            fieldId: 'rate',
            line: i
        }));

        log.debug({
            title: 'currentPriceLevel & rate',
            details: currentPriceLevel + ' | ' + currentRate
        })
    }
}
And if I switch to selectLine and getCurrent, the log is empty. I mean no value returned
b
thats not an attempt at writing a script that only uses selectLine and getCurrentSublistValue
n
I added a setTimeout of 1 sec and it works.