I’m having an issue with setting a sublist field w...
# suitescript
t
I’m having an issue with setting a sublist field with a client script in SuiteScript 2.0. Here’s the situation: I have a fieldChanged entry point that looks for a body field to change. When that body field changes, it takes that body field value and attempts to the sublist value on all the existing line items like so:
Copy code
function updateFieldOnAllLines(dataIn) {
    if (!dataIn.rec || !dataIn.sublist || !dataIn.field) {
        return;
    }

    var rec = dataIn.rec;
    var sublist = dataIn.sublist;
    var field = dataIn.field;
    var ignoreFieldChange = dataIn.ignoreFieldChange;
    var initLine = null;

    for (var i = 0; i < rec.getLineCount(sublist); i++) {
        if (!initLine) {
            initLine = rec.getCurrentSublistIndex({
                sublistId: sublist,
                fieldId: field.fieldId
            })
        }
        rec.selectLine({
            sublistId: sublist,
            line: i
        });

        rec.setCurrentSublistValue({
            sublistId: sublist,
            fieldId: field.fieldId,
            value: field.fieldValue,
            ignoreFieldChange: ignoreFieldChange || false
        })

        rec.commitLine({
            sublistId: sublist
        });
    }
}
Here is the problem: There is another field on the sublist that sources the value from the first sublist field whenever that field changes. When the script goes through to update the sublist field, only the very first line gets updated, both the field I change via code and the field that gets sourced. When the script is finished running, I can see in the UI that the first line is still in edit mode which makes me think that the commitLine function is never running. Things to note: When I use ignoreFieldChanged: true when setting the current sublist value, all the fields properly get set via code but, predictably, the sublist field that sources doesn’t change. I have tried using postSourcing instead of fieldChanged but I am running into the same issue. Also, when I console.log the value of the sublist field after committing the line, it is showing the correct value, though it still shows the old value in the UI. Any help with this would be much appreciated!
b
Weird sublist behavior means you should use forceSyncSourcing.
t
You are my hero @battk thank you! This solved my problem perfectly