GlideRecord cannot be called from server script in widget editor

Ayumu Hibi
Tera Contributor

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! 

4 REPLIES 4

Astik Thombare
Tera Sage

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:

 

  1. Permissions: Ensure that the user executing the script has appropriate permissions to read records from the kb_knowledge table.

  2. 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

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.

newhand
Mega Sage

@Ayumu Hibi 
Add  gr.query() before using gr.next()..

Please mark my answer as correct and helpful based on Impact.

I tried that and no value went into the gr