Search code examples
javascriptnetsuitesuitescript

Suitescript 2.x client script works perfectly in Firefox but stops functioning in Google chrome


I wrote a simple client script using the saveRecord function to copy some info from the header values to the line item fields that correspond. When testing on Firefox I get the expected outcome of each line item field populated with the respective field from the header however, when I test on google chrome no errors are thrown but the script stops working resulting in values seemingly being copied but the lines aren't committed so the data disappears. Sometimes it will not save at all and get stuck on a search box (one of the fields is a select field) with the correct data in it but a slash(/) mysteriously added at the end. Does anyone know a fix?

function saveRecord(scriptContext) {

        try{
            var context = scriptContext.currentRecord
            var ran = false;
            var classValue = context.getText({
                fieldId: 'class'
            })
            var jobNumber = context.getText({
                fieldId: 'custbody_job_number'
            })
            var count = context.getLineCount({
                sublistId: 'item'
            })
            if(context.getText({fieldId: 'custbody_job_number'}) != '' && context.getText({fieldId: 'class'}) != '' ){
                console.info('All fields have data prepare for transfer')
                for(var i = 0; i <= count; i++){ 
                    context.selectLine({
                        sublistId: 'item',
                        line: i
                    })
                    if(context.getCurrentSublistText({sublistId: 'item', fieldId: 'item'}) != ''){
                        context.setCurrentSublistText({
                            sublistId: 'item',
                            fieldId: 'class',
                            text: classValue
                        })
                        context.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'customer',
                            value: jobNumber
                        })
                        context.commitLine({ 
                            sublistId: 'item',
        
                        })
                    }   
                }
                ran = true
            }```

Solution

  • It would really be lovely if setCurrentSublistValue returned a Promise for SS 2.1

    One issue is with your code is that you are over indexing the sublist:

    for(var i = 0; i < count; i++){ //for 0 based indexes the last index is count - 1
    

    However what is happening is that you are committing the line before the customer field finishes sourcing.

    What you want to do at least is:

    context.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'customer',
        value: jobNumber,
        forceSyncSourcing: true // so don't start to commit until sourcing is complete
    })
    

    Sometimes this isn't enough and I've had to resort doing things like putting my sublist indices in an array something like:

    var itemLines = Array.from({length:count}, (_, i)=>i); // modern browsers
    
    function runLines(){
        if(!itemLines.length) return;
        var nxt = itemLines.shift(); // or .pop()
        // do things
        context.commitLine({ 
            sublistId: 'item',
        });
        setTimeout(runLines, 300); // delay by experimentation. add some for daily variability
    }
    runLines();