GlideRecord in UI script

Rosy14
Tera Guru

Hi,

I have below ui script where I want to replace the glideRecord by GlideAjax/g_scratchpad. ALso, want to know how to test if the code is working properly or not

redirectToUrl();

function redirectToUrl() {
grUserPreference = new GlideRecord('sys_user_preference');
grUserPreference.addQuery('user', top.window.NOW.user_id); // picking up current user as g_user doesnt work here.
grUserPreference.addQuery('name', 'sfRedirectPortal');
grUserPreference.query(response);

function response(result) {
        var url = '';
        var sflastRedirect = '';
        if (result.next()) {
            url = result.value;
            sflastRedirect = localStorage.getItem("sflastRedirect");

            if (url != "" && top.window.location.toString().indexOf(url) == -1 && (grUserPreference.sys_updated_on > sflastRedirect || sflastRedirect == null)) {
                localStorage.setItem("sflastRedirect", grUserPreference.sys_updated_on);
                top.window.location = url;
            }
        }
    }
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Rosy14 

your GlideRecord is with callback which should be fine but if you still want you can use GlideAjax

Something like this

Script Include:

var UserPreferenceRedirect = Class.create();
UserPreferenceRedirect.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserPreference: function() {
        var result = {};
        var grUserPreference = new GlideRecord('sys_user_preference');
        grUserPreference.addQuery('user', gs.getUserID());
        grUserPreference.addQuery('name', 'sfRedirectPortal');
        grUserPreference.query();
        if (grUserPreference.next()) {
            result.url = grUserPreference.value.toString();
            result.sys_updated_on = grUserPreference.sys_updated_on.toString();
        }
        return result;
    },

    type: 'UserPreferenceRedirect'
});

UI Script:

redirectToUrl();

function redirectToUrl() {
    var ga = new GlideAjax('UserPreferenceRedirect');
    ga.addParam('sysparm_name', 'getUserPreference');
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        var url = result.url;
        var sflastRedirect = localStorage.getItem("sflastRedirect");

        if (url && top.window.location.toString().indexOf(url) == -1 && (result.sys_updated_on > sflastRedirect || sflastRedirect == null)) {
            localStorage.setItem("sflastRedirect", result.sys_updated_on);
            top.window.location = url;
        }
    });
}

I believe how to test depends on what was the requirement and where is this UI script used.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Rosy14 

your GlideRecord is with callback which should be fine but if you still want you can use GlideAjax

Something like this

Script Include:

var UserPreferenceRedirect = Class.create();
UserPreferenceRedirect.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserPreference: function() {
        var result = {};
        var grUserPreference = new GlideRecord('sys_user_preference');
        grUserPreference.addQuery('user', gs.getUserID());
        grUserPreference.addQuery('name', 'sfRedirectPortal');
        grUserPreference.query();
        if (grUserPreference.next()) {
            result.url = grUserPreference.value.toString();
            result.sys_updated_on = grUserPreference.sys_updated_on.toString();
        }
        return result;
    },

    type: 'UserPreferenceRedirect'
});

UI Script:

redirectToUrl();

function redirectToUrl() {
    var ga = new GlideAjax('UserPreferenceRedirect');
    ga.addParam('sysparm_name', 'getUserPreference');
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        var url = result.url;
        var sflastRedirect = localStorage.getItem("sflastRedirect");

        if (url && top.window.location.toString().indexOf(url) == -1 && (result.sys_updated_on > sflastRedirect || sflastRedirect == null)) {
            localStorage.setItem("sflastRedirect", result.sys_updated_on);
            top.window.location = url;
        }
    });
}

I believe how to test depends on what was the requirement and where is this UI script used.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader