- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2025 02:05 AM
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;
}
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2025 02:21 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2025 02:21 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader