Hi, I'm a newbie in scripting and I'm trying to de...
# suitescript
l
Hi, I'm a newbie in scripting and I'm trying to default the value of Department (line) based on the Account Type of the Account selected under the Expense sublist of Check/Bill transactions. This script below was working when the boxed portion was set to == 'Other Current Asset'. When I added the other Account Types using ||, it stopped working. Any thoughts? Thank you!
b
javascript thing
you are using the or operator on a bunch of strings
👍 1
the output of that will be a single string, the first non empty one
in this case the first
you want to separate out the strings x === 'a' || x === 'b'
e
Yes your expression effectively reduces to
lineAccountType == true
as
( "string1" || "string2" || ...)
will always evaulate to
true
b
or put all the strings in an array and use indexOf or includes
👍 2
@erictgrubaugh im actually saying something different that you,
( "string1" || "string2" || ...)
is equal to
"string1"
so the code should work for "Bank"
that said, the fix is still the same
e
The fix is the same, and yes, it would be equal to
"string1"
, which will always evaluate to
true
b
if ( myVariable == ( "string1" || "string2" || ...))
is equivalent to
if ( myVariable == ( "string1" ))
which is not the same as
true
there is a parenthesis in a key location
a
With that many strings you may want to use an array and
indexOf.
l
Thanks for all the help. I still get this error: Syntax error: missing : after property id I thought it's N/A to Arrays but to actual Objects. I also tried to put the array after var lineAccountTypes (if it matters).
b
thats normally a problem your ide should help you with
you can try sharing the actual text of your code
m
Also make sure your script is running as 2.1 if you want to use
includes
l
message has been deleted
I used indexOf instead this time. The same error persists. It's just an Array not an actual Object with curly braces but still pushing the property id error. Hmmm.
b
pictures of code is much less useful than text of code for syntax errors
l
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
 
define(['N/currentRecord', 'N/search'], function(currentRecord, search) {
function fieldChanged(context) {
var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
var sublistFieldId = context.fieldId;

if (sublistName == 'expense' && sublistFieldId == 'account') {
var bsAccountTypes =    [
                                    'Bank',
                                    'Fixed Asset',
                                    'Other Asset',
                                    'Other Current Asset',
                                    'Accounts Receivable',
                                    'Accounts Payable',
                                    'Credit Card',
                                    'Other Current Liability',
                                    'Long Term Liability',
                                    'Equity'
                                    ];

var lineAccount = currentRecord.getCurrentSublistValue({
                sublistId = 'expense',
fieldId = 'account'
});

var lineAccountType = search.lookupFields({
                type: search.Type.ACCOUNT,
id: lineAccount,
columns: ['type']
}).type[0].text;

if (bsAccountTypes.indexOf(lineAccountType) != -1) {
                    currentRecord.setCurrentSublistValue({
                    sublistId: 'expense',
fieldId: 'department',
value: 203
});
};
};
};

return {
fieldChanged: fieldChanged};
});
b
your ide appears to be failing you here
l
Thanks everyone for the help. It's now working.
b
if you start running into performance problems, you can probably create a custom transaction field that uses sourcing to add the account type to the expense sublist
then you wouldnt need to do a search in your code and instead rely on sourcing
you would probably need to use the postSourcing entry points instead of the fieldChanged entry point if you did so