Hi, I'm new to scripting and I am having a problem...
# suitescript
l
Hi, I'm new to scripting and I am having a problem with this simple script. The goal here is to set a default Department when the account selected is of Other Current Asset type.
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 */
define(['N/currentRecord'],function(currentRecord)
Copy code
{function fieldChanged(context) {
            var currentRecord = context.currentRecord;
Copy code
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId
var line = context.line;
Copy code
var accountType = record.load({
                type: record.Type.ACCOUNT,
                id: accttype})
if (sublistName === 'expense' && sublistFieldName === 'account' && accountType === 'Other Current Asset')
Copy code
currentRecord.setValue({
                    fieldId: 'department',
                    value: 203})
                
});
Copy code
return {fieldChanged: fieldChanged
        };
});
b
its kinda hard to tell from the formatting, but what you shared so far should probably throw a reference error saying that accttype is not defined
☝️ 1
l
Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"syntax error (SS_SCRIPT_FOR_METADATA#20)","stack":[]} Here's the error I got. Couldn't specifically find in SAs.
b
not terribly surprising
what you shared isnt valid javascript
but it suggests that you arent using a tool that can detect javascript syntax errors
l
Do you know which portion is wrong (or all of it)? You don't have to specifically share what should be the correct code.
I'm using VSC 3. Can you suggest a better tool? Thanks!
s
there are no braces around the stuf fthat is supposed to be in the if statement
b
thats actually okay
though dumb to do
s
yeah and the overall end braces arent lining up
b
i can tell you that the problem is an extra parenthesis
s
Also storing the currentRecord in a variable that is the name of the module, really bad idea
b
im not sure what vsc3 is, but if its a modern version of vs code, you should simply be able to name your file with a .js extension to get syntax help
l
message has been deleted
b
see the red squiggly lines
thats it telling you that something is wrong
the first squiggly line is where your problem is
l
Got it. I was able to create the script record but it isn't running as expected. Do you think I have the correct syntax for getting the account type of the account selected?
@Sandii actually I got that format for the variables in SA. But will take note of that. Thanks!
b
nope
the code essentially looks like it was written backwards
it does some of the steps required to do the job, but in the wrong order and missing steps
the first thing you do for a fieldChanged function is identify when you want it to run
the fieldChanged entry point is triggered whenever a field is changed, and you really dont want to run your full logic whenever a field is changed
you only want to run it when the account field is changed
thats what
Copy code
if (sublistName === 'expense' && sublistFieldName === 'account')
is for, and it should be the first thing your code does
parrotbeer right 1
l
Thanks a lot! Will play around it.
b
after you get the first step working, figure out what a reasonable step 2 should be
i can tell you how reasonable it is if you share it
l
message has been deleted
Hi, I've read additional articles and revised my code but it's still not working. Any thoughts? Thank you!
w
See the red squiggly line on the bottom. It looks like you're missing a "}" before the "return:".
@Luis it should look like this
l
@Watz should I use validateField even if it's on a sublist?
The error during upload of the file is SuiteScript 2.0 entry point scripts must implement one script type function. Per SA, I need to define all the used modules which I did?
message has been deleted
s
Your return statement for the whole file is inside your if, see how validateLine is grayed out, it means you aren’t calling it
Also, this will not work as is, glAccountType is an object with properties on it (that’s how search.lookupfields returns) so your next condition will never be true. Log it and you will see how to obtain the string you want from it
l
What is the correct way to pull out the join field if not through search? Record module? Sorry for asking dumb questions.
b
you need to be more careful about changing entry points, not all of them have the same inputs or outputs. validateLine does not have the same values in the scriptContext, which will make your if statement fail
it also requires you to return a value to work correctly
you also need to be more strict about internal ids. Search column internal ids aren't always the same as record internal ids. You can use the record browser and use the Search Columns section to find internal ids
keep in mind what sandii said about glAccountType. You can go through the docs of search.lookupFields to see how it returns values, it wont simply be a string. sandii's suggestion about logging (i recommend console.log) the value of glAccountType works too.
I'd also recommend learning a bit more javascript. You should feel comfortable using the debugger to tell what your code is doing. Or the more primitive use of console logging. Do not use NetSuite to learn javascipt, its terrible at it and there are plenty of reasonable resources on the web about learning javascript.
You are making mistakes with block scoping, which will partly negate all your if statements.
You are also using variables outside of their block scope. While variables have function level scope, its extra confusing to use them that way
you are still making the same sort of mistakes as your first attempt. You need to get your first step (the first if statement) correct before moving on to anything else