Error of missing mandatory fields in REST

PavelB07
Kilo Contributor

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.

1 ACCEPTED SOLUTION

josh_nerius
ServiceNow Employee
ServiceNow Employee

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:



https___dev15730_service-now_com__studio_do_sysparm_transaction_scope_822eceb3db02030005b1dec0cf961964_sysparm_nostack_true.png



And the result:



REST_API_Explorer.png



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.


View solution in original post

11 REPLIES 11

With the same problem here too.
The messages return an empty array

balu25int
Tera Contributor

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.