I have a user event script that runs on beforeSubm...
# suitescript
l
I have a user event script that runs on beforeSubmit but for some reason it doesnt trigger on online form submission. It does trigger when i change it to AfterSubmit but i need it to run beforeSubmit
m
@Livio Can you post the code by chance? There are too many variables that could affect this to say for certain without seeing how it's setup.
l
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/search', 'N/record', 'N/runtime', 'N/file', 'N/format', 'N/log'],

    function(search, record, runtime, file, format, log) {


        function FindItemRecord(context) {

            var newRecord = context.newRecord;
            var item_name = newRecord.getValue({
                fieldId: 'custrecord111'
            });

            try {

                log.debug('item_name', item_name);

                var inventoryitemSearchObj = search.create({
                    type: "inventoryitem",
                    filters: [
                        ["type", "anyof", "InvtPart"],
                        "AND",
                        ["name", "is", item_name]
                    ],
                    columns: [
                        search.createColumn({
                            name: "internalid",
                            label: "Internal ID"
                        }),
                        search.createColumn({
                            name: "itemid",
                            label: "Name"
                        })
                    ]
                });

                var results = inventoryitemSearchObj.run();
                var resultsRange = results.getRange(0, 1);

                if (resultsRange.length > 0) {

                    var item_internal_id = resultsRange[0].getValue({
                        name: "internalid",
                        label: "Internal ID"
                    });
                }

                log.debug('item_id', item_internal_id)

                 newRecord.setValue({
                    fieldId: 'custrecord_pic_request_item',
                    value: item_internal_id
                });

  

            } catch (error) {
                log.debug("error", error);
            }
        }
        return {
            beforeSubmit: FindItemRecord,
        };
    });
m
Interesting, so if you changed beforeSubmit: FindItemRecord to afterSubmit: FindItemRecord then it runs like it should? You're seeing the log messages from that but not on beforeSubmit?
l
thats correct. beforeSubmit doesnt trigger at all . i dont see logs. but afterSubmit works perfectly. What could be the reason. Does it matter that this is on create beforeSubmit
m
You don't have any context filtering in your script so the Create part shouldn't affect it. When you upload it as beforeSubmit, check the Script record and make sure that the Before Submit Function is recognized and checked in the script record. Another area to look at is if there are any other UE scripts running on the beforeSubmit function, is there a script running before yours that's failing and stopping the chain of scripts from running?
l
yes beforeSubmit is actually being recognized. This is a custom record i just created and this is the only script running on this record. Maybe thats how online forms work. scripts maybe only trigger on afterSubmit if im creating a record through an online form
m
Ok, I think I might be thinking of something different from you. When you say through an online form, do you mean the record is being created from another process? Not by someone going to the custom record in the UI and creating the record directly?
l
Online HTML form. not through UI. I created an Online HTML form for that custom record.
records are being created through that online HTML form. and the script only triggers after Submit
m
Sorry about that. Yes, I believe that beforeSubmit only applies to when you're working with the record directly. I know it doesn't run when you do something like a record.transform from another script.
l
i see well thanks for your help then
m
You're welcome. If you need the actions from the script before the record is fully saved, you might need to transfer the process to a SuiteLet. Create the record in the suitelet, run your search, set the values, then save from there. It's kinda a work around beforeSubmit process.
l
Makes sense