Hi All, I am using suitescript to create a restlet...
# suitescript
b
Hi All, I am using suitescript to create a restlet that returns a search, however, I am always getting the following error:
TypeError: Cannot call method "split" of undefined
but I have no "`split`" in any of my code. I can share the script if needs be.
m
I've seen that message come up a couple of times recently for unrelated syntax errors. Can you post your code?
✔️ 1
b
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType Restlet
 */
define(['N/search'],

function(search) {

    /**
     * Function called upon sending a GET request to the RESTlet.
     *
     * @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
     * @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
     * @since 2015.1
     */
    function doPost(requestParams) {

        var results = [];
        var slice = [];
        var i = 0;
      	var startDate = requestParams.startDate;
        var endDate = requestParams.endDate;
        var kitchenSubsidiaries = [10, 37, 56];
        var kitchenLocations = [3, 5, 7];

        var brainSearch = search.create({
            type: search.Type.SALESORDER, // change to the ID of your saved search
            id: 'customsearch1166',
            title: 'Kitchen Brain Integration',
            columns: ['internalId','entity','subsidiary','item','units','custbody_document_date'],
            filters: [
              ['status', search.Operators.NONEOF, ["Cancelled", "Closed", "FullyBilled", "PendingApproval"]],
              ['custbody_document_date', search.Operators.WITHIN, startDate, endDate],
              ['subsidiary', search.Operators.ANYOF, kitchenSubsidiaries],
              ['location', search.Operators.ANYOF, kitchenLocations]
            ]
        });

        var resultSet = brainSearch.run();

        do {
            slice = resultSet.getRange({ start: i, end: i + 999 });
            slice.forEach(function(row) {
                var resultObj = {};
                row.columns.forEach(function(column) {
                    resultObj[column.name] = row.getValue(column);
                    });
                results.push(resultObj);
                i++;
            });
        } while (slice.length >= 1000);

        return JSON.stringify(results);
    }

    return {
        post: doPost,
    };

});
I have JSLint'ed it and it seems fine (?)
Does the
define
need to be a
require
? I see conflicting information in NS docs
m
Sorry just got a call.
😃 1
b
Looks like a lot of other people have had the same issue: https://netsuiteprofessionals.slack.com/archives/C29HQS63G/p1581104568354100
m
define
is correct. The docs usually put
require
in the examples to run in the debugger
b
I think it has something to do with returning / iterating through the search (particularly if it is empty)
can you point me to the docs on how to use the debugger?
am new to SuiteScript (only done SuiteTalk so far) (have some JS/TS experience though)
m
Your code doesn't appear to have any errors and nothing jumps out to me. As a sanity check I would replace the whole
doPost
with just
return "Hello World!"
and make sure that works
👍🏻 1
b
@michoel that works. hmmmm. must be the search.
b
You could get that error if the inputs to one of netsuites functions is undefined when it is expected to have a value
b
can you point me to the restlet parameter documentation? Having a hard time getting it to accept the parameters too :/ (I thought you just had to send them in the body with the same parameter name)
b
By functions, i mean things like row.getValue,resultSet.getRange, and search.create
👍🏻 1
Make sure that the values being passed in arent undefined and dont contain undefined keys or values
b
Okay, I am going to log every [suspicious] object to see if it
undefined
:/
Thanks for that @michoel. I was using netsuite.custhelp.com. Have found a few issues with the script from reading and will let you know how I go
okay, so now i am getting actually useful error messages
So I guess thats a step forward
now I have "SSS_INVALID_SRCH_FILTER_EXPR" which i presumes means I am passing in some of the filters wrong
I am going to take a guess and say it is the date comparison on the custom field
here is the updated script: https://pastebin.com/vP3dnSn7 could one of you have a look for me?
b
Could be both status and custom date
b
@battk is that not how you do array filters?
b
Status internal ids dont look like that
And date filter values should be a Date object or a string in the current users date (and maybe time) format
So what is the value of startDate and endDate and what is the current users date format
b
the date filters should be passed in as Date objects, no?
I am sending a ISO 8601 date as both dates
b
Thats a string
Notably not a netsuite formatted string or a Date
b
wont that get de-serialized to a date (from JSON)?
oh I see
b
There is no Date in json
b
Okay, Thank you so much for your help! So, I need SalesOrder status internal IDs & how to handle dates in SuiteScript. Could you point me in the right direction (documentation or otherwise)?
b
Make the search in the ui first
Then either load the search in code to look at the filterExpression
Or use the chrome search export tool
b
well thats good, I already have a search based on this. Would it be in the XML of the search? (add
&xml=t
to the query)
I dont know how to search a custom field in the UI (I thought it was not possible)
I am using the advanced search
b
i prefer using suitescript in the browser console
Copy code
require(["N/search"], function(search) {
  console.log(search.load({ id: "customsearch1166" }).filterExpression);
});
b
@battk Yep, I see what you mean by the Status values. other then that, it is roughly what I expected
wow, running in browser console has just blown my mind
thats going to be useful
@battk one more question, I have everything else working now: I am currently returning
unit
in my results but the output is the
unit.name
. How do I get it to show
unit.internalid
?
b
if you use getValue in your code, it should give you the internal id
getText should be the name
b
the unit is a search column, will that still work? i.e:
Copy code
columns: ['internalId', 'entity', 'subsidiary', 'item', 'unit.getValue'],
b
no
b
for the results?
b
Copy code
resultObj[column.name] = row.getValue(column);
that should get you the internal id value of the unit column
b
yeah, just saw that also!
yep, I can figure out the rest now I think! thanks again!
oh wait, I am already doing getValue on all columns 😭
b
meh
b
so If i return both the Value and Text, for
unit
the Text is always
null
and Value is the
name
:/
b
its a weird column
use the unit id column
b
unitid
and
unit id
are not recognized columns
b
thats unfortunate for you
its a 2020.1 feature
b
well
b
you might have to search for the matching uom if you really need the internal id
or wait 53 days or so
b
matching UoM? match on what? they all have very similar names (some are the same)
b
the item join will allow you to get the units type
b
I dont think a hacky solution will work for UoMs. they are a mess in our system. I would not be surprised if some UoMs of a unit type have the same name
b
the units type is a normal select field that has internal ids
b
I need UoM, not Unit Type unfortunatly
b
get the unit type
do do a search on units of measure for matching internal ids
there should only be one units of measure that matches
to be clear, the units type is a value like Packs
while individual units are theings like 12 Packs, 6 Packs
the item join unit type will allow you to search uom by internal id
b
I think i understand what you mean - we are talking about the same thing, which is good.
b
and the unit name (or abbreviation name) will allow you to find the matching unit
b
I am going to try what you suggest, however I am not sure if i can rely on matching by name due to what I mentioned before - but i will find out.
b
that said, it is possible for individual units within a unit type to share the same name and abbreviation
but that would be rather unusable
b
@battk you havent seen our system! 😭
Thanks for your help, I am clocking off for the day now :)
a
@shadman Ashrafi this is thread on your issue