I'm having a problem with this code. It returns th...
# suitescript
s
I'm having a problem with this code. It returns this error: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "newRecord" from undefined (/SuiteScripts/rsc_comissao_venda_mr2.js#73)
Copy code
function deleteOld(context) {
        var currentRecord = context.newRecord
        log.debug('cur', currentRecord)
        if (context.type === context.UserEventType.EDIT) {
            var comissao, valorTotalItem
            var recalcularComissao = currentRecord.getValue('custbody_rsc_recalcular_comissao');
            var lineTime = currentRecord.getLineCount({
                sublistId: 'item'
            })

            for (var line = 0; line < lineTime; line++) {
                /*
                var valorTotalItemVenda = currentRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'amount',
                    line: line
                })
                */
                log.debug('currentRecord.id', currentRecord.id)


                var searchComissao = search.create({
                    type: "customrecord_rsc_comissoes",
                    filters: [
                        ["custrecord_rsc_comissoes_pedido", "anyof", currentRecord.id]
                    ],
                    columns: [
                        "internalid",
                        "custrecord_rsc_comissoes_total_venda"
                    ]
                }).run().each(function (result) {

                    valorTotalItem = result.getValue('custrecord_rsc_comissoes_total_venda')
                    comissao = result.getValue('internalid')

                    record.delete({
                        type: 'customrecord_rsc_comissoes',
                        id: comissao
                    })

                    return true
                });


                //atualiza a tabela da comissão do calculo para preservar a margem
                search.create({
                    type: "customrecord_rsc_comissao_preservar",
                    filters: [
                        ["custrecord_rsc_comissao_preservar_venda", "anyof", currentRecord.id]
                    ],
                    columns: [
                        "internalid"
                    ]
                }).run().each(function (result) {
                    var idComissaoSomada = result.getValue('internalid')

                    record.delete({
                        type: 'customrecord_rsc_comissao_preservar',
                        id: idComissaoSomada
                    })

                    return true
                })
            }
        }
    }
    deleteOld();
b
you should be able to find the line that causes the error
s
@battk It's that line :
var currentRecord = context.newRecord
b
in this case, its saying context is undefined
s
@battk But it is not defined as a parameter of this function?
function deleteOld(context){}
b
its not saying that context is not defined
thats a different error, a ReferenceError
its saying that context has a value of undefined
which either means you called
deleteOld
with undefined as the first parameter
or that you didnt call the function with a parameter at all
s
So, should I define a parameter in this function after calling it?
b
you define parameters while calling the function
😄 1
s
Thank you very much, @battk 😄