NetSuite Restlet-USER_ERROR

时间:2019-03-10 18:59:32

标签: netsuite suitescript suitescript2.0

我正在创建此脚本以在NetSuite中写发票。但是,当我发送数据时,出现此错误:“仅在选择批准工艺路线首选项时,才应使用批准状态字段。” enter image description here PS:在界面上尝试保存时会发生相同的错误。 enter image description here 你能帮助我吗?这是代码:

    /**
 * @NApiVersion 2.x
 * @NScriptType restlet
 * @author Adriano Barbosa
 * @since 2019.2
 */

define(['N/record', 'N/file', 'N/log', 'N/search'], function (record, file, log, search) {
    function vpc_contas_receber(context) {
        if ( context.cnpj_forn ) {
            var id_forn;
            search.create({ type: "customer",
                filters: [
                    [ "custentity_enl_cnpjcpf", "is", context.cnpj_forn ]
                ],
                columns: [
                    search.createColumn({ name: "internalid", label: "ID interna" })
                ]
            }).run().each(function(result) {
                id_forn = result.id;
                return true;
            });

            if ( !id_forn ) { 
                log.debug({ title: 'Erro', details: 'Fornecedor ' + context.cnpj_forn + ' não localizado!' });
                return { status: 'Erro', mensagem: 'Fornecedor ' + context.cnpj_forn + ' não localizado!' }
            } else if ( context.nf ) {
                var id_nf_existe, nf_existe, num_fat;
                search.create({ type: "invoice",
                    filters: [
                       [ "type", "anyof", "CustInvc" ], "AND", 
                       [ "custbody_enl_fiscaldocnumber", "is", context.nf ]
                    ],
                    columns: [
                       search.createColumn({ name: "custbody_enl_fiscaldocnumber", label: "Número da Nota Fiscal" }),
                       search.createColumn({ name: "formulatext", formula: "SUBSTR({tranid}, 0)", label: "nº fatura" })
                    ]
                 }).run().each(function(result) {
                    id_nf_existe = result.id;
                    nf_existe = result.getValue({ name: 'custbody_enl_fiscaldocnumber' });
                    num_fat = result.getValue({ name: 'formulatext' });
                 });

                 if ( nf_existe ) {
                    log.debug({ title: 'NF ' + nf_existe + ' já cadastrada!', details: 'VPC: ' + id_nf_existe + '; ' + 'Nº Fatura: ' + num_fat });
                     return { 
                         status: 'NF ' + nf_existe + ' já cadastrada!', 
                         mensagem: 'VPC: ' + id_nf_existe + '; ' + 'Nº Fatura: ' + num_fat
                     }
                 } else {
                    vpc = record.create({ type: 'invoice', isDynamic: true })
                            .setValue({ fieldId: 'entity', value: id_forn })
                            .setValue({ fieldId: 'memo', value: context.obs })
                            .setValue({ fieldId: 'location', value: 1 })
                            .selectNewLine({ sublistId: 'item' })
                            .setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: 27901 })
                            .setCurrentSublistValue({ sublistId: 'item', fieldId: 'rate', value: context.valor })
                            .commitLine({ sublistId: 'item' })/*
                            .setValue({ fieldId: 'custbody_rsc_dtvencboleto', value: new Date(context.venc_blt) })*/
                            .setValue({ fieldId: 'custbody_enl_operationtypeid', value: 15 })
                            .setValue({ fieldId: 'custbody_enl_order_documenttype', value: 7 })
                            .setValue({ fieldId: 'custbody_enl_fiscaldocnumber', value: context.nf })/*
                            .save({ enableSourcing: true, ignoreMandatoryFields: true })*/

                    if ( context.venc_blt ) {
                        var venc_blt = new Date(context.venc_blt);
                        venc_blt.setDate(venc_blt.getDate());
                        vpc.setValue({ fieldId: 'custbody_rsc_dtvencboleto', value: venc_blt });
                    }
                    var id_vpc = vpc.save({ enableSourcing: true, ignoreMandatoryFields: true });
                    try { 
                        var bodyObject = {};

                        var load_id_vpc = record.load({ type: 'invoice', id: id_vpc });
                        bodyObject['id_vpc'] = id_vpc;
                        bodyObject['doc_vpc'] = load_id_vpc.getValue({ fieldId: 'tranid' });
                        bodyObject['memo'] = load_id_vpc.getValue({ fieldId: 'memo' }) || ''; 
                        bodyObject['valor'] = load_id_vpc.getSublistValue({ sublistId: 'item', fieldId: 'rate', line: 0 }).toFixed(2);
                        bodyObject['nf'] = load_id_vpc.getValue({ fieldId: 'custbody_enl_fiscaldocnumber' });   
                        bodyObject['venc_blt'] = load_id_vpc.getValue({ fieldId: 'custbody_rsc_dtvencboleto' });
                        log.debug({ title: 'VPC["Contas a Receber"] cadastrado com sucesso!', details: bodyObject });
                        return { 'status': 'Sucesso!', 'infoVPC': bodyObject }
                    } catch (e) {
                        log.debug({ title: 'Erro', details: e });
                        return { status: 'Erro!', mensagem: e }
                    }
                 }
            }
        }
        // forn = search.create({ type: "vendor",
        //     filters: [
        //        [ "custentity_enl_cnpjcpf", "is", context.fornecedor ]
        //     ],
        //     columns: [
        //        search.createColumn({ name: "internalid", label: "ID interna" })
        //     ]
        //  }).run().getRange({ start: 0, end: 1 })
    }                
    return { 'post': vpc_contas_receber }
});

1 个答案:

答案 0 :(得分:1)

如消息所示:

  

要使用批准状态字段,必须激活发票的批准路由。

要执行此操作,请转到Setup -> Accounting -> Accounting Preferences -> Tab "Approval Routing"并检查要在其上管理批准的交易。这样即可使用“ 批准状态”。