- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2017 05:06 AM
Hi all,
I didn't know how I should name this topic. But I will explain my question.
I have created a Data Policy, which will make Description is mandatory.
When I use Table API for creating an incident it shows error 403 and in response I get error message that Description is mandatory and it must be filled - That works fine, BUT...
But I need to do the same on my own API. In this case, when I do the same with creating incident and leaving Description field blank, it doesn't show any error, it shows that response is OK 200, yet incident is not created.
So the question, how to make my own API work the same as Table API, with throwing Error and showing which mandatory field is empty?
I hope you understand me.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2017 07:27 AM
Hi Pavel,
Is this Scripted REST API in a Scoped or Global application?
A few things that might point you in the right direction:
1. When you call the .insert() method on a GlideRecord, it will return null if the record was not inserted (returns the inserted sys_id if it was successful)
2. gs.getErrorMessages() will return an array of messages for the current transaction (as far as I know, only available in the Global scope at this time)
Used in combination, you may be able to do something like this:
1. Attempt the insert
2. Check to see if the insert returned null
3. If it returned null, something failed, call gs.getErrorMessages()
4. Return the error messages in the API response and manually set the error code
Here's a bit of sample code:
And the result:
Scripted REST APIs give you full control by design, meaning that you must also decide how to handle failures.
I don't know all of the specifics of your implementation, but another option is to use a Web Service Import Set. These give you the ability to execute your own scripted logic in a Transform Map and Data Policies will also be honored as long as the "Apply to import sets" checkbox is checked on the Data Policy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2023 04:20 AM
With the same problem here too.
The messages return an empty array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 06:47 AM - edited ‎01-09-2024 06:51 AM
Ran into this issue today.
My guess is that the error messages are left in the queue only in interactive session and hence getErrorMessages returns the array only in an interactive session. This one is however not mentioned in any posts as far as I can see.
So, if a fix script is written like this
var gr = new GlideRecord('incident');
gr.initialize();
gr.insert();
var messages = gs.getErrorMessages();
gs.info('Error Messages: ' + messages);
And if we execute the Fix Script in Foreground (Run Fix Script > Proceed), then we can see the error messages in the logs. in the format
Error Messages: {Error Message} |
However if we execute the Fix Script in Background (Run Fix Script > Proceed in Background), then in the Progress Worker logs, we can see the error messages, but it is something like
Background message, type:error, message: {Error Message} Error Messages: |
It is not against the "Error Messages" preceding string, meaning that the getErrorMessages did not return anything.
Am not sure what the background message is though. Maybe used for node logs?
Edit: So, it seems like when using the REST API Explorer the getErrorMessages work and when actually calling REST API from another system, it will not.