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

Gaurav Bajaj
Kilo Sage

Are you using scripted API, if yes then you can check the value of description in the request.


If it's not present, you can set the response and response status codes as per your need.




Thanks


Gaurav


Yes, I'm using scripted API.



The point is that that API will be used for automatic incident creation (via Powershell for example) and I want to throw an error if Data Policy doesn't let to create an incident - in this case data policy makes description mandatory, and when I send a request for creating an empty incident, that's what I get:



scripted API:


Capture.PNG



Table API:


Capture.PNG



This Table API error is a little bit different, but the problem is the same, with same Business rules and Data Policies my scripted API gets through (but doesn't post anything), while Table API shows errors as it should. What can be the problem in my API? Why it omits business rules and data policies while sending request?


Hi,



Since you are writing a script and it won't honor the data policy in place. You can use GlideRecordSecure in your script to enforce ACLs instead of Gliderecord.


Also, what you can do here is check the data policy table, get the policy action rules and then apply the same in your script.



Having said that, you need to incorporate everything in your script which happens on table API as I would be doing same in its implementation.



Thanks


Gaurav


I need to use as less scripting as I can, so the suggestion to apply policies action rules to script doesn't make any sense.



All I need is just to get thrown errors with details as in Table API. Unfortunately, I do not have access to Table API's code, so I'm asking if anybody else knows how to make to errors with Data Policies and Business rules?