0

I have a suitescript that is supposed to check a box and then create journal entries when new invoices are created or old ones are updated. However, when importing a csv with the invoices that I want to update it only checks the box but does not create the proper journal entry. I've checked the 'Run Server SuiteScript and Trigger Workflows' in 'Import CSV Preferences' and have done everything I can think of, and I'm still stuck. Any help is welcome. You can find the script below:

/** *@NApiVersion 2.1 *@NScriptType UserEventScript */

define(['N/record'], function(record){

function afterSubmit(context){

    let invoice = context.newRecord;

    let uk_subs = [11,13,14,12,38,39,42,52,54,55,56,57,58,53,46,16,41,18]
    let esp_subs = [47,7,48,5,4,45,3,6,9,10,40]

    let invoice_subs = parseInt(invoice.getValue('subsidiary'));
    let link_anacap = invoice.getValue('custbodyfinaced_anacap_jt');
    let link_journal = invoice.getValue('custbody_journal_created')
    let invoice_num = invoice.getValue('tranid')
    let total_amount = invoice.getValue('custbody_stc_total_after_discount');

    function create_journal(object, account_1, account_2, account_3){

        object.selectNewLine('line');
        object.setCurrentSublistValue('line','account',account_1);
        object.setCurrentSublistValue('line','credit',total_amount);
        object.commitLine('line');

        object.selectNewLine('line');
        object.setCurrentSublistValue('line','account',account_2);
        object.setCurrentSublistValue('line', 'debit',total_amount*0.02);
        object.commitLine('line'); 

        object.selectNewLine('line');
        object.setCurrentSublistValue('line','account',account_3);
        object.setCurrentSublistValue('line', 'debit',total_amount *0.98);
        object.commitLine('line'); 

        return object.save();
    }

    if(context.type == context.UserEventType.CREATE || context.type == context.UserEventType.EDIT){


        if(link_anacap == true && link_journal== false){

            /* record.submitFields({
                id: context.newRecord.id,
                type: context.newRecord.type,
                values:{'custbody_journal_created':true}
            }); */

            let create_jour = record.create({
                type: record.Type.JOURNAL_ENTRY,
                isDynamic : true
            });

            create_jour.setValue('subsidiary', invoice_subs);
            create_jour.setValue('memo', invoice_num);

            record.submitFields({
                id: context.newRecord.id,
                type: context.newRecord.type,
                values:{'custbody_journal_created':true}
            });

            if(esp_subs.includes(invoice_subs)){
                create_journal(create_jour, 1027,1008,5919)
            }

            else if(uk_subs.includes(invoice_subs)){
                create_journal(create_jour,1022, 1008, 7586)
            }
        } 
        } else if(link_anacap == true && link_journal== true){}

    }

1 Answers1

0

Go to the script deployment record for the script that is creating the JEs.

On the 'Context Filtering' tab, there is an 'Execution Context' field. Ensure that 'CSV Import' context is selected.

Mike Robbins
  • 3,184
  • 15
  • 20
  • Hi Mike, it is already selected. Could it be something related to how I'm calling the function create_journal? – juanpellorente Oct 13 '21 at 07:31
  • Are you getting any errors in the CSV import result? Does the CSV status show X of X imported successfully? If the JE creation fails, the invoice would still be created and the box would still be checked. I would wrap the call to `object.save()` in a `try/catch` and add some logging to see why it's failing. – Mike Robbins Oct 13 '21 at 16:39