gr.next() returns false regardless of what I do!

kim-lindgren
Kilo Sage

I have a client script with a simple GlideRecord query that refuses to work. I have tried many different variants, such as: querying a different table, using various addQuery() filters, exchanging if for while, and more. I feel I must be missing something very basic here but I cannot figure it out.

 

Best regards,

Kim 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var envGr = new GlideRecord('cmdb_ci_environment');

    envGr.query();

    alert(envGr.next()) //returns false

    if (envGr.next()) {
        alert("Come on"); //Does not run
    } else {
        alert("Empty") //Runs
    }

}
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron

@kim-lindgren You are trying to run a GlideRecord query inside a client script, this might work for the native platform but would certainly not work in case of Service Portal.

 

I suggest you to create a Client callable script include and all its method using a GlideAjax call from the client script.

 

Also following line might now work as it does not have semicolon in the end.

alert(envGr.next()) //returns false

 

Replace with 

 

alert(envGr.next()); //returns false

 

 Hope this helps.

View solution in original post

7 REPLIES 7

For performance reasons Glide Record objects are not recommended to be executed at client end.

BTW are you able to get into if block now?

@kim-lindgren GlideRecord is a synchronous API call which is blocked on the Service Portal. 

 

Regarding semicolon, it is a debatable topic in Javascript, sometimes compiler adds it from its own side but in other cases I have seen scripts failing please refer to this URL https://www.geeksforgeeks.org/semicolon-in-javascript/ for one such discussion. 

Hola Kim!

To give a general case , 

var x = y
[1, 2, 3].forEach(...)

here javascript's ASI (Automatic Semicolon insertion) would assume that y[3] trying to access third index of y and many more scenarios this can cause a problem which definitely  we'll encounter shortly cause.
If it helped you sort this please mark this .

Thank you kim.