GlideRecord cannot be called from server script in widget editor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:17 AM
I would like to use the value of a field in a record called from a knowledge table in a widget.
But the value is not assigned to gr.
Server Script
(function() {
var gr = new GlideRecord('kb_knowledge');
if (gr.next()) {
var shortDescription = gr.short_description;
gs.info('shortDescription: ' + shortDescription);
data.shortDescription = shortDescription;
} else {
// Knowledge Base record not found.
gs.info('Knowledgebase record not found.');
}
})();
This server script will be “else”.
Is there any configuration I need to consider?
Please let me know if there is another way to do this!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:40 AM - edited 06-11-2024 12:42 AM
Hi @Ayumu Hibi ,
It seems like you're trying to retrieve the value of the short_description field from a knowledge base record (kb_knowledge) and pass it to a widget. If the script is always entering the else block, it suggests that there might not be any records found in the kb_knowledge table.
Here are a few things you can check:
Permissions: Ensure that the user executing the script has appropriate permissions to read records from the kb_knowledge table.
Query Criteria: If you're expecting records to exist in the kb_knowledge table, you might want to add some criteria to your GlideRecord query to ensure that you're retrieving the correct record. For example:
javascript
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('your_condition_field', 'your_condition_value');
gr.query();
if (gr.next()) {
// Retrieve the short_description field
} else {
// Knowledge base record not found with the specified criteria
}
Check Data: Manually verify that there are records in the kb_knowledge table in your instance. Sometimes, the issue might be due to the absence of data.
Debugging: Use the browser's developer tools or the ServiceNow script debugger to inspect the execution of your script and see if any errors are being thrown.
Logging: You're already using gs.info() for logging, which is good. Check the ServiceNow logs to see if any useful information is being logged there.
If you've verified all these points and you're still facing issues, let me know, and we can dig deeper into the problem. Additionally, if you provide more context about the specific requirements or any error messages you're encountering, I can provide more targeted assistance.
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 03:52 AM
Since we were able to confirm the correctness of the authorization and query conditions, we still believe that there is a problem with GlideRecord.
I was able to confirm that gr.update() works, but GlideRecord does not seem to be reading the table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:41 AM
@Ayumu Hibi
Add gr.query() before using gr.next()..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 04:02 AM
I tried that and no value went into the gr