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

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.


Thank you, that works like a charm!


Hello, 

I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.

Could you please guide me how to implement this? Sample code would be great.

Input: Sys ID of the record 
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"sys_id": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"sys_id": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}

Mark_Didrikson
ServiceNow Employee
ServiceNow Employee

I tried adding gs.getErrorMessages() to a Scripted Rest Web Service, but I'm not an empty response. I do have a Data Policy in place and if I run my script as a Fix Script, I get a valid error message. The record is not being inserted, but no error is being returned. Am I missing something?

 

Thanks!

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

// implement resource here
var gr = new GlideRecord('incident');
gr.initialize();
var r = gr.insert();

var messages = gs.getErrorMessages();

return {
r : r,
messages : messages
};

})(request, response);

Facing the same issue you did... Did you ever figure this out?