1

In SuiteScript, I am attempting to run a saved search but I got an error "Cannot read property 'length' from null" . See my code :

 if(search.length>0){
    layout += '<tr height="1cm">';
    layout += '</tr>';
    layout += '<tr>';
    layout += '<td align="right" border="0px" colspan="8" style=\"font-size:12px;\">';
    for(var x = 0;x<search.length;x++){
    layout += iE(search[x].getText('account'))+'<br/>';
    }
    layout +=  '</td>';
    }

also I tried this code also but same result.

if(search!==null %% search.length!==null){
            layout += '<tr height="1cm">';

            layout += '</tr>';
            layout += '<tr>';
            layout += '<td align="right" border="0px" colspan="8" style=\"font-size:12px;\">';
            for(var x = 0;x<search.length;x++){
                layout += iE(search[x].getText('account'))+'<br/>';

            }
            layout +=  '</td>';
            }
Rodel
  • 29
  • 5

7 Answers7

1

try using &&

if(search!==null && search.length!==null){
スージン
  • 143
  • 8
1

Please replace the %% by && (as @scarlet witch suggested) and add little more context.

What I can deduce from your question is that the global variable search is set null at some point of your code, since this is a global variable a lot things could be wrong here, I would recommend use Chrome DevTools and check whats happening.

Please update your question so we may be able to help.

Community
  • 1
  • 1
H. J. Rhenals
  • 135
  • 3
  • 7
1

I suppose you want to check if it's array or not

function isArray(obj){
   return !!obj && Array === obj.constructor;
}

and use it in if statement like

if(isArray(search)){
   // implementation
}

i don't know why you check search.length !== null if you want to check if there is element in array or not you should check by search.length >=0.

ArrayVar.length never be null unless you explicitly set ArrayVar.length = null. it will be 0 by default.

Elec
  • 1,699
  • 2
  • 13
  • 20
1

It may be that search is undefined when the execution reaches this line. Try using

if(typeof search !== "undefined" && search.length!==null){
Jeanger
  • 92
  • 4
0

You should be doing:

if(search!==null && search.length!==null){

The AND operator is &&

Also try if(search && search.length>0){

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

Just use : if(search)

Note that using !== or === means, JavaScript checks for both type of the variable and value.(Strict Equality)

Examples:

var search = "";

search !== null; //true
search.length !== null; //true
search.length === "0"; //false
search.length === 0; //true
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
0

How are you populating the search variable? Is it supposed to be the results of a search?

nlapiSearchRecord returns null when there are no matching results for your search.

The simplest way to check this is just something like:

if (search && search.length) {
    // Process results
}

The pattern that I follow whenever I am working with searches and results is to create one function that performs the search and always returns an array, and another function containing the logic for processing a single search result.

function findRecords() {
    var filters = [...];
    var columns = [...];
    // The `|| []` clause ensures we always return an array, never null
    return nlapiSearchRecord(recordType, null, filters, columns) || [];
}

function processResult(result) {
    // Process a single result
}

// Run search and process all results
findRecords().map(processResult);

You can read more about handling null here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28