How to use g_form.addInfoMessage with GlideRecord Query in UI Macro?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2018 06:39 PM
I am fairly new to scripting, so go easy. I suspect my problem is probably pretty trivial.
I am taking my first pass at creating a UI macro and running in to a problem with getting results from my GlideRecord query to show up when I add an info message.
Objective: Add UI Macro (button) to a form and upon click it will query a table and display results as an info message on the form.
The code works, aside from not posting the results in to the Info Message. The Info Message will show up on the form but it will be blank. If I run the same code in the background script and print the results it successfully retrieves records. I cannot figure out why it won't post the results in the info message.
<script>
function RetrieveRecords(reference) {
var user = g_form.getReference('caller');
var kbviews = new GlideRecord('kb_use');
var results = '';
kbviews.addQuery('user', user);
kbviews.addQuery('sys_created_on>=javascript:gs.beginningOfLast45Minutes()');
kbviews.query();
while (kbviews.next()){
results +=kbviews.getDisplayValue('article.short_description');
}
g_form.addInfoMessage(results);
}
</script>
Note: I realize in the current form the 'results' would be poorly formatted. I am just trying to get the data to display and will then work on formatting.
Thank you for any help!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2018 09:29 AM
Hi,
I think you should use gs.addInfoMessage() instead of g_form.addInforMessage().
Please check once with this.
Please mark it correct if this resolves your problem.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 07:26 AM
Hi Eric,
A UI macro runs on the client (browser) side, not server side so you can't use this solution.
The code runs on the server (in scripts - background), but the UI action/ info message is generated client side, so there's nothing added to your message - the data on kb_use is not stored in your browser when the page loads, so the gliderecord query will fail and this is why the info message is blank.
You need to use Glide Ajax to do the gliderecord part of your code - see HowTo - Ajax Calls: Use GlideRecords on the client... - ServiceNow Community