Hey Peeps! I have a script that’s getting a consta...
# suitescript
m
Hey Peeps! I have a script that’s getting a constant error: Cannot read property ‘id’ of null [at Object.afterSubmit (/SuiteScripts/ue_handle_deposit.js4730)] This is the part of the code that’s giving the error. How can I correct this condition? Do i set a UserEventType condition, or can I set a better condition if the oldrecord id doesn’t exist? const afterSubmit = (context) => { const contextDepApp = context.oldRecord; const depAppId = contextDepApp.id; if (depAppId !== null) { const soEntity = contextDepApp.getValue({ fieldId: ‘customer’
a
I think the only event type that wouldn't have oldRecord would be the CREATE? pretty common to have a condition based on CREATE
m
This script works on a deposit application when it’s deleted. I think you are right though, as it just fires on afterSubmit no matter what. So I should narrow it to !== context.UserEventType.CREATE?
Or === context.UserEventType.DELETE?
b
there are multiple event type that you could account for,
or you can place the null check on
contextDepApp
instead
m
@battk What would be the syntax on that? if(contextDepApp===null)?
The instructions on Netsuite say Step 5: Write the Handle Deletion of a Deposit Application User Event Script This script handles the deletion of a deposit application that is associated with a customer refund or customer deposit when a user saves a sales order. So maybe I can just set the condition for deletions
a
well it depends what you need... if you just need it to ONLY run on deletes then use your === DELETE option if you just need it to not run on creates.. then use !== CREATE... if you want it to exit when there's no context.oldRecrd, then use that as the condition?
m
OK. I think i got it now. I’m going to give it a try different ways.
👍 1
b
i personally prefer using on type coercion with truthy and falsy values
you probably need more javascript tutorials if you dont feel confident about how to write a conditional
m
This looks like a good read! Thanks!
a
right you can do
if(!context.oldRecord) return;
then all your code isn't nested in a conditional
m
Nice!
@Anthony OConnor Which line do I place that at? Above or below contextDepApp? const afterSubmit = (context) => { const contextDepApp = context.oldRecord; const depAppId = contextDepApp.id; const soEntity = contextDepApp.getValue({ fieldId: ‘customer’ });
a
well since you'already assigned context.oldRecord to contextDepApp you can put it below there and use
if (!contextDepApp) return;
Copy code
const afterSubmit = (context) => {
            const contextDepApp = context.oldRecord;
            if (!contextDepApp) return;
            const depAppId = contextDepApp.id;
            const soEntity = contextDepApp.getValue({
                fieldId: 'customer'
            });