Client side GlideRecord callback not working

snoozer
Mega Guru

I'm working on a UI page. I need to load some values from a table into a slushbucket for users to choose. The problem is that in the callback function, I can not access the GlideRecord object. Here is my code:

 

function _getAMFEntriesForSlush()
{
    var amfGR = new GlideRecord('u_amf_entry');
    amfGR.query(_amfCallBack);
}

function _amfCallBack(amfGR)
{
     alert("hi" + amfGR);                                      //  I get this alert
     alert("hi " + amfGR.getRowCount());           // I do not get this alert

// none of the following happens
    while (amfGR.next())
	{
        var amfName = amfGR.getValue('u_name');
        var amfID = amfGR.getValue('sys_id');
        amf_slush.addLeftChoice(amfID, amfName);
    }
}

The code to populate the slushbucket never gets run. I put in some alerts to see where the code is failing. I get that first alert, and it says "hi [object Object]". I do not get the second alert, which tells me there's a problem with referencing the GlideRecord object. Why?

 

What's really frustrating is that I have done this exact thing before and it works perfectly. I copied that code to write this. The other code works, but this doesn't.

 

Here's a the reference I used about client side GlideRecords:

https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/...

 

Any help or thoughts would be appreciated. Thanks.

1 ACCEPTED SOLUTION

snoozer
Mega Guru

I ended up creating a whole new UI Page and copying the code into the new page. It works with no changes. No idea why the previous page didn't work.

View solution in original post

10 REPLIES 10

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @snoozer - To populate the slushbucket, it is good practice to use aserver-side GlideRecord query to retrieve the data, then pass the result to the client-side slushbucket using GlideAjax.

 

If you would prefer not to go that route, something like this should work:

// Called when the UI page loads
addLoadEvent(function () {
    //Load the users when the UI page loads
    amf_slush.clear();

    _getAMFEntriesForSlush();

    return false;
});

function _getAMFEntriesForSlush() {
    var amfGR = new GlideRecord('u_amf_entry');
    amfGR.query(_amfCallBack);
}

function _amfCallBack(amfGR) {
    if (!amfGR)
        return;
    while (amfGR.next()) {
        var amfName = amfGR.getValue('u_name');
        var amfID = amfGR.getValue('sys_id');
        amf_slush.addLeftChoice(amfID, amfName);
    }
}

 

I may have to try the AJAX, but I really didn't want to bother with it.

 

What you wrote is exactly what I have and it's what's not working.

Sounds like gremlins 😀

 

Glad it's working. I was coming back to update...I did pop that code sample into a UI page (updated to work with 'sys_user'), works as expected:

 

SheldonSwift_0-1737145182274.png

 

Haha. I never seem to account for gremlins.

I ended up copying my code to a brand new UI page and it works with no changes. Just so weird.